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     */
026    protected AbstractProjectionChoice(String name, String id) {
027        this.name = name;
028        this.id = id;
029    }
030
031    @Override
032    public String getId() {
033        return id;
034    }
035
036    @Override
037    public String toString() {
038        return name;
039    }
040
041    /**
042     * Returns current projection code.
043     * @return current projection code
044     */
045    public abstract String getCurrentCode();
046
047    /**
048     * Returns projection name.
049     * @return projection name
050     */
051    public abstract String getProjectionName();
052
053    @Override
054    public Projection getProjection() {
055        String code = getCurrentCode();
056        return new CustomProjection(getProjectionName(), code, Optional.ofNullable(Projections.getInit(code))
057                .orElseThrow(() -> new AssertionError("Error: Unknown projection code: " + code)));
058    }
059}