1 42 43 package org.jfree.data; 44 45 import java.io.Serializable ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import org.jfree.util.PublicCloneable; 50 51 54 public class KeyedObjects implements Cloneable , PublicCloneable, Serializable { 55 56 57 private static final long serialVersionUID = 1321582394193530984L; 58 59 60 private List data; 61 62 65 public KeyedObjects() { 66 this.data = new java.util.ArrayList (); 67 } 68 69 74 public int getItemCount() { 75 return this.data.size(); 76 } 77 78 85 public Object getObject(int item) { 86 Object result = null; 87 if (item >= 0 && item < this.data.size()) { 88 KeyedObject kobj = (KeyedObject) this.data.get(item); 89 if (kobj != null) { 90 result = kobj.getObject(); 91 } 92 } 93 return result; 94 } 95 96 105 public Comparable getKey(int index) { 106 Comparable result = null; 107 if (index >= 0 && index < this.data.size()) { 108 KeyedObject item = (KeyedObject) this.data.get(index); 109 if (item != null) { 110 result = item.getKey(); 111 } 112 } 113 return result; 114 } 115 116 123 public int getIndex(Comparable key) { 124 int result = -1; 125 int i = 0; 126 Iterator iterator = this.data.iterator(); 127 while (iterator.hasNext()) { 128 KeyedObject ko = (KeyedObject) iterator.next(); 129 if (ko.getKey().equals(key)) { 130 result = i; 131 } 132 i++; 133 } 134 return result; 135 } 136 137 142 public List getKeys() { 143 List result = new java.util.ArrayList (); 144 Iterator iterator = this.data.iterator(); 145 while (iterator.hasNext()) { 146 KeyedObject ko = (KeyedObject) iterator.next(); 147 result.add(ko.getKey()); 148 } 149 return result; 150 } 151 152 160 public Object getObject(Comparable key) { 161 return getObject(getIndex(key)); 162 } 163 164 171 public void addObject(Comparable key, Object object) { 172 setObject(key, object); 173 } 174 175 183 public void setObject(Comparable key, Object object) { 184 int keyIndex = getIndex(key); 185 if (keyIndex >= 0) { 186 KeyedObject ko = (KeyedObject) this.data.get(keyIndex); 187 ko.setObject(object); 188 } 189 else { 190 KeyedObject ko = new KeyedObject(key, object); 191 this.data.add(ko); 192 } 193 } 194 195 200 public void removeValue(int index) { 201 this.data.remove(index); 202 } 203 204 209 public void removeValue(Comparable key) { 210 removeValue(getIndex(key)); 211 } 212 213 220 public Object clone() throws CloneNotSupportedException { 221 KeyedObjects clone = (KeyedObjects) super.clone(); 222 clone.data = new java.util.ArrayList (); 223 Iterator iterator = this.data.iterator(); 224 while (iterator.hasNext()) { 225 KeyedObject ko = (KeyedObject) iterator.next(); 226 clone.data.add(ko.clone()); 227 } 228 return clone; 229 } 230 231 238 public boolean equals(Object o) { 239 240 if (o == null) { 241 return false; 242 } 243 if (o == this) { 244 return true; 245 } 246 247 if (!(o instanceof KeyedObjects)) { 248 return false; 249 } 250 251 KeyedObjects kos = (KeyedObjects) o; 252 int count = getItemCount(); 253 if (count != kos.getItemCount()) { 254 return false; 255 } 256 257 for (int i = 0; i < count; i++) { 258 Comparable k1 = getKey(i); 259 Comparable k2 = kos.getKey(i); 260 if (!k1.equals(k2)) { 261 return false; 262 } 263 Object o1 = getObject(i); 264 Object o2 = kos.getObject(i); 265 if (o1 == null) { 266 if (o2 != null) { 267 return false; 268 } 269 } 270 else { 271 if (!o1.equals(o2)) { 272 return false; 273 } 274 } 275 } 276 return true; 277 278 } 279 280 } 281 | Popular Tags |