001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import java.util.ArrayList;
005import java.util.Arrays;
006import java.util.List;
007import java.util.Locale;
008import java.util.Map;
009
010import org.openstreetmap.josm.data.coor.LatLon;
011import org.openstreetmap.josm.tools.CheckParameterUtil;
012import org.openstreetmap.josm.tools.TextTagParser;
013
014public final class OsmUtils {
015
016    private OsmUtils() {
017        // Hide default constructor for utils classes
018    }
019
020    static final List<String> TRUE_VALUES = new ArrayList<>(Arrays
021            .asList(new String[] { "true", "yes", "1", "on" }));
022    static final List<String> FALSE_VALUES = new ArrayList<>(Arrays
023            .asList(new String[] { "false", "no", "0", "off" }));
024    static final List<String> REVERSE_VALUES = new ArrayList<>(Arrays
025            .asList(new String[] { "reverse", "-1" }));
026
027    public static final String trueval = "yes";
028    public static final String falseval = "no";
029    public static final String reverseval = "-1";
030
031    public static Boolean getOsmBoolean(String value) {
032        if(value == null) return null;
033        String lowerValue = value.toLowerCase(Locale.ENGLISH);
034        if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE;
035        if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE;
036        return null;
037    }
038
039    public static String getNamedOsmBoolean(String value) {
040        Boolean res = getOsmBoolean(value);
041        return res == null ? value : (res ? trueval : falseval);
042    }
043
044    public static boolean isReversed(String value) {
045        return REVERSE_VALUES.contains(value);
046    }
047
048    public static boolean isTrue(String value) {
049        return TRUE_VALUES.contains(value);
050    }
051
052    public static boolean isFalse(String value) {
053        return FALSE_VALUES.contains(value);
054    }
055
056    /**
057     * Creates a new OSM primitive according to the given assertion. Originally written for unit tests,
058     * this can also be used in another places like validation of local MapCSS validator rules.
059     * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
060     * @return a new OSM primitive according to the given assertion
061     * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
062     * @since 7356
063     */
064    public static OsmPrimitive createPrimitive(String assertion) {
065        CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
066        final String[] x = assertion.split("\\s+", 2);
067        final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
068                ? new Node(LatLon.ZERO)
069                : "w".equals(x[0]) || "way".equals(x[0])
070                ? new Way()
071                : "r".equals(x[0]) || "relation".equals(x[0])
072                ? new Relation()
073                : null;
074        if (p == null) {
075            throw new IllegalArgumentException("Expecting n/node/w/way/r/relation, but got " + x[0]);
076        }
077        for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
078            p.put(i.getKey(), i.getValue());
079        }
080        return p;
081    }
082}