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