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 * @exception IllegalArgumentException thrown, if value if null 081 */ 082 public void setValue(String value) { 083 CheckParameterUtil.ensureParameterNotNull(value, "value"); 084 this.value = value; 085 } 086 087 @Override public String toString() { 088 StringBuilder sb = new StringBuilder(); 089 sb.append("<val='"); 090 sb.append(value); 091 sb.append("',"); 092 sb.append(priority.toString()); 093 sb.append(">"); 094 return sb.toString(); 095 } 096 097 @Override public int hashCode() { 098 final int prime = 31; 099 int result = 1; 100 result = prime * result 101 + ((priority == null) ? 0 : priority.hashCode()); 102 result = prime * result + ((value == null) ? 0 : value.hashCode()); 103 return result; 104 } 105 106 @Override public boolean equals(Object obj) { 107 if (this == obj) 108 return true; 109 if (obj == null) 110 return false; 111 if (obj instanceof String) 112 return obj.equals(value); 113 if (getClass() != obj.getClass()) 114 return false; 115 final AutoCompletionListItem other = (AutoCompletionListItem)obj; 116 if (priority == null) { 117 if (other.priority != null) 118 return false; 119 } else if (!priority.equals(other.priority)) 120 return false; 121 if (value == null) { 122 if (other.value != null) 123 return false; 124 } else if (!value.equals(other.value)) 125 return false; 126 return true; 127 } 128 129 @Override 130 public int compareTo(AutoCompletionListItem other) { 131 int ret = other.priority.compareTo(priority); // higher priority items come first in the list 132 if (ret != 0) 133 return ret; 134 else 135 return this.value.compareTo(other.value); 136 } 137}