1 16 package org.apache.commons.collections; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.util.AbstractCollection ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.NoSuchElementException ; 27 import java.util.Set ; 28 29 import org.apache.commons.collections.iterators.EmptyIterator; 30 31 64 public class MultiHashMap extends HashMap implements MultiMap { 65 66 private transient Collection values = null; 68 69 private static final long serialVersionUID = 1943563828307035349L; 71 72 75 public MultiHashMap() { 76 super(); 77 } 78 79 84 public MultiHashMap(int initialCapacity) { 85 super(initialCapacity); 86 } 87 88 94 public MultiHashMap(int initialCapacity, float loadFactor) { 95 super(initialCapacity, loadFactor); 96 } 97 98 111 public MultiHashMap(Map mapToCopy) { 112 super((int) (mapToCopy.size() * 1.4f)); 114 if (mapToCopy instanceof MultiMap) { 115 for (Iterator it = mapToCopy.entrySet().iterator(); it.hasNext();) { 116 Map.Entry entry = (Map.Entry ) it.next(); 117 Collection coll = (Collection ) entry.getValue(); 118 Collection newColl = createCollection(coll); 119 super.put(entry.getKey(), newColl); 120 } 121 } else { 122 putAll(mapToCopy); 123 } 124 } 125 126 129 private void readObject(ObjectInputStream s) throws IOException , ClassNotFoundException { 130 133 s.defaultReadObject(); 135 136 String version = "1.2"; 138 try { 139 version = System.getProperty("java.version"); 140 } catch (SecurityException ex) { 141 } 143 144 if (version.startsWith("1.2") || version.startsWith("1.3")) { 145 for (Iterator iterator = entrySet().iterator(); iterator.hasNext();) { 146 Map.Entry entry = (Map.Entry ) iterator.next(); 147 super.put(entry.getKey(), ((Collection ) entry.getValue()).iterator().next()); 149 } 150 } 151 } 152 153 160 public int totalSize() { 161 int total = 0; 162 Collection values = super.values(); 163 for (Iterator it = values.iterator(); it.hasNext();) { 164 Collection coll = (Collection ) it.next(); 165 total += coll.size(); 166 } 167 return total; 168 } 169 170 178 public Collection getCollection(Object key) { 179 return (Collection ) get(key); 180 } 181 182 189 public int size(Object key) { 190 Collection coll = getCollection(key); 191 if (coll == null) { 192 return 0; 193 } 194 return coll.size(); 195 } 196 197 204 public Iterator iterator(Object key) { 205 Collection coll = getCollection(key); 206 if (coll == null) { 207 return EmptyIterator.INSTANCE; 208 } 209 return coll.iterator(); 210 } 211 212 222 public Object put(Object key, Object value) { 223 Collection coll = getCollection(key); 226 if (coll == null) { 227 coll = createCollection(null); 228 super.put(key, coll); 229 } 230 boolean results = coll.add(value); 231 return (results ? value : null); 232 } 233 234 242 public boolean putAll(Object key, Collection values) { 243 if (values == null || values.size() == 0) { 244 return false; 245 } 246 Collection coll = getCollection(key); 247 if (coll == null) { 248 coll = createCollection(values); 249 if (coll.size() == 0) { 250 return false; 251 } 252 super.put(key, coll); 253 return true; 254 } else { 255 return coll.addAll(values); 256 } 257 } 258 259 267 public boolean containsValue(Object value) { 268 Set pairs = super.entrySet(); 269 270 if (pairs == null) { 271 return false; 272 } 273 Iterator pairsIterator = pairs.iterator(); 274 while (pairsIterator.hasNext()) { 275 Map.Entry keyValuePair = (Map.Entry ) pairsIterator.next(); 276 Collection coll = (Collection ) keyValuePair.getValue(); 277 if (coll.contains(value)) { 278 return true; 279 } 280 } 281 return false; 282 } 283 284 291 public boolean containsValue(Object key, Object value) { 292 Collection coll = getCollection(key); 293 if (coll == null) { 294 return false; 295 } 296 return coll.contains(value); 297 } 298 299 312 public Object remove(Object key, Object item) { 313 Collection valuesForKey = getCollection(key); 314 if (valuesForKey == null) { 315 return null; 316 } 317 valuesForKey.remove(item); 318 319 if (valuesForKey.isEmpty()){ 322 remove(key); 323 } 324 return item; 325 } 326 327 332 public void clear() { 333 Set pairs = super.entrySet(); 335 Iterator pairsIterator = pairs.iterator(); 336 while (pairsIterator.hasNext()) { 337 Map.Entry keyValuePair = (Map.Entry ) pairsIterator.next(); 338 Collection coll = (Collection ) keyValuePair.getValue(); 339 coll.clear(); 340 } 341 super.clear(); 342 } 343 344 351 public Collection values() { 352 Collection vs = values; 353 return (vs != null ? vs : (values = new Values())); 354 } 355 356 360 private class Values extends AbstractCollection { 361 362 public Iterator iterator() { 363 return new ValueIterator(); 364 } 365 366 public int size() { 367 int compt = 0; 368 Iterator it = iterator(); 369 while (it.hasNext()) { 370 it.next(); 371 compt++; 372 } 373 return compt; 374 } 375 376 public void clear() { 377 MultiHashMap.this.clear(); 378 } 379 380 } 381 382 385 private class ValueIterator implements Iterator { 386 private Iterator backedIterator; 387 private Iterator tempIterator; 388 389 private ValueIterator() { 390 backedIterator = MultiHashMap.super.values().iterator(); 391 } 392 393 private boolean searchNextIterator() { 394 while (tempIterator == null || tempIterator.hasNext() == false) { 395 if (backedIterator.hasNext() == false) { 396 return false; 397 } 398 tempIterator = ((Collection ) backedIterator.next()).iterator(); 399 } 400 return true; 401 } 402 403 public boolean hasNext() { 404 return searchNextIterator(); 405 } 406 407 public Object next() { 408 if (searchNextIterator() == false) { 409 throw new NoSuchElementException (); 410 } 411 return tempIterator.next(); 412 } 413 414 public void remove() { 415 if (tempIterator == null) { 416 throw new IllegalStateException (); 417 } 418 tempIterator.remove(); 419 } 420 421 } 422 423 431 public Object clone() { 432 MultiHashMap cloned = (MultiHashMap) super.clone(); 433 434 for (Iterator it = cloned.entrySet().iterator(); it.hasNext();) { 436 Map.Entry entry = (Map.Entry ) it.next(); 437 Collection coll = (Collection ) entry.getValue(); 438 Collection newColl = createCollection(coll); 439 entry.setValue(newColl); 440 } 441 return cloned; 442 } 443 444 452 protected Collection createCollection(Collection coll) { 453 if (coll == null) { 454 return new ArrayList (); 455 } else { 456 return new ArrayList (coll); 457 } 458 } 459 460 } 461 | Popular Tags |