Class CompositeCollection
java.lang.Object
org.apache.commons.collections.collection.CompositeCollection
- All Implemented Interfaces:
Iterable
,Collection
- Direct Known Subclasses:
CompositeSet
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:
- Commons Collections 3.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Brian McCallister, Stephen Colebourne, Phil Steitz
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Pluggable strategy to handle changes to the composite. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Collection[]
Collections in the compositeprotected CompositeCollection.CollectionMutator
CollectionMutator to handle changes to the collection -
Constructor Summary
ConstructorsConstructorDescriptionCreate an empty CompositeCollection.Create a Composite Collection with only coll composited.CompositeCollection
(Collection[] colls) Create a CompositeCollection with colls as the initial list of composited collections. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.boolean
addAll
(Collection coll) Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.void
Add an additional collection to this composite.void
addComposited
(Collection[] comps) Add these Collections to the list of collections in this compositevoid
Add two additional collections to this composite.void
clear()
Removes all of the elements from this collection .boolean
Checks whether this composite collection contains the object.boolean
containsAll
(Collection coll) Checks whether this composite contains all the elements in the specified collection.Gets the collections being decorated.boolean
isEmpty()
Checks whether this composite collection is empty.iterator()
Gets an iterator over all the collections in this composite.boolean
Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.boolean
removeAll
(Collection coll) Removes the elements in the specified collection from this composite collection.void
removeComposited
(Collection coll) Removes a collection from the those being decorated in this composite.boolean
retainAll
(Collection coll) Retains all the elements in the specified collection in this composite collection, removing all others.void
Specify a CollectionMutator strategy instance to handle changes.int
size()
Gets the size of this composite collection.Object[]
toArray()
Returns an array containing all of the elements in this composite.Object[]
Returns an object array, populating the supplied array if possible.Returns a new collection containing all of the elementsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
mutator
CollectionMutator to handle changes to the collection -
all
Collections in the composite
-
-
Constructor Details
-
CompositeCollection
public CompositeCollection()Create an empty CompositeCollection. -
CompositeCollection
Create a Composite Collection with only coll composited.- Parameters:
coll
- a collection to decorate
-
CompositeCollection
Create a CompositeCollection with colls as the initial list of composited collections.- Parameters:
colls
- an array of collections to decorate
-
-
Method Details
-
size
public int size()Gets the size of this composite collection.This implementation calls
size()
on each collection.- Specified by:
size
in interfaceCollection
- Returns:
- total number of elements in all contained containers
-
isEmpty
public boolean isEmpty()Checks whether this composite collection is empty.This implementation calls
isEmpty()
on each collection.- Specified by:
isEmpty
in interfaceCollection
- Returns:
- true if all of the contained collections are empty
-
contains
Checks whether this composite collection contains the object.This implementation calls
contains()
on each collection.- Specified by:
contains
in interfaceCollection
- Parameters:
obj
- the object to search for- Returns:
- true if obj is contained in any of the contained collections
-
iterator
Gets an iterator over all the collections in this composite.This implementation uses an
IteratorChain
.- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- Returns:
- an
IteratorChain
instance which supportsremove()
. Iteration occurs over contained collections in the order they were added, but this behavior should not be relied upon. - See Also:
-
toArray
Returns an array containing all of the elements in this composite.- Specified by:
toArray
in interfaceCollection
- Returns:
- an object array of all the elements in the collection
-
toArray
Returns an object array, populating the supplied array if possible. SeeCollection
interface for full details.- Specified by:
toArray
in interfaceCollection
- Parameters:
array
- the array to use, populating if possible- Returns:
- an array of all the elements in the collection
-
add
Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
add
in interfaceCollection
- Parameters:
obj
- the object to add- Returns:
- true if the collection was modified
- Throws:
UnsupportedOperationException
- if CollectionMutator hasn't been setUnsupportedOperationException
- if add is unsupportedClassCastException
- if the object cannot be added due to its typeNullPointerException
- if the object cannot be added because its nullIllegalArgumentException
- if the object cannot be added
-
remove
Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
remove
in interfaceCollection
- Parameters:
obj
- the object being removed- Returns:
- true if the collection is changed
- Throws:
UnsupportedOperationException
- if removed is unsupportedClassCastException
- if the object cannot be removed due to its typeNullPointerException
- if the object cannot be removed because its nullIllegalArgumentException
- if the object cannot be removed
-
containsAll
Checks whether this composite contains all the elements in the specified collection.This implementation calls
contains()
for each element in the specified collection.- Specified by:
containsAll
in interfaceCollection
- Parameters:
coll
- the collection to check for- Returns:
- true if all elements contained
-
addAll
Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
addAll
in interfaceCollection
- Parameters:
coll
- the collection to add- Returns:
- true if the collection was modified
- Throws:
UnsupportedOperationException
- if CollectionMutator hasn't been setUnsupportedOperationException
- if add is unsupportedClassCastException
- if the object cannot be added due to its typeNullPointerException
- if the object cannot be added because its nullIllegalArgumentException
- if the object cannot be added
-
removeAll
Removes the elements in the specified collection from this composite collection.This implementation calls
removeAll
on each collection.- Specified by:
removeAll
in interfaceCollection
- Parameters:
coll
- the collection to remove- Returns:
- true if the collection was modified
- Throws:
UnsupportedOperationException
- if removeAll is unsupported
-
retainAll
Retains all the elements in the specified collection in this composite collection, removing all others.This implementation calls
retainAll()
on each collection.- Specified by:
retainAll
in interfaceCollection
- Parameters:
coll
- the collection to remove- Returns:
- true if the collection was modified
- Throws:
UnsupportedOperationException
- if retainAll is unsupported
-
clear
public void clear()Removes all of the elements from this collection .This implementation calls
clear()
on each collection.- Specified by:
clear
in interfaceCollection
- Throws:
UnsupportedOperationException
- if clear is unsupported
-
setMutator
Specify a CollectionMutator strategy instance to handle changes.- Parameters:
mutator
- the mutator to use
-
addComposited
Add these Collections to the list of collections in this composite- Parameters:
comps
- Collections to be appended to the composite
-
addComposited
Add an additional collection to this composite.- Parameters:
c
- the collection to add
-
addComposited
Add two additional collections to this composite.- Parameters:
c
- the first collection to addd
- the second collection to add
-
removeComposited
Removes a collection from the those being decorated in this composite.- Parameters:
coll
- collection to be removed
-
toCollection
Returns a new collection containing all of the elements- Returns:
- A new ArrayList containing all of the elements in this composite. The new collection is not backed by this composite.
-
getCollections
Gets the collections being decorated.- Returns:
- Unmodifiable collection of all collections in this composite.
-