001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.ac;
003
004import org.openstreetmap.josm.tools.CheckParameterUtil;
005
006/**
007 * Represents an entry in the list of auto completion values.
008 *
009 *  An AutoCompletionListItem 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 list 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 AutoCompletionItemPriority}.
015 *
016 * The value is a string which will be displayed in the auto completion list.
017 *
018 */
019public class AutoCompletionListItem implements Comparable<AutoCompletionListItem> {
020
021    /** the pritority of this item */
022    private  AutoCompletionItemPriority priority;
023    /** the value of this item */
024    private String value;
025
026    /**
027     * Constructs a new {@code AutoCompletionListItem} with the given value and priority.
028     * @param value The value
029     * @param priority The priority
030     */
031    public AutoCompletionListItem(String value, AutoCompletionItemPriority priority) {
032        this.value = value;
033        this.priority = priority;
034    }
035
036    /**
037     * Constructs a new {@code AutoCompletionListItem} with the given value and unknown priority.
038     * @param value The value
039     */
040    public AutoCompletionListItem(String value) {
041        this.value = value;
042        priority = AutoCompletionItemPriority.UNKNOWN;
043    }
044
045    /**
046     * Constructs a new {@code AutoCompletionListItem}.
047     */
048    public AutoCompletionListItem() {
049        value = "";
050        priority = AutoCompletionItemPriority.UNKNOWN;
051    }
052
053    /**
054     * Returns the priority.
055     * @return the priority
056     */
057    public AutoCompletionItemPriority getPriority() {
058        return priority;
059    }
060
061    /**
062     * Sets the priority.
063     * @param priority  the priority
064     */
065    public void setPriority(AutoCompletionItemPriority 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     * sets the value
079     * @param value the value; must not be null
080     * @throws IllegalArgumentException if value if null
081     */
082    public void setValue(String value) {
083        CheckParameterUtil.ensureParameterNotNull(value, "value");
084        this.value = value;
085    }
086
087    @Override
088    public String toString() {
089        StringBuilder sb = new StringBuilder();
090        sb.append("<val='")
091          .append(value)
092          .append("',")
093          .append(priority)
094          .append('>');
095        return sb.toString();
096    }
097
098    @Override
099    public int hashCode() {
100        final int prime = 31;
101        int result = 1;
102        result = prime * result
103                + ((priority == null) ? 0 : priority.hashCode());
104        result = prime * result + ((value == null) ? 0 : value.hashCode());
105        return result;
106    }
107
108    @Override
109    public boolean equals(Object obj) {
110        if (this == obj)
111            return true;
112        if (obj == null)
113            return false;
114        if (obj instanceof String)
115            return obj.equals(value);
116        if (getClass() != obj.getClass())
117            return false;
118        final AutoCompletionListItem other = (AutoCompletionListItem) obj;
119        if (priority == null) {
120            if (other.priority != null)
121                return false;
122        } else if (!priority.equals(other.priority))
123            return false;
124        if (value == null) {
125            if (other.value != null)
126                return false;
127        } else if (!value.equals(other.value))
128            return false;
129        return true;
130    }
131
132    @Override
133    public int compareTo(AutoCompletionListItem other) {
134        int ret = other.priority.compareTo(priority); // higher priority items come first in the list
135        if (ret != 0)
136            return ret;
137        else
138            return this.value.compareTo(other.value);
139    }
140}