001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.coor.conversion;
003
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.List;
007
008import org.openstreetmap.josm.tools.CheckParameterUtil;
009
010/**
011 * Class that manages the available coordinate formats.
012 * @since 12735
013 */
014public final class CoordinateFormatManager {
015
016    private static final List<ICoordinateFormat> formats = new ArrayList<>();
017
018    static {
019        registerCoordinateFormat(DecimalDegreesCoordinateFormat.INSTANCE);
020        registerCoordinateFormat(DMSCoordinateFormat.INSTANCE);
021        registerCoordinateFormat(NauticalCoordinateFormat.INSTANCE);
022        registerCoordinateFormat(ProjectedCoordinateFormat.INSTANCE);
023    }
024
025    private CoordinateFormatManager() {
026        // hide constructor
027    }
028
029    /**
030     * Register a coordinate format.
031     * <p>
032     * It will be available as a choice in the preferences.
033     * @param format the coordinate format
034     */
035    public static void registerCoordinateFormat(ICoordinateFormat format) {
036        formats.add(format);
037    }
038
039    /**
040     * Get the list of all registered coordinate formats.
041     * @return the list of all registered coordinate formats
042     */
043    public static List<ICoordinateFormat> getCoordinateFormats() {
044        return Collections.unmodifiableList(formats);
045    }
046
047    private static volatile ICoordinateFormat defaultCoordinateFormat = DecimalDegreesCoordinateFormat.INSTANCE;
048
049    /**
050     * Replies the default coordinate format to be use
051     *
052     * @return the default coordinate format
053     */
054    public static ICoordinateFormat getDefaultFormat() {
055        return defaultCoordinateFormat;
056    }
057
058    /**
059     * Sets the default coordinate format
060     *
061     * @param format the default coordinate format
062     */
063    public static void setCoordinateFormat(ICoordinateFormat format) {
064        if (format != null) {
065            defaultCoordinateFormat = format;
066        }
067    }
068
069    /**
070     * Get registered coordinate format by id
071     *
072     * @param id id of the coordinate format to get
073     * @return the registered {@link ICoordinateFormat} with given id, or <code>null</code>
074     * if no match is found
075     */
076    public static ICoordinateFormat getCoordinateFormat(String id) {
077        CheckParameterUtil.ensureParameterNotNull(id, "id");
078        return formats.stream().filter(format -> id.equals(format.getId())).findFirst().orElse(null);
079    }
080}