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