~ubuntu-branches/ubuntu/oneiric/mkgmap/oneiric

« back to all changes in this revision

Viewing changes to src/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnit.java

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine, Andreas Putzo, Francesco Paolo Lovergine
  • Date: 2009-07-16 11:10:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090716111016-yycxqya1f26xmti7
Tags: 0.0.0+svn1067-1
[ Andreas Putzo ]
* New upstream snapshot.
* Added ${misc:Depends} among dependencies to fix a lintian warning.
* Bumped debhelper compatibility level to 7.
* Updated long description.
* Updated Homepage in debian/control, debian/copyright, debian/watch.
* Added numerous files from /doc to debian/docs.
* Mentioned Bernhard Heibler in debian/copyright and updated copyright
  year of software and packaging.
* Bumped policy to 3.8.2, without changes.
* Added DM-Upload-Allowed to debian/control.

[ Francesco Paolo Lovergine ]
* Added me as Uploader to avoid possible inappropriate NMU notices.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Steve Ratcliffe
 
3
 * 
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License version 2 as
 
6
 *  published by the Free Software Foundation.
 
7
 * 
 
8
 *  This program is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 * 
 
13
 * 
 
14
 * Author: Steve Ratcliffe
 
15
 * Create date: 09-Nov-2008
 
16
 */
 
17
package uk.me.parabola.mkgmap.osmstyle.eval;
 
18
 
 
19
import java.util.regex.Matcher;
 
20
import java.util.regex.Pattern;
 
21
 
 
22
/**
 
23
 * Represents a number and the units it is in. We want ultimately to be
 
24
 * able to do things like: is 10km/h > 8mph, and get the right answer
 
25
 * by converting to a common unit.
 
26
 *
 
27
 * To start with we will just compare the numbers.
 
28
 *
 
29
 * @author Steve Ratcliffe
 
30
 */
 
31
public class ValueWithUnit implements Comparable<ValueWithUnit> {
 
32
        private static final Pattern EXTRACT_NUMBER_UNIT
 
33
                        = Pattern.compile("[ \t]*([0-9]+)[ \t]*(.*)");
 
34
 
 
35
        private final int value;
 
36
        private final String unit;
 
37
 
 
38
        private final boolean valid;
 
39
 
 
40
        public ValueWithUnit(String val) {
 
41
                Matcher m = EXTRACT_NUMBER_UNIT.matcher(val);
 
42
                boolean found = m.find();
 
43
                if (found) {
 
44
                        // If found is true, then the first group is an integer, so no need
 
45
                        // to check.
 
46
                        value = Integer.parseInt(m.group(1));
 
47
                        unit = m.group(2).trim();
 
48
                        valid = true;
 
49
                } else {
 
50
                        value = 0;
 
51
                        unit = val;
 
52
                        valid = false;
 
53
                }
 
54
        }
 
55
 
 
56
        /**
 
57
         * Compares this object with the specified object for order. Returns
 
58
         * a negative integer, zero, or a positive integer as this object
 
59
         * is less than, equal to, or greater than the specified object.
 
60
         *
 
61
         * To start with, just compare the value and ignore the unit.
 
62
         */
 
63
        public int compareTo(ValueWithUnit o) {
 
64
                if (value == o.value) return 0;
 
65
                else return value < o.value ? -1 : 1;
 
66
        }
 
67
 
 
68
        public boolean isValid() {
 
69
                return valid;
 
70
        }
 
71
 
 
72
        public String toString() {
 
73
                return value + unit;
 
74
        }
 
75
}