001// License: GPL. For details, see LICENSE file.
002package 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 */
010public class AutoCompletionItemPriority implements Comparable<AutoCompletionItemPriority> {
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 AutoCompletionItemPriority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPriority(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 AutoCompletionItemPriority IS_IN_DATASET = new AutoCompletionItemPriority(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 AutoCompletionItemPriority IS_IN_STANDARD = new AutoCompletionItemPriority(false, true, false);
030
031    /**
032     * Indicates that this is a value from a selected object.
033     */
034    public static final AutoCompletionItemPriority  IS_IN_SELECTION  = new AutoCompletionItemPriority(false, false, true);
035
036    /** Unknown priority. This is the lowest priority. */
037    public static final AutoCompletionItemPriority UNKNOWN = new AutoCompletionItemPriority(false, false, false);
038
039    private final boolean inDataSet;
040    private final boolean inStandard;
041    private final boolean selected;
042
043    public AutoCompletionItemPriority(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    @Override
066    public int compareTo(AutoCompletionItemPriority other) {
067        int sel = Boolean.valueOf(selected).compareTo(other.selected);
068        if (sel != 0) return sel;
069
070        int ds = Boolean.valueOf(inDataSet).compareTo(other.inDataSet);
071        if (ds != 0) return ds;
072
073        int std = Boolean.valueOf(inStandard).compareTo(other.inStandard);
074        if (std != 0) return std;
075
076        return 0;
077    }
078
079    /**
080     * Merges two priorities.
081     * The resulting priority is always &gt;= the original ones.
082     */
083    public AutoCompletionItemPriority mergeWith(AutoCompletionItemPriority other) {
084        return new AutoCompletionItemPriority(
085                inDataSet || other.inDataSet,
086                inStandard || other.inStandard,
087                selected || other.selected);
088    }
089
090    @Override public String toString() {
091        return String.format("<Priority; inDataSet: %b, inStandard: %b, selected: %b>", inDataSet, inStandard, selected);
092    }
093}