1 53 54 package org.jfree.data; 55 56 import java.io.Serializable ; 57 import java.util.Collections ; 58 import java.util.Comparator ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.jfree.util.ObjectUtilities; 63 import org.jfree.util.PublicCloneable; 64 import org.jfree.util.SortOrder; 65 66 70 public class DefaultKeyedValues implements KeyedValues, 71 Cloneable , PublicCloneable, 72 Serializable { 73 74 75 private static final long serialVersionUID = 8468154364608194797L; 76 77 78 private List data; 79 80 83 public DefaultKeyedValues() { 84 this.data = new java.util.ArrayList (); 85 } 86 87 92 public int getItemCount() { 93 return this.data.size(); 94 } 95 96 105 public Number getValue(int item) { 106 Number result = null; 107 KeyedValue kval = (KeyedValue) this.data.get(item); 108 if (kval != null) { 109 result = kval.getValue(); 110 } 111 return result; 112 } 113 114 123 public Comparable getKey(int index) { 124 Comparable result = null; 125 KeyedValue item = (KeyedValue) this.data.get(index); 126 if (item != null) { 127 result = item.getKey(); 128 } 129 return result; 130 } 131 132 139 public int getIndex(Comparable key) { 140 int i = 0; 141 Iterator iterator = this.data.iterator(); 142 while (iterator.hasNext()) { 143 KeyedValue kv = (KeyedValue) iterator.next(); 144 if (kv.getKey().equals(key)) { 145 return i; 146 } 147 i++; 148 } 149 return -1; } 151 152 157 public List getKeys() { 158 List result = new java.util.ArrayList (); 159 Iterator iterator = this.data.iterator(); 160 while (iterator.hasNext()) { 161 KeyedValue kv = (KeyedValue) iterator.next(); 162 result.add(kv.getKey()); 163 } 164 return result; 165 } 166 167 176 public Number getValue(Comparable key) { 177 int index = getIndex(key); 178 if (index < 0) { 179 throw new UnknownKeyException("Key not found: " + key); 180 } 181 return getValue(index); 182 } 183 184 190 public void addValue(Comparable key, double value) { 191 addValue(key, new Double (value)); 192 } 193 194 202 public void addValue(Comparable key, Number value) { 203 setValue(key, value); 204 } 205 206 212 public void setValue(Comparable key, double value) { 213 setValue(key, new Double (value)); 214 } 215 216 222 public void setValue(Comparable key, Number value) { 223 if (key == null) { 224 throw new IllegalArgumentException ("Null 'key' argument."); 225 } 226 int keyIndex = getIndex(key); 227 if (keyIndex >= 0) { 228 DefaultKeyedValue kv = (DefaultKeyedValue) this.data.get(keyIndex); 229 kv.setValue(value); 230 } 231 else { 232 KeyedValue kv = new DefaultKeyedValue(key, value); 233 this.data.add(kv); 234 } 235 } 236 237 242 public void removeValue(int index) { 243 this.data.remove(index); 244 } 245 246 253 public void removeValue(Comparable key) { 254 int index = getIndex(key); 255 if (index >= 0) { 256 removeValue(index); 257 } 258 } 259 260 265 public void sortByKeys(SortOrder order) { 266 Comparator comparator = new KeyedValueComparator( 267 KeyedValueComparatorType.BY_KEY, order 268 ); 269 Collections.sort(this.data, comparator); 270 } 271 272 279 public void sortByValues(SortOrder order) { 280 Comparator comparator = new KeyedValueComparator( 281 KeyedValueComparatorType.BY_VALUE, order 282 ); 283 Collections.sort(this.data, comparator); 284 } 285 286 293 public boolean equals(Object obj) { 294 if (obj == this) { 295 return true; 296 } 297 298 if (!(obj instanceof KeyedValues)) { 299 return false; 300 } 301 302 KeyedValues that = (KeyedValues) obj; 303 int count = getItemCount(); 304 if (count != that.getItemCount()) { 305 return false; 306 } 307 308 for (int i = 0; i < count; i++) { 309 Comparable k1 = getKey(i); 310 Comparable k2 = that.getKey(i); 311 if (!k1.equals(k2)) { 312 return false; 313 } 314 Number v1 = getValue(i); 315 Number v2 = that.getValue(i); 316 if (v1 == null) { 317 if (v2 != null) { 318 return false; 319 } 320 } 321 else { 322 if (!v1.equals(v2)) { 323 return false; 324 } 325 } 326 } 327 return true; 328 } 329 330 335 public int hashCode() { 336 return (this.data != null ? this.data.hashCode() : 0); 337 } 338 339 347 public Object clone() throws CloneNotSupportedException { 348 DefaultKeyedValues clone = (DefaultKeyedValues) super.clone(); 349 clone.data = (List ) ObjectUtilities.deepClone(this.data); 350 return clone; 351 } 352 353 } 354 | Popular Tags |