001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.text.DecimalFormatSymbols;
005import java.text.spi.DecimalFormatSymbolsProvider;
006import java.util.Locale;
007
008/**
009 * JOSM implementation of the {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} class,
010 * consistent with ISO 80000-1.
011 * This class will only be used with Java 9 and later runtimes, as Java 8 implementation relies
012 * on Java Extension Mechanism only, while Java 9 supports application classpath.
013 * See {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider} javadoc for more details.
014 * @since 12931
015 */
016public class JosmDecimalFormatSymbolsProvider extends DecimalFormatSymbolsProvider {
017
018    @Override
019    public DecimalFormatSymbols getInstance(Locale locale) {
020        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
021        // Override digit group separator to be consistent across languages with ISO 80000-1, chapter 7.3.1
022        symbols.setGroupingSeparator('\u202F'); // U+202F: NARROW NO-BREAK SPACE
023        return symbols;
024    }
025
026    @Override
027    public Locale[] getAvailableLocales() {
028        return I18n.getAvailableTranslations();
029    }
030
031    /**
032     * Returns a new {@code double} initialized to the value represented by the specified {@code String},
033     * allowing both dot and comma decimal separators.
034     *
035     * @param  s   the string to be parsed.
036     * @return the {@code double} value represented by the string argument.
037     * @throws NullPointerException  if the string is null
038     * @throws NumberFormatException if the string does not contain a parsable {@code double}.
039     * @see    Double#parseDouble(String)
040     * @since 13050
041     */
042    public static double parseDouble(String s) {
043        return Double.parseDouble(s.replace(',', '.'));
044    }
045}