1 | /* |
2 | * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences. |
3 | * Copyright (C) 2007 - JScience (http://jscience.org/) |
4 | * All rights reserved. |
5 | * |
6 | * Permission to use, copy, modify, and distribute this software is |
7 | * freely granted, provided that this notice is preserved. |
8 | */ |
9 | package javax.measure; |
10 | |
11 | import java.math.BigDecimal; |
12 | import java.text.FieldPosition; |
13 | import java.text.Format; |
14 | import java.text.NumberFormat; |
15 | import java.text.ParseException; |
16 | import java.text.ParsePosition; |
17 | |
18 | import javax.measure.unit.CompoundUnit; |
19 | import javax.measure.unit.Unit; |
20 | import javax.measure.unit.UnitFormat; |
21 | |
22 | /** |
23 | * <p> This class provides the interface for formatting and parsing {@link |
24 | * Measure measures}.</p> |
25 | * |
26 | * <p> As a minimum, instances of this class should be able to parse/format |
27 | * measure using {@link CompoundUnit}. </p> |
28 | * |
29 | * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> |
30 | * @version 4.2, August 26, 2007 |
31 | */ |
32 | public abstract class MeasureFormat extends Format { |
33 | |
34 | /** |
35 | * Returns the measure format for the default locale. |
36 | * |
37 | * @return <code>getInstance(Number.getInstance(), Unit.getInstance())</code> |
38 | */ |
39 | public static MeasureFormat getInstance() { |
40 | return DEFAULT; |
41 | } |
42 | |
43 | static final NumberUnit DEFAULT = new NumberUnit(NumberFormat |
44 | .getInstance(), UnitFormat.getInstance()); |
45 | |
46 | /** |
47 | * Returns the measure format using the specified number format and |
48 | * unit format (the number and unit are separated by a space). |
49 | * |
50 | * @param numberFormat the number format. |
51 | * @param unitFormat the unit format. |
52 | * @return the corresponding format. |
53 | */ |
54 | public static MeasureFormat getInstance(NumberFormat numberFormat, |
55 | UnitFormat unitFormat) { |
56 | return new NumberUnit(numberFormat, unitFormat); |
57 | } |
58 | |
59 | // Holds default implementation. |
60 | static final class NumberUnit extends MeasureFormat { |
61 | private final NumberFormat _numberFormat; |
62 | |
63 | private final UnitFormat _unitFormat; |
64 | |
65 | private NumberUnit(NumberFormat numberFormat, UnitFormat unitFormat) { |
66 | _numberFormat = numberFormat; |
67 | _unitFormat = unitFormat; |
68 | } |
69 | |
70 | @Override |
71 | public StringBuffer format(Object obj, StringBuffer toAppendTo, |
72 | FieldPosition pos) { |
73 | Measure<?, ?> measure = (Measure<?, ?>) obj; |
74 | Object value = measure.getValue(); |
75 | Unit<?> unit = measure.getUnit(); |
76 | if (value instanceof Number) { |
77 | if (unit instanceof CompoundUnit) |
78 | return formatCompound(((Number) value).doubleValue(), |
79 | (CompoundUnit<?>) unit, toAppendTo, pos); |
80 | _numberFormat.format(value, toAppendTo, pos); |
81 | } else { |
82 | toAppendTo.append(value); |
83 | } |
84 | if (!measure.getUnit().equals(Unit.ONE)) { |
85 | toAppendTo.append(' '); |
86 | _unitFormat.format(unit, toAppendTo, pos); |
87 | } |
88 | return toAppendTo; |
89 | } |
90 | |
91 | // Measure using Compound unit have no separators in their representation. |
92 | StringBuffer formatCompound(double value, Unit<?> unit, |
93 | StringBuffer toAppendTo, FieldPosition pos) { |
94 | if (!(unit instanceof CompoundUnit)) { |
95 | toAppendTo.append((long) value); |
96 | return _unitFormat.format(unit, toAppendTo, pos); |
97 | } |
98 | Unit<?> high = ((CompoundUnit<?>) unit).getHigher(); |
99 | Unit<?> low = ((CompoundUnit<?>) unit).getLower(); // The unit in which the value is stated. |
100 | long highValue = (long) low.getConverterTo(high).convert(value); |
101 | double lowValue = value |
102 | - high.getConverterTo(low).convert(highValue); |
103 | formatCompound(highValue, high, toAppendTo, pos); |
104 | formatCompound(lowValue, low, toAppendTo, pos); |
105 | return toAppendTo; |
106 | } |
107 | |
108 | @Override |
109 | public Object parseObject(String source, ParsePosition pos) { |
110 | int start = pos.getIndex(); |
111 | try { |
112 | int i = start; |
113 | Number value = _numberFormat.parse(source, pos); |
114 | if (i == pos.getIndex()) |
115 | return null; // Cannot parse. |
116 | i = pos.getIndex(); |
117 | if (i >= source.length()) |
118 | return measureOf(value, Unit.ONE); // No unit. |
119 | boolean isCompound = !Character.isWhitespace(source.charAt(i)); |
120 | if (isCompound) |
121 | return parseCompound(value, source, pos); |
122 | if (++i >= source.length()) |
123 | return measureOf(value, Unit.ONE); // No unit. |
124 | pos.setIndex(i); // Skips separator. |
125 | Unit<?> unit = _unitFormat.parseProductUnit(source, pos); |
126 | return measureOf(value, unit); |
127 | } catch (ParseException e) { |
128 | pos.setIndex(start); |
129 | pos.setErrorIndex(e.getErrorOffset()); |
130 | return null; |
131 | } |
132 | } |
133 | |
134 | @SuppressWarnings("unchecked") |
135 | private Object parseCompound(Number highValue, String source, |
136 | ParsePosition pos) throws ParseException { |
137 | Unit high = _unitFormat.parseSingleUnit(source, pos); |
138 | int i = pos.getIndex(); |
139 | if (i >= source.length() |
140 | || Character.isWhitespace(source.charAt(i))) |
141 | return measureOf(highValue, high); |
142 | Measure lowMeasure = (Measure) parseObject(source, pos); |
143 | Unit unit = lowMeasure.getUnit(); |
144 | long l = lowMeasure.longValue(unit) |
145 | + (long) high.getConverterTo(unit).convert( |
146 | highValue.longValue()); |
147 | return Measure.valueOf(l, unit); |
148 | } |
149 | |
150 | @SuppressWarnings("unchecked") |
151 | private static Measure measureOf(Number value, Unit unit) { |
152 | if (value instanceof Double) { |
153 | return Measure.valueOf(value.doubleValue(), unit); |
154 | } else if (value instanceof Long) { |
155 | return Measure.valueOf(value.longValue(), unit); |
156 | } else if (value instanceof Float) { |
157 | return Measure.valueOf(value.floatValue(), unit); |
158 | } else if (value instanceof Integer) { |
159 | return Measure.valueOf(value.intValue(), unit); |
160 | } else if (value instanceof BigDecimal) { |
161 | return DecimalMeasure.valueOf((BigDecimal) value, unit); |
162 | } else { |
163 | return Measure.valueOf(value.doubleValue(), unit); |
164 | } |
165 | } |
166 | |
167 | private static final long serialVersionUID = 1L; |
168 | } |
169 | } |