001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.data.osm;
003    
004    import static org.openstreetmap.josm.tools.I18n.marktr;
005    import static org.openstreetmap.josm.tools.I18n.tr;
006    
007    import java.text.MessageFormat;
008    import java.util.Arrays;
009    import java.util.Collection;
010    
011    public enum OsmPrimitiveType {
012    
013        NODE (marktr(/* ICON(data/) */"node"), Node.class, NodeData.class),
014        WAY  (marktr(/* ICON(data/) */"way"), Way.class, WayData.class),
015        RELATION (marktr(/* ICON(data/) */"relation"), Relation.class, RelationData.class),
016    
017        /* only for display, no real type */
018        CLOSEDWAY  (marktr(/* ICON(data/) */"closedway"), null, WayData.class),
019        MULTIPOLYGON (marktr(/* ICON(data/) */"multipolygon"), null, RelationData.class);
020    
021        private final static Collection<OsmPrimitiveType> DATA_VALUES = Arrays.asList(NODE, WAY, RELATION);
022    
023        private final String apiTypeName;
024        private final Class<? extends OsmPrimitive> osmClass;
025        private final Class<? extends PrimitiveData> dataClass;
026    
027        OsmPrimitiveType(String apiTypeName, Class<? extends OsmPrimitive> osmClass, Class<? extends PrimitiveData> dataClass) {
028            this.apiTypeName = apiTypeName;
029            this.osmClass = osmClass;
030            this.dataClass = dataClass;
031        }
032    
033        public String getAPIName() {
034            return apiTypeName;
035        }
036    
037        public Class<? extends OsmPrimitive> getOsmClass() {
038            return osmClass;
039        }
040    
041        public Class<? extends PrimitiveData> getDataClass() {
042            return dataClass;
043        }
044    
045        public static OsmPrimitiveType fromApiTypeName(String typeName) {
046            for (OsmPrimitiveType type : OsmPrimitiveType.values()) {
047                if (type.getAPIName().equals(typeName)) return type;
048            }
049            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
050        }
051    
052        public static OsmPrimitiveType from(IPrimitive obj) {
053            if (obj instanceof INode) return NODE;
054            if (obj instanceof IWay) return WAY;
055            if (obj instanceof IRelation) return RELATION;
056            throw new IllegalArgumentException();
057        }
058    
059        public static OsmPrimitiveType from(String value) {
060            if (value == null) return null;
061            for (OsmPrimitiveType type: values()){
062                if (type.getAPIName().equalsIgnoreCase(value))
063                    return type;
064            }
065            return null;
066        }
067    
068        public static Collection<OsmPrimitiveType> dataValues() {
069            return DATA_VALUES;
070        }
071    
072        public OsmPrimitive newInstance(long uniqueId, boolean allowNegative) {
073            switch (this) {
074            case NODE:
075                return new Node(uniqueId, allowNegative);
076            case WAY:
077                return new Way(uniqueId, allowNegative);
078            case RELATION:
079                return new Relation(uniqueId, allowNegative);
080            default:
081                throw new AssertionError();
082            }
083        }
084    
085        @Override
086        public String toString() {
087            return tr(getAPIName());
088        }
089    }