001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.conflict.tags;
003    
004    import java.util.Collection;
005    
006    import org.openstreetmap.josm.data.osm.OsmPrimitive;
007    import org.openstreetmap.josm.data.osm.Tag;
008    import org.openstreetmap.josm.data.osm.TagCollection;
009    import org.openstreetmap.josm.data.osm.TigerUtils;
010    
011    /**
012     * Collection of utility methods for tag conflict resolution
013     *
014     */
015    public 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            tc.removeByKey("created_by");
036    
037            int numNodesWithTags = 0;
038            for (OsmPrimitive p: merged) {
039                if (p.getKeys().size() >0) {
040                    numNodesWithTags++;
041                }
042            }
043            if (numNodesWithTags <= 1)
044                return;
045    
046            for (String key: tc.getKeys()) {
047                // make sure the empty value is in the tag set if a tag is not present
048                // on all merged nodes
049                //
050                for (OsmPrimitive p: merged) {
051                    if (p.get(key) == null) {
052                        tc.add(new Tag(key, "")); // add a tag with key and empty value
053                    }
054                }
055            }
056        }
057    
058        /**
059         * Combines tags from TIGER data
060         *
061         * @param tc the tag collection
062         */
063        public static void combineTigerTags(TagCollection tc) {
064            for (String key: tc.getKeys()) {
065                if (TigerUtils.isTigerTag(key)) {
066                    tc.setUniqueForKey(key, TigerUtils.combineTags(key, tc.getValues(key)));
067                }
068            }
069        }
070    
071        /**
072         * Completes tags in the tag collection <code>tc</code> with the empty value
073         * for each tag. If the empty value is present the tag conflict resolution dialog
074         * will offer an option for removing the tag and not only options for selecting
075         * one of the current values of the tag.
076         *
077         * @param tc the tag collection
078         */
079        public static void completeTagCollectionForEditing(TagCollection tc) {
080            for (String key: tc.getKeys()) {
081                // make sure the empty value is in the tag set such that we can delete the tag
082                // in the conflict dialog if necessary
083                //
084                tc.add(new Tag(key,""));
085            }
086        }
087    }