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