001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor.conversion;
003
004import static org.openstreetmap.josm.tools.I18n.trc;
005
006import java.text.DecimalFormat;
007import java.text.NumberFormat;
008import java.util.Locale;
009
010/**
011 * Abstract base class for {@link ICoordinateFormat} implementations.
012 * @since 12735
013 */
014public abstract class AbstractCoordinateFormat implements ICoordinateFormat {
015
016    protected final String id;
017    protected final String displayName;
018
019    /** The normal number format for server precision coordinates */
020    protected static final DecimalFormat cDdFormatter = newUnlocalizedDecimalFormat("###0.0######");
021    /** Character denoting South, as string */
022    protected static final String SOUTH = trc("compass", "S");
023    /** Character denoting North, as string */
024    protected static final String NORTH = trc("compass", "N");
025    /** Character denoting West, as string */
026    protected static final String WEST = trc("compass", "W");
027    /** Character denoting East, as string */
028    protected static final String EAST = trc("compass", "E");
029
030    protected AbstractCoordinateFormat(String id, String displayName) {
031        this.id = id;
032        this.displayName = displayName;
033    }
034
035    /**
036     * Creates a new unlocalized {@link DecimalFormat}.
037     * By not using the localized decimal separator, we can present a comma separated list of coordinates.
038     * @param pattern decimal format pattern
039     * @return {@code DecimalFormat} using dot as decimal separator
040     * @see DecimalFormat#applyPattern
041     * @since 14203
042     */
043    public static DecimalFormat newUnlocalizedDecimalFormat(String pattern) {
044        DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.UK);
045        format.applyPattern(pattern);
046        return format;
047    }
048
049    @Override
050    public String getId() {
051        return id;
052    }
053
054    @Override
055    public String getDisplayName() {
056        return displayName;
057    }
058
059    @Override
060    public String toString() {
061        return getDisplayName();
062    }
063}