| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.tagging.ac;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.Objects;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Describes the priority of an item in an autocompletion set.
|
|---|
| 8 | * The selected flag is currently only used in plugins.
|
|---|
| 9 | *
|
|---|
| 10 | * Instances of this class are not modifiable.
|
|---|
| 11 | * @since 12859 (copied from {@code gui.tagging.ac.AutoCompletionItemPriority})
|
|---|
| 12 | */
|
|---|
| 13 | public class AutoCompletionPriority implements Comparable<AutoCompletionPriority> {
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Indicates, that the value is standard and it is found in the data.
|
|---|
| 17 | * This has higher priority than some arbitrary standard value that is
|
|---|
| 18 | * usually not used by the user.
|
|---|
| 19 | */
|
|---|
| 20 | public static final AutoCompletionPriority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionPriority(true, true, false);
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Indicates that this is an arbitrary value from the data set, i.e.
|
|---|
| 24 | * the value of a tag name=*.
|
|---|
| 25 | */
|
|---|
| 26 | public static final AutoCompletionPriority IS_IN_DATASET = new AutoCompletionPriority(true, false, false);
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Indicates that this is a standard value, i.e. a standard tag name
|
|---|
| 30 | * or a standard value for a given tag name (from the presets).
|
|---|
| 31 | */
|
|---|
| 32 | public static final AutoCompletionPriority IS_IN_STANDARD = new AutoCompletionPriority(false, true, false);
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Indicates that this is a value from a selected object.
|
|---|
| 36 | */
|
|---|
| 37 | public static final AutoCompletionPriority IS_IN_SELECTION = new AutoCompletionPriority(false, false, true);
|
|---|
| 38 |
|
|---|
| 39 | /** Unknown priority. This is the lowest priority. */
|
|---|
| 40 | public static final AutoCompletionPriority UNKNOWN = new AutoCompletionPriority(false, false, false);
|
|---|
| 41 |
|
|---|
| 42 | private static final int NO_USER_INPUT = Integer.MAX_VALUE;
|
|---|
| 43 |
|
|---|
| 44 | private final int userInput;
|
|---|
| 45 | private final boolean inDataSet;
|
|---|
| 46 | private final boolean inStandard;
|
|---|
| 47 | private final boolean selected;
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Constructs a new {@code AutoCompletionItemPriority}.
|
|---|
| 51 | *
|
|---|
| 52 | * @param inDataSet true, if the item is found in the currently active data layer
|
|---|
| 53 | * @param inStandard true, if the item is a standard tag, e.g. from the presets
|
|---|
| 54 | * @param selected true, if it is found on an object that is currently selected
|
|---|
| 55 | * @param userInput null, if the user hasn't entered this tag so far. A number when
|
|---|
| 56 | * the tag key / value has been entered by the user before. A lower number means
|
|---|
| 57 | * this happened more recently and beats a higher number in priority.
|
|---|
| 58 | */
|
|---|
| 59 | public AutoCompletionPriority(boolean inDataSet, boolean inStandard, boolean selected, Integer userInput) {
|
|---|
| 60 | this.inDataSet = inDataSet;
|
|---|
| 61 | this.inStandard = inStandard;
|
|---|
| 62 | this.selected = selected;
|
|---|
| 63 | this.userInput = userInput == null ? NO_USER_INPUT : userInput;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Constructs a new {@code AutoCompletionItemPriority}.
|
|---|
| 68 | *
|
|---|
| 69 | * @param inDataSet true, if the item is found in the currently active data layer
|
|---|
| 70 | * @param inStandard true, if the item is a standard tag, e.g. from the presets
|
|---|
| 71 | * @param selected true, if it is found on an object that is currently selected
|
|---|
| 72 | */
|
|---|
| 73 | public AutoCompletionPriority(boolean inDataSet, boolean inStandard, boolean selected) {
|
|---|
| 74 | this(inDataSet, inStandard, selected, NO_USER_INPUT);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Determines if the item is found in the currently active data layer.
|
|---|
| 79 | * @return {@code true} if the item is found in the currently active data layer
|
|---|
| 80 | */
|
|---|
| 81 | public boolean isInDataSet() {
|
|---|
| 82 | return inDataSet;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Determines if the item is a standard tag, e.g. from the presets.
|
|---|
| 87 | * @return {@code true} if the item is a standard tag, e.g. from the presets
|
|---|
| 88 | */
|
|---|
| 89 | public boolean isInStandard() {
|
|---|
| 90 | return inStandard;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Determines if it is found on an object that is currently selected.
|
|---|
| 95 | * @return {@code true} if it is found on an object that is currently selected
|
|---|
| 96 | */
|
|---|
| 97 | public boolean isSelected() {
|
|---|
| 98 | return selected;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Returns a number when the tag key / value has been entered by the user before.
|
|---|
| 103 | * A lower number means this happened more recently and beats a higher number in priority.
|
|---|
| 104 | * @return a number when the tag key / value has been entered by the user before.
|
|---|
| 105 | * {@code null}, if the user hasn't entered this tag so far.
|
|---|
| 106 | */
|
|---|
| 107 | public Integer getUserInput() {
|
|---|
| 108 | return userInput == NO_USER_INPUT ? null : userInput;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Imposes an ordering on the priorities.
|
|---|
| 113 | * Currently, being in the current DataSet is worth more than being in the Presets.
|
|---|
| 114 | */
|
|---|
| 115 | @Override
|
|---|
| 116 | public int compareTo(AutoCompletionPriority other) {
|
|---|
| 117 | int ui = Integer.compare(other.userInput, userInput);
|
|---|
| 118 | if (ui != 0)
|
|---|
| 119 | return ui;
|
|---|
| 120 |
|
|---|
| 121 | int sel = Boolean.compare(selected, other.selected);
|
|---|
| 122 | if (sel != 0)
|
|---|
| 123 | return sel;
|
|---|
| 124 |
|
|---|
| 125 | int ds = Boolean.compare(inDataSet, other.inDataSet);
|
|---|
| 126 | if (ds != 0)
|
|---|
| 127 | return ds;
|
|---|
| 128 |
|
|---|
| 129 | int std = Boolean.compare(inStandard, other.inStandard);
|
|---|
| 130 | if (std != 0)
|
|---|
| 131 | return std;
|
|---|
| 132 |
|
|---|
| 133 | return 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Merges two priorities.
|
|---|
| 138 | * The resulting priority is always >= the original ones.
|
|---|
| 139 | * @param other other priority
|
|---|
| 140 | * @return the merged priority
|
|---|
| 141 | */
|
|---|
| 142 | public AutoCompletionPriority mergeWith(AutoCompletionPriority other) {
|
|---|
| 143 | return new AutoCompletionPriority(
|
|---|
| 144 | inDataSet || other.inDataSet,
|
|---|
| 145 | inStandard || other.inStandard,
|
|---|
| 146 | selected || other.selected,
|
|---|
| 147 | Math.min(userInput, other.userInput));
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | @Override
|
|---|
| 151 | public int hashCode() {
|
|---|
| 152 | return Objects.hash(inDataSet, inStandard, selected, userInput);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | @Override
|
|---|
| 156 | public boolean equals(Object obj) {
|
|---|
| 157 | if (this == obj)
|
|---|
| 158 | return true;
|
|---|
| 159 | if (obj == null || getClass() != obj.getClass())
|
|---|
| 160 | return false;
|
|---|
| 161 | AutoCompletionPriority other = (AutoCompletionPriority) obj;
|
|---|
| 162 | return inDataSet == other.inDataSet &&
|
|---|
| 163 | inStandard == other.inStandard &&
|
|---|
| 164 | selected == other.selected &&
|
|---|
| 165 | userInput == other.userInput;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | @Override
|
|---|
| 169 | public String toString() {
|
|---|
| 170 | return String.format("<Priority; userInput: %s, inDataSet: %b, inStandard: %b, selected: %b>",
|
|---|
| 171 | userInput == NO_USER_INPUT ? "no" : Integer.toString(userInput), inDataSet, inStandard, selected);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|