001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.tags;
003
004import java.util.Collection;
005
006import org.openstreetmap.josm.data.osm.OsmPrimitive;
007import org.openstreetmap.josm.data.osm.Tag;
008import org.openstreetmap.josm.data.osm.TagCollection;
009import org.openstreetmap.josm.data.osm.TigerUtils;
010
011/**
012 * Collection of utility methods for tag conflict resolution
013 *
014 */
015public final class TagConflictResolutionUtil {
016
017    /** no constructor, just static utility methods */
018    private TagConflictResolutionUtil() {}
019
020    /**
021     * Normalizes the tags in the tag collection <code>tc</code> before resolving tag conflicts.
022     *
023     *  Removes irrelevant tags like "created_by".
024     *
025     *  For tags which are not present on at least one of the merged nodes, the empty value ""
026     *  is added to the list of values for this tag, but only if there are at least two
027     *  primitives with tags.
028     *
029     * @param tc the tag collection
030     * @param merged the collection of merged  primitives
031     */
032    public static void normalizeTagCollectionBeforeEditing(TagCollection tc, Collection<? extends OsmPrimitive> merged) {
033        // remove irrelevant tags
034        //
035        for(String key : OsmPrimitive.getDiscardableKeys()) {
036            tc.removeByKey(key);
037        }
038
039        int numNodesWithTags = 0;
040        for (OsmPrimitive p: merged) {
041            if (!p.getKeys().isEmpty()) {
042                numNodesWithTags++;
043            }
044        }
045        if (numNodesWithTags <= 1)
046            return;
047
048        for (String key: tc.getKeys()) {
049            // make sure the empty value is in the tag set if a tag is not present
050            // on all merged nodes
051            //
052            for (OsmPrimitive p: merged) {
053                if (p.get(key) == null) {
054                    tc.add(new Tag(key, "")); // add a tag with key and empty value
055                }
056            }
057        }
058    }
059
060    /**
061     * Combines tags from TIGER data
062     *
063     * @param tc the tag collection
064     */
065    public static void combineTigerTags(TagCollection tc) {
066        for (String key: tc.getKeys()) {
067            if (TigerUtils.isTigerTag(key)) {
068                tc.setUniqueForKey(key, TigerUtils.combineTags(key, tc.getValues(key)));
069            }
070        }
071    }
072
073    /**
074     * Completes tags in the tag collection <code>tc</code> with the empty value
075     * for each tag. If the empty value is present the tag conflict resolution dialog
076     * will offer an option for removing the tag and not only options for selecting
077     * one of the current values of the tag.
078     *
079     * @param tc the tag collection
080     */
081    public static void completeTagCollectionForEditing(TagCollection tc) {
082        for (String key: tc.getKeys()) {
083            // make sure the empty value is in the tag set such that we can delete the tag
084            // in the conflict dialog if necessary
085            //
086            tc.add(new Tag(key,""));
087        }
088    }
089}