001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.gui.tagging.ac;
003    
004    /**
005     * Describes the priority of an item in an autocompletion list.
006     * The selected flag is currently only used in plugins.
007     *
008     * Instances of this class are not modifiable.
009     */
010    public class AutoCompletionItemPritority implements Comparable<AutoCompletionItemPritority> {
011    
012        /**
013         * Indicates, that the value is standard and it is found in the data.
014         * This has higher priority than some arbitrary standard value that is
015         * usually not used by the user.
016         */
017        public static final AutoCompletionItemPritority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPritority(true, true, false);
018    
019        /**
020         * Indicates that this is an arbitrary value from the data set, i.e.
021         * the value of a tag name=*.
022         */
023        public static final AutoCompletionItemPritority IS_IN_DATASET = new AutoCompletionItemPritority(true, false, false);
024    
025        /**
026         * Indicates that this is a standard value, i.e. a standard tag name
027         * or a standard value for a given tag name (from the presets).
028         */
029        public static final AutoCompletionItemPritority IS_IN_STANDARD = new AutoCompletionItemPritority(false, true, false);
030    
031        /**
032         * Indicates that this is a value from a selected object.
033         */
034        public static final AutoCompletionItemPritority  IS_IN_SELECTION  = new AutoCompletionItemPritority(false, false, true);
035    
036        /** Unknown priority. This is the lowest priority. */
037        public static final AutoCompletionItemPritority UNKNOWN = new AutoCompletionItemPritority(false, false, false);
038    
039        private final boolean inDataSet;
040        private final boolean inStandard;
041        private final boolean selected;
042    
043        public AutoCompletionItemPritority(boolean inDataSet, boolean inStandard, boolean selected) {
044            this.inDataSet = inDataSet;
045            this.inStandard = inStandard;
046            this.selected = selected;
047        }
048    
049        public boolean isInDataSet() {
050            return inDataSet;
051        }
052    
053        public boolean isInStandard() {
054            return inStandard;
055        }
056    
057        public boolean isSelected() {
058            return selected;
059        }
060    
061        /**
062         * Imposes an ordering on the priorities.
063         * Currently, being in the current DataSet is worth more than being in the Presets.
064         */
065        public int compareTo(AutoCompletionItemPritority other) {
066            int sel = Boolean.valueOf(selected).compareTo(other.selected);
067            if (sel != 0) return sel;
068    
069            int ds = Boolean.valueOf(inDataSet).compareTo(other.inDataSet);
070            if (ds != 0) return ds;
071    
072            int std = Boolean.valueOf(inStandard).compareTo(other.inStandard);
073            if (std != 0) return std;
074    
075            return 0;
076        }
077    
078        /**
079         * Merges two priorities.
080         * The resulting priority is always >= the original ones.
081         */
082        public AutoCompletionItemPritority mergeWith(AutoCompletionItemPritority other) {
083            return new AutoCompletionItemPritority(
084                    inDataSet || other.inDataSet,
085                    inStandard || other.inStandard,
086                    selected || other.selected);
087        }
088    
089        @Override public String toString() {
090            return String.format("<Priority; inDataSet: %b, inStandard: %b, selected: %b>", inDataSet, inStandard, selected);
091        }
092    }