source: josm/trunk/src/org/openstreetmap/josm/data/SystemOfMeasurement.java@ 11710

Last change on this file since 11710 was 11553, checked in by Don-vip, 7 years ago

refactor handling of null values - use Java 8 Optional where possible

  • Property svn:eol-style set to native
File size: 11.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5
6import java.text.NumberFormat;
7import java.util.Collections;
8import java.util.LinkedHashMap;
9import java.util.Locale;
10import java.util.Map;
11import java.util.Optional;
12import java.util.concurrent.CopyOnWriteArrayList;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
16
17/**
18 * A system of units used to express length and area measurements.
19 * <p>
20 * This class also manages one globally set system of measurement stored in the {@link ProjectionPreference}
21 * @since 3406 (creation)
22 * @since 6992 (extraction in this package)
23 */
24public class SystemOfMeasurement {
25
26 /**
27 * Interface to notify listeners of the change of the system of measurement.
28 * @since 8554
29 * @since 10600 (functional interface)
30 */
31 @FunctionalInterface
32 public interface SoMChangeListener {
33 /**
34 * The current SoM has changed.
35 * @param oldSoM The old system of measurement
36 * @param newSoM The new (current) system of measurement
37 */
38 void systemOfMeasurementChanged(String oldSoM, String newSoM);
39 }
40
41 /**
42 * Metric system (international standard).
43 * @since 3406
44 */
45 public static final SystemOfMeasurement METRIC = new SystemOfMeasurement(1, "m", 1000, "km", "km/h", 3.6, 10_000, "ha");
46
47 /**
48 * Chinese system.
49 * See <a href="https://en.wikipedia.org/wiki/Chinese_units_of_measurement#Chinese_length_units_effective_in_1930">length units</a>,
50 * <a href="https://en.wikipedia.org/wiki/Chinese_units_of_measurement#Chinese_area_units_effective_in_1930">area units</a>
51 * @since 3406
52 */
53 public static final SystemOfMeasurement CHINESE = new SystemOfMeasurement(1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */,
54 "km/h", 3.6, 666.0 + 2.0/3.0, "\u4ea9" /* mu */);
55
56 /**
57 * Imperial system (British Commonwealth and former British Empire).
58 * @since 3406
59 */
60 public static final SystemOfMeasurement IMPERIAL = new SystemOfMeasurement(0.3048, "ft", 1609.344, "mi", "mph", 2.23694, 4046.86, "ac");
61
62 /**
63 * Nautical mile system (navigation, polar exploration).
64 * @since 5549
65 */
66 public static final SystemOfMeasurement NAUTICAL_MILE = new SystemOfMeasurement(185.2, "kbl", 1852, "NM", "kn", 1.94384);
67
68 /**
69 * Known systems of measurement.
70 * @since 3406
71 */
72 public static final Map<String, SystemOfMeasurement> ALL_SYSTEMS;
73 static {
74 Map<String, SystemOfMeasurement> map = new LinkedHashMap<>();
75 map.put(marktr("Metric"), METRIC);
76 map.put(marktr("Chinese"), CHINESE);
77 map.put(marktr("Imperial"), IMPERIAL);
78 map.put(marktr("Nautical Mile"), NAUTICAL_MILE);
79 ALL_SYSTEMS = Collections.unmodifiableMap(map);
80 }
81
82 private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<>();
83
84 /**
85 * Removes a global SoM change listener.
86 *
87 * @param listener the listener. Ignored if null or already absent
88 * @since 8554
89 */
90 public static void removeSoMChangeListener(SoMChangeListener listener) {
91 somChangeListeners.remove(listener);
92 }
93
94 /**
95 * Adds a SoM change listener.
96 *
97 * @param listener the listener. Ignored if null or already registered.
98 * @since 8554
99 */
100 public static void addSoMChangeListener(SoMChangeListener listener) {
101 if (listener != null) {
102 somChangeListeners.addIfAbsent(listener);
103 }
104 }
105
106 protected static void fireSoMChanged(String oldSoM, String newSoM) {
107 for (SoMChangeListener l : somChangeListeners) {
108 l.systemOfMeasurementChanged(oldSoM, newSoM);
109 }
110 }
111
112 /**
113 * Returns the current global system of measurement.
114 * @return The current system of measurement (metric system by default).
115 * @since 8554
116 */
117 public static SystemOfMeasurement getSystemOfMeasurement() {
118 return Optional.ofNullable(SystemOfMeasurement.ALL_SYSTEMS.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get()))
119 .orElse(SystemOfMeasurement.METRIC);
120 }
121
122 /**
123 * Sets the current global system of measurement.
124 * @param somKey The system of measurement key. Must be defined in {@link SystemOfMeasurement#ALL_SYSTEMS}.
125 * @throws IllegalArgumentException if {@code somKey} is not known
126 * @since 8554
127 */
128 public static void setSystemOfMeasurement(String somKey) {
129 if (!SystemOfMeasurement.ALL_SYSTEMS.containsKey(somKey)) {
130 throw new IllegalArgumentException("Invalid system of measurement: "+somKey);
131 }
132 String oldKey = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
133 if (ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.put(somKey)) {
134 fireSoMChanged(oldKey, somKey);
135 }
136 }
137
138 /** First value, in meters, used to translate unit according to above formula. */
139 public final double aValue;
140 /** Second value, in meters, used to translate unit according to above formula. */
141 public final double bValue;
142 /** First unit used to format text. */
143 public final String aName;
144 /** Second unit used to format text. */
145 public final String bName;
146 /** Speed value for the most common speed symbol, in meters per second
147 * @since 10175 */
148 public final double speedValue;
149 /** Most common speed symbol (kmh/h, mph, kn, etc.)
150 * @since 10175 */
151 public final String speedName;
152 /** Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}. Set to {@code -1} if not used.
153 * @since 5870 */
154 public final double areaCustomValue;
155 /** Specific optional area unit. Set to {@code null} if not used.
156 * @since 5870 */
157 public final String areaCustomName;
158
159 /**
160 * System of measurement. Currently covers only length (and area) units.
161 *
162 * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as
163 * x_a == x_m / aValue
164 *
165 * @param aValue First value, in meters, used to translate unit according to above formula.
166 * @param aName First unit used to format text.
167 * @param bValue Second value, in meters, used to translate unit according to above formula.
168 * @param bName Second unit used to format text.
169 * @param speedName the most common speed symbol (kmh/h, mph, kn, etc.)
170 * @param speedValue the speed value for the most common speed symbol, for 1 meter per second
171 * @since 10175
172 */
173 public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, String speedName, double speedValue) {
174 this(aValue, aName, bValue, bName, speedName, speedValue, -1, null);
175 }
176
177 /**
178 * System of measurement. Currently covers only length (and area) units.
179 *
180 * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as
181 * x_a == x_m / aValue
182 *
183 * @param aValue First value, in meters, used to translate unit according to above formula.
184 * @param aName First unit used to format text.
185 * @param bValue Second value, in meters, used to translate unit according to above formula.
186 * @param bName Second unit used to format text.
187 * @param speedName the most common speed symbol (kmh/h, mph, kn, etc.)
188 * @param speedValue the speed value for the most common speed symbol, for 1 meter per second
189 * @param areaCustomValue Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}.
190 * Set to {@code -1} if not used.
191 * @param areaCustomName Specific optional area unit. Set to {@code null} if not used.
192 *
193 * @since 10175
194 */
195 public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, String speedName, double speedValue,
196 double areaCustomValue, String areaCustomName) {
197 this.aValue = aValue;
198 this.aName = aName;
199 this.bValue = bValue;
200 this.bName = bName;
201 this.speedValue = speedValue;
202 this.speedName = speedName;
203 this.areaCustomValue = areaCustomValue;
204 this.areaCustomName = areaCustomName;
205 }
206
207 /**
208 * Returns the text describing the given distance in this system of measurement.
209 * @param dist The distance in metres
210 * @return The text describing the given distance in this system of measurement.
211 */
212 public String getDistText(double dist) {
213 return getDistText(dist, null, 0.01);
214 }
215
216 /**
217 * Returns the text describing the given distance in this system of measurement.
218 * @param dist The distance in metres
219 * @param format A {@link NumberFormat} to format the area value
220 * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
221 * @return The text describing the given distance in this system of measurement.
222 * @since 6422
223 */
224 public String getDistText(final double dist, final NumberFormat format, final double threshold) {
225 double a = dist / aValue;
226 if (a > bValue / aValue && !Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false))
227 return formatText(dist / bValue, bName, format);
228 else if (a < threshold)
229 return "< " + formatText(threshold, aName, format);
230 else
231 return formatText(a, aName, format);
232 }
233
234 /**
235 * Returns the text describing the given area in this system of measurement.
236 * @param area The area in square metres
237 * @return The text describing the given area in this system of measurement.
238 * @since 5560
239 */
240 public String getAreaText(double area) {
241 return getAreaText(area, null, 0.01);
242 }
243
244 /**
245 * Returns the text describing the given area in this system of measurement.
246 * @param area The area in square metres
247 * @param format A {@link NumberFormat} to format the area value
248 * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
249 * @return The text describing the given area in this system of measurement.
250 * @since 6422
251 */
252 public String getAreaText(final double area, final NumberFormat format, final double threshold) {
253 double a = area / (aValue*aValue);
254 boolean lowerOnly = Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false);
255 boolean customAreaOnly = Main.pref.getBoolean("system_of_measurement.use_only_custom_area_unit", false);
256 if ((!lowerOnly && areaCustomValue > 0 && a > areaCustomValue / (aValue*aValue)
257 && a < (bValue*bValue) / (aValue*aValue)) || customAreaOnly)
258 return formatText(area / areaCustomValue, areaCustomName, format);
259 else if (!lowerOnly && a >= (bValue*bValue) / (aValue*aValue))
260 return formatText(area / (bValue * bValue), bName + '\u00b2', format);
261 else if (a < threshold)
262 return "< " + formatText(threshold, aName + '\u00b2', format);
263 else
264 return formatText(a, aName + '\u00b2', format);
265 }
266
267 private static String formatText(double v, String unit, NumberFormat format) {
268 if (format != null) {
269 return format.format(v) + ' ' + unit;
270 }
271 return String.format(Locale.US, v < 9.999999 ? "%.2f %s" : "%.1f %s", v, unit);
272 }
273}
Note: See TracBrowser for help on using the repository browser.