1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.text.MessageFormat;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | public enum OsmPrimitiveType {
|
---|
12 |
|
---|
13 | NODE(marktr(/* ICON(data/) */"node"), Node.class, NodeData.class),
|
---|
14 | WAY(marktr(/* ICON(data/) */"way"), Way.class, WayData.class),
|
---|
15 | RELATION(marktr(/* ICON(data/) */"relation"), Relation.class, RelationData.class),
|
---|
16 |
|
---|
17 | /* only for display, no real type */
|
---|
18 | CLOSEDWAY(marktr(/* ICON(data/) */"closedway"), null, WayData.class),
|
---|
19 | MULTIPOLYGON(marktr(/* ICON(data/) */"multipolygon"), null, RelationData.class);
|
---|
20 |
|
---|
21 | private static final Collection<OsmPrimitiveType> DATA_VALUES = Arrays.asList(NODE, WAY, RELATION);
|
---|
22 |
|
---|
23 | private final String apiTypeName;
|
---|
24 | private final Class<? extends OsmPrimitive> osmClass;
|
---|
25 | private final Class<? extends PrimitiveData> dataClass;
|
---|
26 |
|
---|
27 | OsmPrimitiveType(String apiTypeName, Class<? extends OsmPrimitive> osmClass, Class<? extends PrimitiveData> dataClass) {
|
---|
28 | this.apiTypeName = apiTypeName;
|
---|
29 | this.osmClass = osmClass;
|
---|
30 | this.dataClass = dataClass;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public String getAPIName() {
|
---|
34 | return apiTypeName;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public Class<? extends OsmPrimitive> getOsmClass() {
|
---|
38 | return osmClass;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public Class<? extends PrimitiveData> getDataClass() {
|
---|
42 | return dataClass;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public static OsmPrimitiveType fromApiTypeName(String typeName) {
|
---|
46 | for (OsmPrimitiveType type : OsmPrimitiveType.values()) {
|
---|
47 | if (type.getAPIName().equals(typeName)) return type;
|
---|
48 | }
|
---|
49 | throw new IllegalArgumentException(MessageFormat.format(
|
---|
50 | "Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Determines the OSM primitive type of the given object.
|
---|
55 | * @param obj the OSM object to inspect
|
---|
56 | * @return the OSM primitive type of {@code obj}
|
---|
57 | * @throws IllegalArgumentException if {@code obj} is null or of unknown type
|
---|
58 | */
|
---|
59 | public static OsmPrimitiveType from(IPrimitive obj) {
|
---|
60 | if (obj instanceof INode) return NODE;
|
---|
61 | if (obj instanceof IWay) return WAY;
|
---|
62 | if (obj instanceof IRelation) return RELATION;
|
---|
63 | throw new IllegalArgumentException("Unknown type: "+obj);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static OsmPrimitiveType from(String value) {
|
---|
67 | if (value == null) return null;
|
---|
68 | for (OsmPrimitiveType type: values()) {
|
---|
69 | if (type.getAPIName().equalsIgnoreCase(value))
|
---|
70 | return type;
|
---|
71 | }
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static Collection<OsmPrimitiveType> dataValues() {
|
---|
76 | return DATA_VALUES;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public OsmPrimitive newInstance(long uniqueId, boolean allowNegative) {
|
---|
80 | switch (this) {
|
---|
81 | case NODE:
|
---|
82 | return new Node(uniqueId, allowNegative);
|
---|
83 | case WAY:
|
---|
84 | return new Way(uniqueId, allowNegative);
|
---|
85 | case RELATION:
|
---|
86 | return new Relation(uniqueId, allowNegative);
|
---|
87 | default:
|
---|
88 | throw new AssertionError();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public String toString() {
|
---|
94 | return tr(getAPIName());
|
---|
95 | }
|
---|
96 | }
|
---|