1 16 package org.apache.commons.collections.map; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.apache.commons.collections.CollectionUtils; 24 import org.apache.commons.collections.collection.CompositeCollection; 25 import org.apache.commons.collections.set.CompositeSet; 26 27 39 public class CompositeMap implements Map { 40 41 42 private Map [] composite; 43 44 45 private MapMutator mutator; 46 47 50 public CompositeMap() { 51 this(new Map []{}, null); 52 } 53 54 61 public CompositeMap(Map one, Map two) { 62 this(new Map []{one, two}, null); 63 } 64 65 72 public CompositeMap(Map one, Map two, MapMutator mutator) { 73 this(new Map []{one, two}, mutator); 74 } 75 76 83 public CompositeMap(Map [] composite) { 84 this(composite, null); 85 } 86 87 94 public CompositeMap(Map [] composite, MapMutator mutator) { 95 this.mutator = mutator; 96 this.composite = new Map [0]; 97 for (int i = composite.length - 1; i >= 0; --i) { 98 this.addComposited(composite[i]); 99 } 100 } 101 102 108 public void setMutator(MapMutator mutator) { 109 this.mutator = mutator; 110 } 111 112 119 public synchronized void addComposited(Map map) throws IllegalArgumentException { 120 for (int i = composite.length - 1; i >= 0; --i) { 121 Collection intersect = CollectionUtils.intersection(this.composite[i].keySet(), map.keySet()); 122 if (intersect.size() != 0) { 123 if (this.mutator == null) { 124 throw new IllegalArgumentException ("Key collision adding Map to CompositeMap"); 125 } 126 else { 127 this.mutator.resolveCollision(this, this.composite[i], map, intersect); 128 } 129 } 130 } 131 Map [] temp = new Map [this.composite.length + 1]; 132 System.arraycopy(this.composite, 0, temp, 0, this.composite.length); 133 temp[temp.length - 1] = map; 134 this.composite = temp; 135 } 136 137 143 public synchronized Map removeComposited(Map map) { 144 int size = this.composite.length; 145 for (int i = 0; i < size; ++i) { 146 if (this.composite[i].equals(map)) { 147 Map [] temp = new Map [size - 1]; 148 System.arraycopy(this.composite, 0, temp, 0, i); 149 System.arraycopy(this.composite, i + 1, temp, i, size - i - 1); 150 this.composite = temp; 151 return map; 152 } 153 } 154 return null; 155 } 156 157 163 public void clear() { 164 for (int i = this.composite.length - 1; i >= 0; --i) { 165 this.composite[i].clear(); 166 } 167 } 168 169 185 public boolean containsKey(Object key) { 186 for (int i = this.composite.length - 1; i >= 0; --i) { 187 if (this.composite[i].containsKey(key)) { 188 return true; 189 } 190 } 191 return false; 192 } 193 194 210 public boolean containsValue(Object value) { 211 for (int i = this.composite.length - 1; i >= 0; --i) { 212 if (this.composite[i].containsValue(value)) { 213 return true; 214 } 215 } 216 return false; 217 } 218 219 236 public Set entrySet() { 237 CompositeSet entries = new CompositeSet(); 238 for (int i = this.composite.length - 1; i >= 0; --i) { 239 entries.addComposited(this.composite[i].entrySet()); 240 } 241 return entries; 242 } 243 244 268 public Object get(Object key) { 269 for (int i = this.composite.length - 1; i >= 0; --i) { 270 if (this.composite[i].containsKey(key)) { 271 return this.composite[i].get(key); 272 } 273 } 274 return null; 275 } 276 277 282 public boolean isEmpty() { 283 for (int i = this.composite.length - 1; i >= 0; --i) { 284 if (!this.composite[i].isEmpty()) { 285 return false; 286 } 287 } 288 return true; 289 } 290 291 306 public Set keySet() { 307 CompositeSet keys = new CompositeSet(); 308 for (int i = this.composite.length - 1; i >= 0; --i) { 309 keys.addComposited(this.composite[i].keySet()); 310 } 311 return keys; 312 } 313 314 339 public Object put(Object key, Object value) { 340 if (this.mutator == null) { 341 throw new UnsupportedOperationException ("No mutator specified"); 342 } 343 return this.mutator.put(this, this.composite, key, value); 344 } 345 346 368 public void putAll(Map map) { 369 if (this.mutator == null) { 370 throw new UnsupportedOperationException ("No mutator specified"); 371 } 372 this.mutator.putAll(this, this.composite, map); 373 } 374 375 400 public Object remove(Object key) { 401 for (int i = this.composite.length - 1; i >= 0; --i) { 402 if (this.composite[i].containsKey(key)) { 403 return this.composite[i].remove(key); 404 } 405 } 406 return null; 407 } 408 409 416 public int size() { 417 int size = 0; 418 for (int i = this.composite.length - 1; i >= 0; --i) { 419 size += this.composite[i].size(); 420 } 421 return size; 422 } 423 424 437 public Collection values() { 438 CompositeCollection keys = new CompositeCollection(); 439 for (int i = this.composite.length - 1; i >= 0; --i) { 440 keys.addComposited(this.composite[i].values()); 441 } 442 return keys; 443 } 444 445 451 public boolean equals(Object obj) { 452 if (obj instanceof Map ) { 453 Map map = (Map ) obj; 454 return (this.entrySet().equals(map.entrySet())); 455 } 456 return false; 457 } 458 459 462 public int hashCode() { 463 int code = 0; 464 for (Iterator i = this.entrySet().iterator(); i.hasNext();) { 465 code += i.next().hashCode(); 466 } 467 return code; 468 } 469 470 475 public static interface MapMutator { 476 486 public void resolveCollision( 487 CompositeMap composite, Map existing, Map added, Collection intersect); 488 489 511 public Object put(CompositeMap map, Map [] composited, Object key, Object value); 512 513 529 public void putAll(CompositeMap map, Map [] composited, Map mapToAdd); 530 } 531 } 532 | Popular Tags |