001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import java.util.Set;
005import java.util.TreeSet;
006
007/**
008 * A simple class to keep helper functions for merging TIGER data
009 *
010 * @author daveh
011 * @since 529
012 */
013public final class TigerUtils {
014
015    private TigerUtils() {
016        // Hide default constructor for utils classes
017    }
018
019    /**
020     * Determines if the given tag is a TIGER one
021     * @param tag The tag to check
022     * @return {@code true} if {@code tag} starts with {@code tiger:} namespace
023     */
024    public static boolean isTigerTag(String tag) {
025        if (tag.indexOf("tiger:") == -1)
026            return false;
027        return true;
028    }
029
030    /**
031     * Determines if the given key denotes an integer value.
032     * @param name The key to determine
033     * @return {@code true} if the given key denotes an integer value
034     */
035    public static boolean tagIsInt(String name) {
036        if ("tiger:tlid".equals(name))
037            return true;
038        return false;
039    }
040
041    public static Object tagObj(String name) {
042        if (tagIsInt(name))
043            return Integer.valueOf(name);
044        return name;
045    }
046
047    public static String combineTags(Set<String> values) {
048        Set<Object> resultSet = new TreeSet<>();
049        for (String value: values) {
050            String[] parts = value.split(":");
051            for (String part: parts) {
052               resultSet.add(tagObj(part));
053            }
054            // Do not produce useless changeset noise if a single value is used and does not contain redundant splitted parts (fix #7405)
055            if (values.size() == 1 && resultSet.size() == parts.length) {
056                return value;
057            }
058        }
059        StringBuilder combined = new StringBuilder();
060        for (Object part : resultSet) {
061            if (combined.length() > 0) {
062                combined.append(':');
063            }
064            combined.append(part);
065        }
066        return combined.toString();
067    }
068}