Ignore:
Timestamp:
2019-09-30T23:31:03+02:00 (5 years ago)
Author:
Don-vip
Message:

sets the default system of measurement based on current country

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/SystemOfMeasurement.java

    r12846 r15395  
    55
    66import java.text.NumberFormat;
    7 import java.util.Collections;
    8 import java.util.LinkedHashMap;
    97import java.util.Locale;
    108import java.util.Map;
    119import java.util.Optional;
    1210import java.util.concurrent.CopyOnWriteArrayList;
     11import java.util.function.Function;
     12import java.util.stream.Collectors;
     13import java.util.stream.Stream;
    1314
    1415import org.openstreetmap.josm.data.preferences.StringProperty;
     
    2526
    2627    /**
    27      * Preferences entry for system of measurement.
    28      * @since 12674 (moved from ProjectionPreference)
    29      */
    30     public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", "Metric");
    31 
    32     /**
    3328     * Interface to notify listeners of the change of the system of measurement.
    3429     * @since 8554
     
    4944     * @since 3406
    5045     */
    51     public static final SystemOfMeasurement METRIC = new SystemOfMeasurement(1, "m", 1000, "km", "km/h", 3.6, 10_000, "ha");
     46    public static final SystemOfMeasurement METRIC = new SystemOfMeasurement(marktr("Metric"), 1, "m", 1000, "km", "km/h", 3.6, 10_000, "ha");
    5247
    5348    /**
     
    5752     * @since 3406
    5853     */
    59     public static final SystemOfMeasurement CHINESE = new SystemOfMeasurement(1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */,
    60             "km/h", 3.6, 666.0 + 2.0/3.0, "\u4ea9" /* mu */);
     54    public static final SystemOfMeasurement CHINESE = new SystemOfMeasurement(marktr("Chinese"),
     55            1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */, "km/h", 3.6, 666.0 + 2.0/3.0, "\u4ea9" /* mu */);
    6156
    6257    /**
     
    6459     * @since 3406
    6560     */
    66     public static final SystemOfMeasurement IMPERIAL = new SystemOfMeasurement(0.3048, "ft", 1609.344, "mi", "mph", 2.23694, 4046.86, "ac");
     61    public static final SystemOfMeasurement IMPERIAL = new SystemOfMeasurement(marktr("Imperial"),
     62            0.3048, "ft", 1609.344, "mi", "mph", 2.23694, 4046.86, "ac");
    6763
    6864    /**
     
    7066     * @since 5549
    7167     */
    72     public static final SystemOfMeasurement NAUTICAL_MILE = new SystemOfMeasurement(185.2, "kbl", 1852, "NM", "kn", 1.94384);
     68    public static final SystemOfMeasurement NAUTICAL_MILE = new SystemOfMeasurement(marktr("Nautical Mile"),
     69            185.2, "kbl", 1852, "NM", "kn", 1.94384);
    7370
    7471    /**
     
    7673     * @since 3406
    7774     */
    78     public static final Map<String, SystemOfMeasurement> ALL_SYSTEMS;
    79     static {
    80         Map<String, SystemOfMeasurement> map = new LinkedHashMap<>();
    81         map.put(marktr("Metric"), METRIC);
    82         map.put(marktr("Chinese"), CHINESE);
    83         map.put(marktr("Imperial"), IMPERIAL);
    84         map.put(marktr("Nautical Mile"), NAUTICAL_MILE);
    85         ALL_SYSTEMS = Collections.unmodifiableMap(map);
    86     }
     75    public static final Map<String, SystemOfMeasurement> ALL_SYSTEMS = Stream.of(METRIC, CHINESE, IMPERIAL, NAUTICAL_MILE)
     76            .collect(Collectors.toMap(SystemOfMeasurement::getName, Function.identity()));
     77
     78    /**
     79     * Preferences entry for system of measurement.
     80     * @since 12674 (moved from ProjectionPreference)
     81     */
     82    public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", getDefault().getName());
    8783
    8884    private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<>();
     
    142138    }
    143139
     140    /** Translated name of this system of measurement. */
     141    private final String name;
    144142    /** First value, in meters, used to translate unit according to above formula. */
    145143    public final double aValue;
     
    169167     * x_a == x_m / aValue
    170168     *
     169     * @param name Translated name of this system of measurement
    171170     * @param aValue First value, in meters, used to translate unit according to above formula.
    172171     * @param aName First unit used to format text.
     
    175174     * @param speedName the most common speed symbol (kmh/h, mph, kn, etc.)
    176175     * @param speedValue the speed value for the most common speed symbol, for 1 meter per second
    177      * @since 10175
    178      */
    179     public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, String speedName, double speedValue) {
    180         this(aValue, aName, bValue, bName, speedName, speedValue, -1, null);
     176     * @since 15395
     177     */
     178    public SystemOfMeasurement(String name, double aValue, String aName, double bValue, String bName, String speedName, double speedValue) {
     179        this(name, aValue, aName, bValue, bName, speedName, speedValue, -1, null);
    181180    }
    182181
     
    187186     * x_a == x_m / aValue
    188187     *
     188     * @param name Translated name of this system of measurement
    189189     * @param aValue First value, in meters, used to translate unit according to above formula.
    190190     * @param aName First unit used to format text.
     
    197197     * @param areaCustomName Specific optional area unit. Set to {@code null} if not used.
    198198     *
    199      * @since 10175
    200      */
    201     public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, String speedName, double speedValue,
     199     * @since 15395
     200     */
     201    public SystemOfMeasurement(String name, double aValue, String aName, double bValue, String bName, String speedName, double speedValue,
    202202            double areaCustomValue, String areaCustomName) {
     203        this.name = name;
    203204        this.aValue = aValue;
    204205        this.aName = aName;
     
    271272    }
    272273
     274    /**
     275     * Returns the translated name of this system of measurement.
     276     * @return the translated name of this system of measurement
     277     * @since 15395
     278     */
     279    public String getName() {
     280        return name;
     281    }
     282
     283    /**
     284     * Returns the default system of measurement for the current country.
     285     * @return the default system of measurement for the current country
     286     * @since 15395
     287     */
     288    public static SystemOfMeasurement getDefault() {
     289        switch (Locale.getDefault().getCountry()) {
     290            case "US":
     291                // https://en.wikipedia.org/wiki/Metrication_in_the_United_States#Current_use
     292                // Imperial units still used in transportation and Earth sciences
     293                return IMPERIAL;
     294            default:
     295                return METRIC;
     296        }
     297    }
     298
    273299    private static String formatText(double v, String unit, NumberFormat format) {
    274300        if (format != null) {
Note: See TracChangeset for help on using the changeset viewer.