001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.tagging.ac;
003
004import java.util.Objects;
005
006/**
007 * Represents an entry in the set of auto completion values.
008 *
009 *  An AutoCompletionItem has a <em>priority</em> and a <em>value</em>.
010 *
011 *  The priority helps to sort the auto completion items according to their importance. For instance,
012 *  in an auto completion set for tag names, standard tag names would be assigned a higher
013 *  priority than arbitrary tag names present in the current data set. There are three priority levels,
014 *  {@link AutoCompletionPriority}.
015 *
016 * The value is a string which will be displayed in the auto completion list.
017 * @since 12859 (copied from {@code gui.tagging.ac.AutoCompletionListItem})
018 */
019public class AutoCompletionItem implements Comparable<AutoCompletionItem> {
020
021    /** the priority of this item */
022    private AutoCompletionPriority priority;
023    /** the value of this item */
024    private final String value;
025
026    /**
027     * Constructs a new {@code AutoCompletionItem} with the given value and priority.
028     * @param value The value
029     * @param priority The priority
030     */
031    public AutoCompletionItem(String value, AutoCompletionPriority priority) {
032        this.value = value;
033        this.priority = priority;
034    }
035
036    /**
037     * Constructs a new {@code AutoCompletionItem} with the given value and unknown priority.
038     * @param value The value
039     */
040    public AutoCompletionItem(String value) {
041        this.value = value;
042        priority = AutoCompletionPriority.UNKNOWN;
043    }
044
045    /**
046     * Constructs a new {@code AutoCompletionItem}.
047     */
048    public AutoCompletionItem() {
049        value = "";
050        priority = AutoCompletionPriority.UNKNOWN;
051    }
052
053    /**
054     * Returns the priority.
055     * @return the priority
056     */
057    public AutoCompletionPriority getPriority() {
058        return priority;
059    }
060
061    /**
062     * Sets the priority.
063     * @param priority  the priority
064     */
065    public void setPriority(AutoCompletionPriority priority) {
066        this.priority = priority;
067    }
068
069    /**
070     * Returns the value.
071     * @return the value
072     */
073    public String getValue() {
074        return value;
075    }
076
077    /**
078     * Here we return the value instead of a representation of the inner object state because both
079     * {@link javax.swing.plaf.basic.BasicComboBoxEditor#setItem(Object)} and
080     * {@link javax.swing.DefaultListCellRenderer#getListCellRendererComponent}
081     * expect it, thus making derived Editor and CellRenderer classes superfluous.
082     */
083    @Override
084    public String toString() {
085        return value;
086    }
087
088    @Override
089    public int hashCode() {
090        return Objects.hash(priority, value);
091    }
092
093    @Override
094    public boolean equals(Object obj) {
095        if (this == obj)
096            return true;
097        if (!(obj instanceof AutoCompletionItem))
098            return false;
099        final AutoCompletionItem other = (AutoCompletionItem) obj;
100        if (value == null ? other.value != null : !value.equals(other.value))
101            return false;
102        return priority == null ? other.priority == null : priority.equals(other.priority);
103    }
104
105    @Override
106    public int compareTo(AutoCompletionItem other) {
107        // sort on priority descending
108        int ret = other.priority.compareTo(priority);
109        // then alphabetic ascending
110        return ret != 0 ? ret : this.value.compareTo(other.value);
111    }
112}