001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.projection;
003
004import java.util.Optional;
005
006import org.openstreetmap.josm.data.projection.CustomProjection;
007import org.openstreetmap.josm.data.projection.Projection;
008import org.openstreetmap.josm.data.projection.Projections;
009
010/**
011 * Super class for ProjectionChoice implementations.
012 * <p>
013 * Handles common parameters <code>name</code> and <code>id</code>.
014 */
015public abstract class AbstractProjectionChoice implements ProjectionChoice {
016
017    protected String name;
018    protected String id;
019
020    /**
021     * Constructs a new {@code AbstractProjectionChoice}.
022     *
023     * @param name short name of the projection choice as shown in the GUI
024     * @param id unique identifier for the projection choice
025     * @param cacheDir unused
026     * @deprecated use {@link #AbstractProjectionChoice(String, String)} instead
027     */
028    @Deprecated
029    public AbstractProjectionChoice(String name, String id, String cacheDir) {
030        this(name, id);
031    }
032
033    /**
034     * Constructs a new {@code AbstractProjectionChoice}.
035     *
036     * @param name short name of the projection choice as shown in the GUI
037     * @param id unique identifier for the projection choice
038     */
039    public AbstractProjectionChoice(String name, String id) {
040        this.name = name;
041        this.id = id;
042    }
043
044    @Override
045    public String getId() {
046        return id;
047    }
048
049    @Override
050    public String toString() {
051        return name;
052    }
053
054    /**
055     * Returns current projection code.
056     * @return current projection code
057     */
058    public abstract String getCurrentCode();
059
060    /**
061     * Returns projection name.
062     * @return projection name
063     */
064    public abstract String getProjectionName();
065
066    @Override
067    public Projection getProjection() {
068        String code = getCurrentCode();
069        return new CustomProjection(getProjectionName(), code, Optional.ofNullable(Projections.getInit(code))
070                .orElseThrow(() -> new AssertionError("Error: Unknown projection code: " + code)));
071    }
072}