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    public static boolean tagIsInt(String name) {
031        if (name.equals("tiger:tlid"))
032            return true;
033        return false;
034    }
035
036    public static Object tagObj(String name) {
037        if (tagIsInt(name))
038            return Integer.valueOf(name);
039        return name;
040    }
041
042    public static String combineTags(String name, Set<String> values) {
043        TreeSet<Object> resultSet = new TreeSet<Object>();
044        for (String value: values) {
045            String[] parts = value.split(":");
046            for (String part: parts) {
047               resultSet.add(tagObj(part));
048            }
049            // Do not produce useless changeset noise if a single value is used and does not contain redundant splitted parts (fix #7405)
050            if (values.size() == 1 && resultSet.size() == parts.length) {
051                return value;
052            }
053        }
054        StringBuilder combined = new StringBuilder();
055        for (Object part : resultSet) {
056            if (combined.length() > 0) {
057                combined.append(":");
058            }
059            combined.append(part);
060        }
061        return combined.toString();
062    }
063
064    public static String combineTags(String name, String t1, String t2) {
065        Set<String> set = new TreeSet<String>();
066        set.add(t1);
067        set.add(t2);
068        return TigerUtils.combineTags(name, set);
069    }
070}