1 16 package org.apache.commons.collections.set; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Set ; 21 22 import org.apache.commons.collections.CollectionUtils; 23 import org.apache.commons.collections.collection.CompositeCollection; 24 25 37 public class CompositeSet extends CompositeCollection implements Set { 38 41 public CompositeSet() { 42 super(); 43 } 44 45 49 public CompositeSet(Set set) { 50 super(set); 51 } 52 53 56 public CompositeSet(Set [] sets) { 57 super(sets); 58 } 59 60 71 public synchronized void addComposited(Collection c) { 72 if (!(c instanceof Set )) { 73 throw new IllegalArgumentException ("Collections added must implement java.util.Set"); 74 } 75 76 for (Iterator i = this.getCollections().iterator(); i.hasNext();) { 77 Set set = (Set ) i.next(); 78 Collection intersects = CollectionUtils.intersection(set, c); 79 if (intersects.size() > 0) { 80 if (this.mutator == null) { 81 throw new UnsupportedOperationException ( 82 "Collision adding composited collection with no SetMutator set"); 83 } 84 else if (!(this.mutator instanceof SetMutator)) { 85 throw new UnsupportedOperationException ( 86 "Collision adding composited collection to a CompositeSet with a CollectionMutator instead of a SetMutator"); 87 } 88 ((SetMutator) this.mutator).resolveCollision(this, set, (Set ) c, intersects); 89 if (CollectionUtils.intersection(set, c).size() > 0) { 90 throw new IllegalArgumentException ( 91 "Attempt to add illegal entry unresolved by SetMutator.resolveCollision()"); 92 } 93 } 94 } 95 super.addComposited(new Collection []{c}); 96 } 97 98 103 public synchronized void addComposited(Collection c, Collection d) { 104 if (!(c instanceof Set )) throw new IllegalArgumentException ("Argument must implement java.util.Set"); 105 if (!(d instanceof Set )) throw new IllegalArgumentException ("Argument must implement java.util.Set"); 106 this.addComposited(new Set []{(Set ) c, (Set ) d}); 107 } 108 109 114 public synchronized void addComposited(Collection [] comps) { 115 for (int i = comps.length - 1; i >= 0; --i) { 116 this.addComposited(comps[i]); 117 } 118 } 119 120 127 public void setMutator(CollectionMutator mutator) { 128 super.setMutator(mutator); 129 } 130 131 132 133 140 public boolean remove(Object obj) { 141 for (Iterator i = this.getCollections().iterator(); i.hasNext();) { 142 Set set = (Set ) i.next(); 143 if (set.contains(obj)) return set.remove(obj); 144 } 145 return false; 146 } 147 148 149 152 public boolean equals(Object obj) { 153 if (obj instanceof Set ) { 154 Set set = (Set ) obj; 155 if (set.containsAll(this) && set.size() == this.size()) { 156 return true; 157 } 158 } 159 return false; 160 } 161 162 165 public int hashCode() { 166 int code = 0; 167 for (Iterator i = this.iterator(); i.hasNext();) { 168 Object next = i.next(); 169 code += (next != null ? next.hashCode() : 0); 170 } 171 return code; 172 } 173 174 180 public static interface SetMutator extends CompositeCollection.CollectionMutator { 181 195 public void resolveCollision(CompositeSet comp, Set existing, Set added, Collection intersects); 196 } 197 } 198 | Popular Tags |