2
* Copyright (C) 2008 Steve Ratcliffe
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.
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.
14
* Author: Steve Ratcliffe
15
* Create date: 09-Nov-2008
17
package uk.me.parabola.mkgmap.osmstyle.eval;
19
import java.util.regex.Matcher;
20
import java.util.regex.Pattern;
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.
27
* To start with we will just compare the numbers.
29
* @author Steve Ratcliffe
31
public class ValueWithUnit implements Comparable<ValueWithUnit> {
32
private static final Pattern EXTRACT_NUMBER_UNIT
33
= Pattern.compile("[ \t]*([0-9]+)[ \t]*(.*)");
35
private final int value;
36
private final String unit;
38
private final boolean valid;
40
public ValueWithUnit(String val) {
41
Matcher m = EXTRACT_NUMBER_UNIT.matcher(val);
42
boolean found = m.find();
44
// If found is true, then the first group is an integer, so no need
46
value = Integer.parseInt(m.group(1));
47
unit = m.group(2).trim();
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.
61
* To start with, just compare the value and ignore the unit.
63
public int compareTo(ValueWithUnit o) {
64
if (value == o.value) return 0;
65
else return value < o.value ? -1 : 1;
68
public boolean isValid() {
72
public String toString() {