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

Last change on this file since 7392 was 6889, checked in by Don-vip, 10 years ago

fix some Sonar issues (JLS order)

  • Property svn:eol-style set to native
File size: 2.9 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("Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
50 }
51
52 public static OsmPrimitiveType from(IPrimitive obj) {
53 if (obj instanceof INode) return NODE;
54 if (obj instanceof IWay) return WAY;
55 if (obj instanceof IRelation) return RELATION;
56 throw new IllegalArgumentException();
57 }
58
59 public static OsmPrimitiveType from(String value) {
60 if (value == null) return null;
61 for (OsmPrimitiveType type: values()){
62 if (type.getAPIName().equalsIgnoreCase(value))
63 return type;
64 }
65 return null;
66 }
67
68 public static Collection<OsmPrimitiveType> dataValues() {
69 return DATA_VALUES;
70 }
71
72 public OsmPrimitive newInstance(long uniqueId, boolean allowNegative) {
73 switch (this) {
74 case NODE:
75 return new Node(uniqueId, allowNegative);
76 case WAY:
77 return new Way(uniqueId, allowNegative);
78 case RELATION:
79 return new Relation(uniqueId, allowNegative);
80 default:
81 throw new AssertionError();
82 }
83 }
84
85 @Override
86 public String toString() {
87 return tr(getAPIName());
88 }
89}
Note: See TracBrowser for help on using the repository browser.