Class CompositeCollection<E>
- java.lang.Object
-
- org.apache.commons.collections4.collection.CompositeCollection<E>
-
- Type Parameters:
E
- the type of the elements in the collection
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<E>
,java.util.Collection<E>
public class CompositeCollection<E> extends java.lang.Object implements java.util.Collection<E>, java.io.Serializable
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:
- 3.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CompositeCollection.CollectionMutator<E>
Pluggable strategy to handle changes to the composite.
-
Constructor Summary
Constructors Constructor Description CompositeCollection()
Create an empty CompositeCollection.CompositeCollection(java.util.Collection<E> compositeCollection)
Create a Composite Collection with one collection.CompositeCollection(java.util.Collection<E>... compositeCollections)
Create a Composite Collection with an array of collections.CompositeCollection(java.util.Collection<E> compositeCollection1, java.util.Collection<E> compositeCollection2)
Create a Composite Collection with two collections.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E obj)
Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.boolean
addAll(java.util.Collection<? extends E> coll)
Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.void
addComposited(java.util.Collection<E> compositeCollection)
Add these Collections to the list of collections in this compositevoid
addComposited(java.util.Collection<E>... compositeCollections)
Add these Collections to the list of collections in this compositevoid
addComposited(java.util.Collection<E> compositeCollection1, java.util.Collection<E> compositeCollection2)
Add these Collections to the list of collections in this compositevoid
clear()
Removes all of the elements from this collection .boolean
contains(java.lang.Object obj)
Checks whether this composite collection contains the object.boolean
containsAll(java.util.Collection<?> coll)
Checks whether this composite contains all the elements in the specified collection.java.util.List<java.util.Collection<E>>
getCollections()
Gets the collections being decorated.boolean
isEmpty()
Checks whether this composite collection is empty.java.util.Iterator<E>
iterator()
Gets an iterator over all the collections in this composite.boolean
remove(java.lang.Object obj)
Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.boolean
removeAll(java.util.Collection<?> coll)
Removes the elements in the specified collection from this composite collection.void
removeComposited(java.util.Collection<E> coll)
Removes a collection from the those being decorated in this composite.boolean
removeIf(java.util.function.Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate from this composite collection.boolean
retainAll(java.util.Collection<?> coll)
Retains all the elements in the specified collection in this composite collection, removing all others.void
setMutator(CompositeCollection.CollectionMutator<E> mutator)
Specify a CollectionMutator strategy instance to handle changes.int
size()
Gets the size of this composite collection.java.lang.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.java.util.Collection<E>
toCollection()
Returns a new collection containing all of the elements-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Constructor Detail
-
CompositeCollection
public CompositeCollection()
Create an empty CompositeCollection.
-
CompositeCollection
public CompositeCollection(java.util.Collection<E> compositeCollection)
Create a Composite Collection with one collection.- Parameters:
compositeCollection
- the Collection to be appended to the composite
-
CompositeCollection
public CompositeCollection(java.util.Collection<E> compositeCollection1, java.util.Collection<E> compositeCollection2)
Create a Composite Collection with two collections.- Parameters:
compositeCollection1
- the Collection to be appended to the compositecompositeCollection2
- the Collection to be appended to the composite
-
CompositeCollection
public CompositeCollection(java.util.Collection<E>... compositeCollections)
Create a Composite Collection with an array of collections.- Parameters:
compositeCollections
- the collections to composite
-
-
Method Detail
-
size
public int size()
Gets the size of this composite collection.This implementation calls
size()
on each collection.- Specified by:
size
in interfacejava.util.Collection<E>
- 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 interfacejava.util.Collection<E>
- Returns:
- true if all of the contained collections are empty
-
contains
public boolean contains(java.lang.Object obj)
Checks whether this composite collection contains the object.This implementation calls
contains()
on each collection.- Specified by:
contains
in interfacejava.util.Collection<E>
- Parameters:
obj
- the object to search for- Returns:
- true if obj is contained in any of the contained collections
-
iterator
public java.util.Iterator<E> iterator()
Gets an iterator over all the collections in this composite.This implementation uses an
IteratorChain
.- Specified by:
iterator
in interfacejava.util.Collection<E>
- Specified by:
iterator
in interfacejava.lang.Iterable<E>
- 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:
IteratorChain
-
toArray
public java.lang.Object[] toArray()
Returns an array containing all of the elements in this composite.- Specified by:
toArray
in interfacejava.util.Collection<E>
- Returns:
- an object array of all the elements in the collection
-
toArray
public <T> T[] toArray(T[] array)
Returns an object array, populating the supplied array if possible. SeeCollection
interface for full details.- Specified by:
toArray
in interfacejava.util.Collection<E>
- Type Parameters:
T
- the type of the elements in the collection- Parameters:
array
- the array to use, populating if possible- Returns:
- an array of all the elements in the collection
-
add
public boolean add(E obj)
Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
add
in interfacejava.util.Collection<E>
- Parameters:
obj
- the object to add- Returns:
true
if the collection was modified- Throws:
java.lang.UnsupportedOperationException
- if CollectionMutator hasn't been setjava.lang.UnsupportedOperationException
- if add is unsupportedjava.lang.ClassCastException
- if the object cannot be added due to its typejava.lang.NullPointerException
- if the object cannot be added because its nulljava.lang.IllegalArgumentException
- if the object cannot be added
-
remove
public boolean remove(java.lang.Object obj)
Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
remove
in interfacejava.util.Collection<E>
- Parameters:
obj
- the object being removed- Returns:
- true if the collection is changed
- Throws:
java.lang.UnsupportedOperationException
- if removed is unsupportedjava.lang.ClassCastException
- if the object cannot be removed due to its typejava.lang.NullPointerException
- if the object cannot be removed because its nulljava.lang.IllegalArgumentException
- if the object cannot be removed
-
containsAll
public boolean containsAll(java.util.Collection<?> coll)
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 interfacejava.util.Collection<E>
- Parameters:
coll
- the collection to check for- Returns:
- true if all elements contained
-
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.- Specified by:
addAll
in interfacejava.util.Collection<E>
- Parameters:
coll
- the collection to add- Returns:
- true if the collection was modified
- Throws:
java.lang.UnsupportedOperationException
- if CollectionMutator hasn't been setjava.lang.UnsupportedOperationException
- if add is unsupportedjava.lang.ClassCastException
- if the object cannot be added due to its typejava.lang.NullPointerException
- if the object cannot be added because its nulljava.lang.IllegalArgumentException
- if the object cannot be added
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
Removes the elements in the specified collection from this composite collection.This implementation calls
removeAll
on each collection.- Specified by:
removeAll
in interfacejava.util.Collection<E>
- Parameters:
coll
- the collection to remove- Returns:
- true if the collection was modified
- Throws:
java.lang.UnsupportedOperationException
- if removeAll is unsupported
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate from this composite collection.This implementation calls
removeIf
on each collection.- Specified by:
removeIf
in interfacejava.util.Collection<E>
- Parameters:
filter
- a predicate which returns true for elements to be removed- Returns:
- true if the collection was modified
- Throws:
java.lang.UnsupportedOperationException
- if removeIf is unsupported- Since:
- 4.4
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
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 interfacejava.util.Collection<E>
- Parameters:
coll
- the collection to remove- Returns:
- true if the collection was modified
- Throws:
java.lang.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 interfacejava.util.Collection<E>
- Throws:
java.lang.UnsupportedOperationException
- if clear is unsupported
-
setMutator
public void setMutator(CompositeCollection.CollectionMutator<E> mutator)
Specify a CollectionMutator strategy instance to handle changes.- Parameters:
mutator
- the mutator to use
-
addComposited
public void addComposited(java.util.Collection<E> compositeCollection)
Add these Collections to the list of collections in this composite- Parameters:
compositeCollection
- the Collection to be appended to the composite
-
addComposited
public void addComposited(java.util.Collection<E> compositeCollection1, java.util.Collection<E> compositeCollection2)
Add these Collections to the list of collections in this composite- Parameters:
compositeCollection1
- the Collection to be appended to the compositecompositeCollection2
- the Collection to be appended to the composite
-
addComposited
public void addComposited(java.util.Collection<E>... compositeCollections)
Add these Collections to the list of collections in this composite- Parameters:
compositeCollections
- the Collections to be appended to the composite
-
removeComposited
public void removeComposited(java.util.Collection<E> coll)
Removes a collection from the those being decorated in this composite.- Parameters:
coll
- collection to be removed
-
toCollection
public java.util.Collection<E> 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
public java.util.List<java.util.Collection<E>> getCollections()
Gets the collections being decorated.- Returns:
- Unmodifiable list of all collections in this composite.
-
-