| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.osm; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.text.MessageFormat; |
|---|
| 8 | import java.util.Arrays; |
|---|
| 9 | import java.util.Collection; |
|---|
| 10 | |
|---|
| 11 | public 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 final static 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 | } |
|---|