1 16 package org.apache.commons.collections.collection; 17 18 import java.lang.reflect.Array ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import org.apache.commons.collections.iterators.EmptyIterator; 25 import org.apache.commons.collections.iterators.IteratorChain; 26 import org.apache.commons.collections.list.UnmodifiableList; 27 28 42 public class CompositeCollection implements Collection { 43 44 45 protected CollectionMutator mutator; 46 47 48 protected Collection [] all; 49 50 53 public CompositeCollection() { 54 super(); 55 this.all = new Collection [0]; 56 } 57 58 63 public CompositeCollection(Collection coll) { 64 this(); 65 this.addComposited(coll); 66 } 67 68 74 public CompositeCollection(Collection [] colls) { 75 this(); 76 this.addComposited(colls); 77 } 78 79 87 public int size() { 88 int size = 0; 89 for (int i = this.all.length - 1; i >= 0; i--) { 90 size += this.all[i].size(); 91 } 92 return size; 93 } 94 95 102 public boolean isEmpty() { 103 for (int i = this.all.length - 1; i >= 0; i--) { 104 if (this.all[i].isEmpty() == false) { 105 return false; 106 } 107 } 108 return true; 109 } 110 111 119 public boolean contains(Object obj) { 120 for (int i = this.all.length - 1; i >= 0; i--) { 121 if (this.all[i].contains(obj)) { 122 return true; 123 } 124 } 125 return false; 126 } 127 128 138 public Iterator iterator() { 139 if (this.all.length == 0) { 140 return EmptyIterator.INSTANCE; 141 } 142 IteratorChain chain = new IteratorChain(); 143 for (int i = 0; i < this.all.length; ++i) { 144 chain.addIterator(this.all[i].iterator()); 145 } 146 return chain; 147 } 148 149 154 public Object [] toArray() { 155 final Object [] result = new Object [this.size()]; 156 int i = 0; 157 for (Iterator it = this.iterator(); it.hasNext(); i++) { 158 result[i] = it.next(); 159 } 160 return result; 161 } 162 163 170 public Object [] toArray(Object [] array) { 171 int size = this.size(); 172 Object [] result = null; 173 if (array.length >= size) { 174 result = array; 175 } 176 else { 177 result = (Object []) Array.newInstance(array.getClass().getComponentType(), size); 178 } 179 180 int offset = 0; 181 for (int i = 0; i < this.all.length; ++i) { 182 for (Iterator it = this.all[i].iterator(); it.hasNext();) { 183 result[offset++] = it.next(); 184 } 185 } 186 if (result.length > size) { 187 result[size] = null; 188 } 189 return result; 190 } 191 192 204 public boolean add(Object obj) { 205 if (this.mutator == null) { 206 throw new UnsupportedOperationException ( 207 "add() is not supported on CompositeCollection without a CollectionMutator strategy"); 208 } 209 return this.mutator.add(this, this.all, obj); 210 } 211 212 223 public boolean remove(Object obj) { 224 if (this.mutator == null) { 225 throw new UnsupportedOperationException ( 226 "remove() is not supported on CompositeCollection without a CollectionMutator strategy"); 227 } 228 return this.mutator.remove(this, this.all, obj); 229 } 230 231 240 public boolean containsAll(Collection coll) { 241 for (Iterator it = coll.iterator(); it.hasNext();) { 242 if (this.contains(it.next()) == false) { 243 return false; 244 } 245 } 246 return true; 247 } 248 249 261 public boolean addAll(Collection coll) { 262 if (this.mutator == null) { 263 throw new UnsupportedOperationException ( 264 "addAll() is not supported on CompositeCollection without a CollectionMutator strategy"); 265 } 266 return this.mutator.addAll(this, this.all, coll); 267 } 268 269 278 public boolean removeAll(Collection coll) { 279 if (coll.size() == 0) { 280 return false; 281 } 282 boolean changed = false; 283 for (int i = this.all.length - 1; i >= 0; i--) { 284 changed = (this.all[i].removeAll(coll) || changed); 285 } 286 return changed; 287 } 288 289 299 public boolean retainAll(final Collection coll) { 300 boolean changed = false; 301 for (int i = this.all.length - 1; i >= 0; i--) { 302 changed = (this.all[i].retainAll(coll) || changed); 303 } 304 return changed; 305 } 306 307 314 public void clear() { 315 for (int i = 0; i < this.all.length; ++i) { 316 this.all[i].clear(); 317 } 318 } 319 320 326 public void setMutator(CollectionMutator mutator) { 327 this.mutator = mutator; 328 } 329 330 335 public void addComposited(Collection [] comps) { 336 ArrayList list = new ArrayList (Arrays.asList(this.all)); 337 list.addAll(Arrays.asList(comps)); 338 all = (Collection []) list.toArray(new Collection [list.size()]); 339 } 340 341 346 public void addComposited(Collection c) { 347 this.addComposited(new Collection []{c}); 348 } 349 350 356 public void addComposited(Collection c, Collection d) { 357 this.addComposited(new Collection []{c, d}); 358 } 359 360 365 public void removeComposited(Collection coll) { 366 ArrayList list = new ArrayList (this.all.length); 367 list.addAll(Arrays.asList(this.all)); 368 list.remove(coll); 369 this.all = (Collection []) list.toArray(new Collection [list.size()]); 370 } 371 372 378 public Collection toCollection() { 379 return new ArrayList (this); 380 } 381 382 387 public Collection getCollections() { 388 return UnmodifiableList.decorate(Arrays.asList(this.all)); 389 } 390 391 395 public interface CollectionMutator { 396 397 409 public boolean add(CompositeCollection composite, Collection [] collections, Object obj); 410 411 423 public boolean addAll(CompositeCollection composite, Collection [] collections, Collection coll); 424 425 437 public boolean remove(CompositeCollection composite, Collection [] collections, Object obj); 438 439 } 440 441 } 442 443 | Popular Tags |