1 47 48 package org.jfree.util; 49 50 import java.io.Serializable ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import java.util.NoSuchElementException ; 56 import java.util.Set ; 57 58 65 public class HashNMap implements Serializable , Cloneable { 66 67 68 private static final long serialVersionUID = -670924844536074826L; 69 70 74 private static final class EmptyIterator implements Iterator { 75 76 79 private EmptyIterator() { 80 super(); 81 } 82 83 90 public boolean hasNext() { 91 return false; 92 } 93 94 100 public Object next() { 101 throw new NoSuchElementException ("This iterator is empty."); 102 } 103 104 118 public void remove() { 119 throw new UnsupportedOperationException ("This iterator is empty, no remove supported."); 120 } 121 } 122 123 127 private static final Iterator EMPTY_ITERATOR = new EmptyIterator(); 128 129 132 private HashMap table; 133 134 137 private static final Object [] EMPTY_ARRAY = new Object [0]; 138 139 142 public HashNMap() { 143 this.table = new HashMap (); 144 } 145 146 151 protected List createList() { 152 return new ArrayList (); 153 } 154 155 163 public boolean put(final Object key, final Object val) { 164 final List v = (List ) this.table.get(key); 165 if (v == null) { 166 final List newList = createList(); 167 newList.add(val); 168 this.table.put(key, newList); 169 return true; 170 } 171 else { 172 v.clear(); 173 return v.add(val); 174 } 175 } 176 177 186 public boolean add(final Object key, final Object val) { 187 final List v = (List ) this.table.get(key); 188 if (v == null) { 189 put(key, val); 190 return true; 191 } 192 else { 193 return v.add(val); 194 } 195 } 196 197 204 public Object getFirst(final Object key) { 205 return get(key, 0); 206 } 207 208 217 public Object get(final Object key, final int n) { 218 final List v = (List ) this.table.get(key); 219 if (v == null) { 220 return null; 221 } 222 return v.get(n); 223 } 224 225 231 public Iterator getAll(final Object key) { 232 final List v = (List ) this.table.get(key); 233 if (v == null) { 234 return EMPTY_ITERATOR; 235 } 236 return v.iterator(); 237 } 238 239 244 public Iterator keys() { 245 return this.table.keySet().iterator(); 246 } 247 248 253 public Set keySet() { 254 return this.table.keySet(); 255 } 256 257 265 public boolean remove(final Object key, final Object value) { 266 final List v = (List ) this.table.get(key); 267 if (v == null) { 268 return false; 269 } 270 271 if (!v.remove(value)) { 272 return false; 273 } 274 if (v.size() == 0) { 275 this.table.remove(key); 276 } 277 return true; 278 } 279 280 285 public void removeAll(final Object key) { 286 this.table.remove(key); 287 } 288 289 292 public void clear() { 293 this.table.clear(); 294 } 295 296 302 public boolean containsKey(final Object key) { 303 return this.table.containsKey(key); 304 } 305 306 312 public boolean containsValue(final Object value) { 313 final Iterator e = this.table.values().iterator(); 314 boolean found = false; 315 while (e.hasNext() && !found) { 316 final List v = (List ) e.next(); 317 found = v.contains(value); 318 } 319 return found; 320 } 321 322 329 public boolean containsValue(final Object key, final Object value) { 330 final List v = (List ) this.table.get(key); 331 if (v == null) { 332 return false; 333 } 334 return v.contains(value); 335 } 336 337 343 public boolean contains(final Object value) { 344 if (containsKey(value)) { 345 return true; 346 } 347 return containsValue(value); 348 } 349 350 356 public Object clone() throws CloneNotSupportedException { 357 final HashNMap map = (HashNMap) super.clone(); 358 map.table = new HashMap (); 359 final Iterator iterator = keys(); 360 while (iterator.hasNext()) { 361 final Object key = iterator.next(); 362 final List list = (List ) map.table.get(key); 363 if (list != null) { 364 map.table.put(key, ObjectUtilities.clone(list)); 365 } 366 } 367 return map; 368 } 369 370 378 public Object [] toArray(final Object key, final Object [] data) { 379 if (key == null) { 380 throw new NullPointerException ("Key must not be null."); 381 } 382 final List list = (List ) this.table.get(key); 383 if (list != null) { 384 return list.toArray(data); 385 } 386 if (data.length > 0) { 387 data[0] = null; 388 } 389 return data; 390 } 391 392 399 public Object [] toArray(final Object key) { 400 if (key == null) { 401 throw new NullPointerException ("Key must not be null."); 402 } 403 final List list = (List ) this.table.get(key); 404 if (list != null) { 405 return list.toArray(); 406 } 407 return EMPTY_ARRAY; 408 } 409 410 417 public int getValueCount(final Object key) { 418 if (key == null) { 419 throw new NullPointerException ("Key must not be null."); 420 } 421 final List list = (List ) this.table.get(key); 422 if (list != null) { 423 return list.size(); 424 } 425 return 0; 426 } 427 } 428 | Popular Tags |