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