001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.tagging.ac;
003
004/**
005 * Represents an entry in the set of auto completion values.
006 *
007 *  An AutoCompletionItem has a <em>priority</em> and a <em>value</em>.
008 *
009 *  The priority helps to sort the auto completion items according to their importance. For instance,
010 *  in an auto completion set for tag names, standard tag names would be assigned a higher
011 *  priority than arbitrary tag names present in the current data set. There are three priority levels,
012 *  {@link AutoCompletionPriority}.
013 *
014 * The value is a string which will be displayed in the auto completion list.
015 * @since 12859 (copied from {@code gui.tagging.ac.AutoCompletionListItem})
016 */
017public class AutoCompletionItem implements Comparable<AutoCompletionItem> {
018
019    /** the priority of this item */
020    private AutoCompletionPriority priority;
021    /** the value of this item */
022    private final String value;
023
024    /**
025     * Constructs a new {@code AutoCompletionItem} with the given value and priority.
026     * @param value The value
027     * @param priority The priority
028     */
029    public AutoCompletionItem(String value, AutoCompletionPriority priority) {
030        this.value = value;
031        this.priority = priority;
032    }
033
034    /**
035     * Constructs a new {@code AutoCompletionItem} with the given value and unknown priority.
036     * @param value The value
037     */
038    public AutoCompletionItem(String value) {
039        this.value = value;
040        priority = AutoCompletionPriority.UNKNOWN;
041    }
042
043    /**
044     * Constructs a new {@code AutoCompletionItem}.
045     */
046    public AutoCompletionItem() {
047        value = "";
048        priority = AutoCompletionPriority.UNKNOWN;
049    }
050
051    /**
052     * Returns the priority.
053     * @return the priority
054     */
055    public AutoCompletionPriority getPriority() {
056        return priority;
057    }
058
059    /**
060     * Sets the priority.
061     * @param priority  the priority
062     */
063    public void setPriority(AutoCompletionPriority priority) {
064        this.priority = priority;
065    }
066
067    /**
068     * Returns the value.
069     * @return the value
070     */
071    public String getValue() {
072        return value;
073    }
074
075    @Override
076    public String toString() {
077        StringBuilder sb = new StringBuilder();
078        sb.append("<val='")
079          .append(value)
080          .append("',")
081          .append(priority)
082          .append('>');
083        return sb.toString();
084    }
085
086    @Override
087    public int hashCode() {
088        final int prime = 31;
089        int result = 1;
090        result = prime * result
091                + ((priority == null) ? 0 : priority.hashCode());
092        result = prime * result + ((value == null) ? 0 : value.hashCode());
093        return result;
094    }
095
096    @Override
097    public boolean equals(Object obj) {
098        if (this == obj)
099            return true;
100        if (obj == null)
101            return false;
102        if (obj instanceof String)
103            return obj.equals(value);
104        if (getClass() != obj.getClass())
105            return false;
106        final AutoCompletionItem other = (AutoCompletionItem) obj;
107        if (priority == null) {
108            if (other.priority != null)
109                return false;
110        } else if (!priority.equals(other.priority))
111            return false;
112        if (value == null) {
113            if (other.value != null)
114                return false;
115        } else if (!value.equals(other.value))
116            return false;
117        return true;
118    }
119
120    @Override
121    public int compareTo(AutoCompletionItem other) {
122        int ret = other.priority.compareTo(priority); // higher priority items come first in the list
123        if (ret != 0)
124            return ret;
125        else
126            return this.value.compareTo(other.value);
127    }
128}