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

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

javadoc

  • Property svn:eol-style set to native
File size: 3.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 public String getAPIName() {
42 return apiTypeName;
43 }
44
45 public Class<? extends OsmPrimitive> getOsmClass() {
46 return osmClass;
47 }
48
49 public Class<? extends PrimitiveData> getDataClass() {
50 return dataClass;
51 }
52
53 public static OsmPrimitiveType fromApiTypeName(String typeName) {
54 for (OsmPrimitiveType type : OsmPrimitiveType.values()) {
55 if (type.getAPIName().equals(typeName)) return type;
56 }
57 throw new IllegalArgumentException(MessageFormat.format(
58 "Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
59 }
60
61 /**
62 * Determines the OSM primitive type of the given object.
63 * @param obj the OSM object to inspect
64 * @return the OSM primitive type of {@code obj}
65 * @throws IllegalArgumentException if {@code obj} is null or of unknown type
66 */
67 public static OsmPrimitiveType from(IPrimitive obj) {
68 if (obj instanceof INode) return NODE;
69 if (obj instanceof IWay) return WAY;
70 if (obj instanceof IRelation) return RELATION;
71 throw new IllegalArgumentException("Unknown type: "+obj);
72 }
73
74 public static OsmPrimitiveType from(String value) {
75 if (value == null) return null;
76 for (OsmPrimitiveType type: values()) {
77 if (type.getAPIName().equalsIgnoreCase(value))
78 return type;
79 }
80 return null;
81 }
82
83 public static Collection<OsmPrimitiveType> dataValues() {
84 return DATA_VALUES;
85 }
86
87 public OsmPrimitive newInstance(long uniqueId, boolean allowNegative) {
88 switch (this) {
89 case NODE:
90 return new Node(uniqueId, allowNegative);
91 case WAY:
92 return new Way(uniqueId, allowNegative);
93 case RELATION:
94 return new Relation(uniqueId, allowNegative);
95 default:
96 throw new AssertionError();
97 }
98 }
99
100 @Override
101 public String toString() {
102 return tr(getAPIName());
103 }
104}
Note: See TracBrowser for help on using the repository browser.