source: josm/trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveType.java@ 12540

Last change on this file since 12540 was 12018, checked in by Don-vip, 7 years ago

add OsmPrimitiveType.newVersionedInstance() + javadoc and unit tests

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.text.MessageFormat;
8import java.util.Arrays;
9import java.util.Collection;
10
11/**
12 * OSM primitive type.
13 * @since 1670
14 */
15public enum OsmPrimitiveType {
16
17 /** Node type */
18 NODE(marktr(/* ICON(data/) */"node"), Node.class, NodeData.class),
19 /** Way type */
20 WAY(marktr(/* ICON(data/) */"way"), Way.class, WayData.class),
21 /** Relation type */
22 RELATION(marktr(/* ICON(data/) */"relation"), Relation.class, RelationData.class),
23
24 /** Closed way: only for display, no real type */
25 CLOSEDWAY(marktr(/* ICON(data/) */"closedway"), null, WayData.class),
26 /** Multipolygon: only for display, no real type */
27 MULTIPOLYGON(marktr(/* ICON(data/) */"multipolygon"), null, RelationData.class);
28
29 private static final Collection<OsmPrimitiveType> DATA_VALUES = Arrays.asList(NODE, WAY, RELATION);
30
31 private final String apiTypeName;
32 private final Class<? extends OsmPrimitive> osmClass;
33 private final Class<? extends PrimitiveData> dataClass;
34
35 OsmPrimitiveType(String apiTypeName, Class<? extends OsmPrimitive> osmClass, Class<? extends PrimitiveData> dataClass) {
36 this.apiTypeName = apiTypeName;
37 this.osmClass = osmClass;
38 this.dataClass = dataClass;
39 }
40
41 /**
42 * Returns the API type name / JOSM display name.
43 * @return the API type name / JOSM display name
44 */
45 public String getAPIName() {
46 return apiTypeName;
47 }
48
49 /**
50 * Returns the OSM class for data values, or null.
51 * @return the OSM class for data values, or null
52 */
53 public Class<? extends OsmPrimitive> getOsmClass() {
54 return osmClass;
55 }
56
57 /**
58 * Returns the data class.
59 * @return the data class
60 */
61 public Class<? extends PrimitiveData> getDataClass() {
62 return dataClass;
63 }
64
65 /**
66 * Returns enum value from API type name / JOSM display name, case sensitive.
67 * @param typeName API type name / JOSM display name, case sensitive
68 * @return matching enum value
69 * @throws IllegalArgumentException if the type name does not match any valid type
70 * @see #from(String)
71 */
72 public static OsmPrimitiveType fromApiTypeName(String typeName) {
73 for (OsmPrimitiveType type : OsmPrimitiveType.values()) {
74 if (type.getAPIName().equals(typeName)) return type;
75 }
76 throw new IllegalArgumentException(MessageFormat.format(
77 "Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
78 }
79
80 /**
81 * Determines the OSM primitive type of the given object.
82 * @param obj the OSM object to inspect
83 * @return the OSM primitive type of {@code obj}
84 * @throws IllegalArgumentException if {@code obj} is null or of unknown type
85 */
86 public static OsmPrimitiveType from(IPrimitive obj) {
87 if (obj instanceof INode) return NODE;
88 if (obj instanceof IWay) return WAY;
89 if (obj instanceof IRelation) return RELATION;
90 throw new IllegalArgumentException("Unknown type: "+obj);
91 }
92
93 /**
94 * Returns enum value from API type name / JOSM display name, case insensitive.
95 * @param value API type name / JOSM display name, case insensitive
96 * @return matching enum value or null
97 * @see #fromApiTypeName
98 */
99 public static OsmPrimitiveType from(String value) {
100 for (OsmPrimitiveType type: values()) {
101 if (type.getAPIName().equalsIgnoreCase(value))
102 return type;
103 }
104 return null;
105 }
106
107 /**
108 * Returns the values matching real OSM API data types (node, way, relation).
109 * @return the values matching real OSM API data types (node, way, relation)
110 */
111 public static Collection<OsmPrimitiveType> dataValues() {
112 return DATA_VALUES;
113 }
114
115 /**
116 * Constructs a new primitive instance (node, way or relation) without version.
117 * @param uniqueId the unique id
118 * @param allowNegative {@code true} to allow negative id
119 * @return a new primitive instance (node, way or relation)
120 * @throws IllegalArgumentException if uniqueId &lt; 0 and allowNegative is false
121 */
122 public OsmPrimitive newInstance(long uniqueId, boolean allowNegative) {
123 switch (this) {
124 case NODE:
125 return new Node(uniqueId, allowNegative);
126 case WAY:
127 return new Way(uniqueId, allowNegative);
128 case RELATION:
129 return new Relation(uniqueId, allowNegative);
130 default:
131 throw new AssertionError();
132 }
133 }
134
135 /**
136 * Constructs a new primitive instance (node, way or relation) with given version.
137 * @param id The id. Must be &gt;= 0
138 * @param version The version
139 * @return a new primitive instance (node, way or relation) with given version
140 * @throws IllegalArgumentException if id &lt; 0
141 * @since 12018
142 */
143 public OsmPrimitive newVersionedInstance(long id, int version) {
144 switch (this) {
145 case NODE:
146 return new Node(id, version);
147 case WAY:
148 return new Way(id, version);
149 case RELATION:
150 return new Relation(id, version);
151 default:
152 throw new AssertionError();
153 }
154 }
155
156 @Override
157 public String toString() {
158 return tr(getAPIName());
159 }
160}
Note: See TracBrowser for help on using the repository browser.