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

Last change on this file since 11285 was 9212, checked in by Don-vip, 8 years ago

checkstyle 6.14 + tune xml validation settings in Eclipse project

  • Property svn:eol-style set to native
File size: 3.2 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
11public 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}
Note: See TracBrowser for help on using the repository browser.