Package org.apache.cayenne.util.commons
Class CompositeCollection<E>
java.lang.Object
org.apache.cayenne.util.commons.CompositeCollection<E>
- All Implemented Interfaces:
- Iterable<E>,- Collection<E>
public class CompositeCollection<E> extends Object implements Collection<E>
Decorates a collection of other collections to provide a single unified view.
 
Changes made to this collection will actually be made on the decorated collection. Add and remove operations require the use of a pluggable strategy. If no strategy is provided then add and remove are unsupported.
- Since:
- 4.1 NOTE: this is a simplified and type-safe version of CompositeCollection found in commons-collections v3.2.1
- Author:
- Brian McCallister, Stephen Colebourne, Phil Steitz
- 
Field SummaryFields Modifier and Type Field Description protected ArrayList<Collection<E>>allCollections in the composite
- 
Constructor SummaryConstructors Constructor Description CompositeCollection()Create an empty CompositeCollection.
- 
Method SummaryModifier and Type Method Description booleanadd(Object obj)Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.booleanaddAll(Collection<? extends E> coll)Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.voidaddComposited(Collection<E> c)Add an additional collection to this composite.voidclear()Removes all of the elements from this collection .booleancontains(Object obj)Checks whether this composite collection contains the object.booleancontainsAll(Collection<?> coll)Checks whether this composite contains all the elements in the specified collection.Collection<Collection<E>>getCollections()Gets the collections being decorated.booleanisEmpty()Checks whether this composite collection is empty.Iterator<E>iterator()Gets an iterator over all the collections in this composite.booleanremove(Object obj)Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.booleanremoveAll(Collection<?> coll)Removes the elements in the specified collection from this composite collection.voidremoveComposited(Collection<E> coll)Removes a collection from the those being decorated in this composite.booleanretainAll(Collection<?> coll)Retains all the elements in the specified collection in this composite collection, removing all others.intsize()Gets the size of this composite collection.Object[]toArray()Returns an array containing all of the elements in this composite.<T> T[]toArray(T[] array)Returns an object array, populating the supplied array if possible.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collectionequals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
- 
Field Details- 
allCollections in the composite
 
- 
- 
Constructor Details- 
CompositeCollectionpublic CompositeCollection()Create an empty CompositeCollection.
 
- 
- 
Method Details- 
sizepublic int size()Gets the size of this composite collection.This implementation calls size()on each collection.- Specified by:
- sizein interface- Collection<E>
- Returns:
- total number of elements in all contained containers
 
- 
isEmptypublic boolean isEmpty()Checks whether this composite collection is empty.This implementation calls isEmpty()on each collection.- Specified by:
- isEmptyin interface- Collection<E>
- Returns:
- true if all of the contained collections are empty
 
- 
containsChecks whether this composite collection contains the object.This implementation calls contains()on each collection.- Specified by:
- containsin interface- Collection<E>
- Parameters:
- obj- the object to search for
- Returns:
- true if obj is contained in any of the contained collections
 
- 
iteratorGets an iterator over all the collections in this composite.This implementation uses an IteratorChain.- Specified by:
- iteratorin interface- Collection<E>
- Specified by:
- iteratorin interface- Iterable<E>
- Returns:
- an IteratorChaininstance which supportsremove(). Iteration occurs over contained collections in the order they were added, but this behavior should not be relied upon.
- See Also:
- IteratorChain
 
- 
toArrayReturns an array containing all of the elements in this composite.- Specified by:
- toArrayin interface- Collection<E>
- Returns:
- an object array of all the elements in the collection
 
- 
toArraypublic <T> T[] toArray(T[] array)Returns an object array, populating the supplied array if possible. SeeCollectioninterface for full details.- Specified by:
- toArrayin interface- Collection<E>
- Parameters:
- array- the array to use, populating if possible
- Returns:
- an array of all the elements in the collection
 
- 
addAdds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
- addin interface- Collection<E>
- Parameters:
- obj- the object to add
- Returns:
- true if the collection was modified
- Throws:
- UnsupportedOperationException- if CollectionMutator hasn't been set
- UnsupportedOperationException- if add is unsupported
- ClassCastException- if the object cannot be added due to its type
- NullPointerException- if the object cannot be added because its null
- IllegalArgumentException- if the object cannot be added
 
- 
removeRemoves an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
- removein interface- Collection<E>
- Parameters:
- obj- the object being removed
- Returns:
- true if the collection is changed
- Throws:
- UnsupportedOperationException- if removed is unsupported
- ClassCastException- if the object cannot be removed due to its type
- NullPointerException- if the object cannot be removed because its null
- IllegalArgumentException- if the object cannot be removed
 
- 
containsAllChecks whether this composite contains all the elements in the specified collection.This implementation calls contains()for each element in the specified collection.- Specified by:
- containsAllin interface- Collection<E>
- Parameters:
- coll- the collection to check for
- Returns:
- true if all elements contained
 
- 
addAllAdds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
- addAllin interface- Collection<E>
- Parameters:
- coll- the collection to add
- Returns:
- true if the collection was modified
- Throws:
- UnsupportedOperationException- if CollectionMutator hasn't been set
- UnsupportedOperationException- if add is unsupported
- ClassCastException- if the object cannot be added due to its type
- NullPointerException- if the object cannot be added because its null
- IllegalArgumentException- if the object cannot be added
 
- 
removeAllRemoves the elements in the specified collection from this composite collection.This implementation calls removeAllon each collection.- Specified by:
- removeAllin interface- Collection<E>
- Parameters:
- coll- the collection to remove
- Returns:
- true if the collection was modified
- Throws:
- UnsupportedOperationException- if removeAll is unsupported
 
- 
retainAllRetains all the elements in the specified collection in this composite collection, removing all others.This implementation calls retainAll()on each collection.- Specified by:
- retainAllin interface- Collection<E>
- Parameters:
- coll- the collection to remove
- Returns:
- true if the collection was modified
- Throws:
- UnsupportedOperationException- if retainAll is unsupported
 
- 
clearpublic void clear()Removes all of the elements from this collection .This implementation calls clear()on each collection.- Specified by:
- clearin interface- Collection<E>
- Throws:
- UnsupportedOperationException- if clear is unsupported
 
- 
addCompositedAdd an additional collection to this composite.- Parameters:
- c- the collection to add
 
- 
removeCompositedRemoves a collection from the those being decorated in this composite.- Parameters:
- coll- collection to be removed
 
- 
getCollectionsGets the collections being decorated.- Returns:
- Unmodifiable collection of all collections in this composite.
 
 
-