| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.coor.conversion;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.ArrayList;
|
|---|
| 5 | import java.util.Collections;
|
|---|
| 6 | import java.util.List;
|
|---|
| 7 |
|
|---|
| 8 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Class that manages the available coordinate formats.
|
|---|
| 12 | * @since 12735
|
|---|
| 13 | */
|
|---|
| 14 | public class CoordinateFormatManager {
|
|---|
| 15 |
|
|---|
| 16 | private static final List<ICoordinateFormat> formats = new ArrayList<>();
|
|---|
| 17 |
|
|---|
| 18 | static {
|
|---|
| 19 | registerCoordinateFormat(DecimalDegreesCoordinateFormat.INSTANCE);
|
|---|
| 20 | registerCoordinateFormat(DMSCoordinateFormat.INSTANCE);
|
|---|
| 21 | registerCoordinateFormat(NauticalCoordinateFormat.INSTANCE);
|
|---|
| 22 | registerCoordinateFormat(ProjectedCoordinateFormat.INSTANCE);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | private CoordinateFormatManager() {
|
|---|
| 26 | // hide constructor
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Register a coordinate format.
|
|---|
| 31 | * <p>
|
|---|
| 32 | * It will be available as a choice in the preferences.
|
|---|
| 33 | * @param format the coordinate format
|
|---|
| 34 | */
|
|---|
| 35 | public static void registerCoordinateFormat(ICoordinateFormat format) {
|
|---|
| 36 | formats.add(format);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Get the list of all registered coordinate formats.
|
|---|
| 41 | * @return the list of all registered coordinate formats
|
|---|
| 42 | */
|
|---|
| 43 | public static List<ICoordinateFormat> getCoordinateFormats() {
|
|---|
| 44 | return Collections.unmodifiableList(formats);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | private static volatile ICoordinateFormat defaultCoordinateFormat = DecimalDegreesCoordinateFormat.INSTANCE;
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Replies the default coordinate format to be use
|
|---|
| 51 | *
|
|---|
| 52 | * @return the default coordinate format
|
|---|
| 53 | */
|
|---|
| 54 | public static ICoordinateFormat getDefaultFormat() {
|
|---|
| 55 | return defaultCoordinateFormat;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Sets the default coordinate format
|
|---|
| 60 | *
|
|---|
| 61 | * @param format the default coordinate format
|
|---|
| 62 | */
|
|---|
| 63 | public static void setCoordinateFormat(ICoordinateFormat format) {
|
|---|
| 64 | if (format != null) {
|
|---|
| 65 | defaultCoordinateFormat = format;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Get registered coordinate format by id
|
|---|
| 71 | *
|
|---|
| 72 | * @param id id of the coordinate format to get
|
|---|
| 73 | * @return the registered {@link ICoordinateFormat} with given id, or <code>null</code>
|
|---|
| 74 | * if no match is found
|
|---|
| 75 | */
|
|---|
| 76 | public static ICoordinateFormat getCoordinateFormat(String id) {
|
|---|
| 77 | CheckParameterUtil.ensureParameterNotNull(id, "id");
|
|---|
| 78 | return formats.stream().filter(format -> id.equals(format.getId())).findFirst().orElse(null);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|