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