Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.collection

public Class CompositeCollection<E>

extends Object
implements Collection<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type of the elements in the collection
Imports
java.io.Serializable, java.lang.reflect.Array, java.util.ArrayList, .Collection, .Iterator, .List, .Objects, java.util.function.Predicate, org.apache.commons.collections4.CollectionUtils, org.apache.commons.collections4.iterators.EmptyIterator, .IteratorChain, org.apache.commons.collections4.list.UnmodifiableList

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
CompositeCollection.CollectionMutator<
the element being held in the collection
E
>

Pluggable strategy to handle changes to the composite.

Field Summary

Modifier and TypeField and Description
private final List<Collection<E>>
all

Collections in the composite

private CompositeCollection.CollectionMutator<E>
mutator

CollectionMutator to handle changes to the collection

private static final long
serialVersionUID

Serialization version

Constructor Summary

AccessConstructor and Description
public
CompositeCollection()

Create an empty CompositeCollection.

public
CompositeCollection(final Collection<E>
the Collection to be appended to the composite
compositeCollection
)

Create a Composite Collection with one collection.

public
CompositeCollection(final Collection<E>
the Collection to be appended to the composite
compositeCollection1
,
final Collection<E>
the Collection to be appended to the composite
compositeCollection2
)

Create a Composite Collection with two collections.

public
CompositeCollection(final Collection<E>...
the collections to composite
compositeCollections
)

Create a Composite Collection with an array of collections.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the collection was modified
add
(final E
the object to add
obj
)

Implements java.util.Collection.add.

Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.
public boolean

Returns:

true if the collection was modified
addAll
(final Collection<? extends E>
the collection to add
coll
)

Implements java.util.Collection.addAll.

Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.
public void
addComposited(final Collection<E>
the Collection to be appended to the composite
compositeCollection
)

Add these Collections to the list of collections in this composite

public void
addComposited(final Collection<E>
the Collection to be appended to the composite
compositeCollection1
,
final Collection<E>
the Collection to be appended to the composite
compositeCollection2
)

Add these Collections to the list of collections in this composite

public void
addComposited(final Collection<E>...
the Collections to be appended to the composite
compositeCollections
)

Add these Collections to the list of collections in this composite

public void
clear()

Implements java.util.Collection.clear.

Removes all of the elements from this collection .
public boolean

Returns:

true if obj is contained in any of the contained collections
contains
(final Object
the object to search for
obj
)

Implements java.util.Collection.contains.

Checks whether this composite collection contains the object.
public boolean

Returns:

true if all elements contained
containsAll
(final Collection<?>
the collection to check for
coll
)

Implements java.util.Collection.containsAll.

Checks whether this composite contains all the elements in the specified collection.
public List<Collection<E>>

Returns:

Unmodifiable list of all collections in this composite.
getCollections
()

Gets the collections being decorated.

protected CompositeCollection.CollectionMutator<E>

Returns:

CollectionMutator<E>
getMutator
()

Get the collection mutator to be used for this CompositeCollection.

public boolean

Returns:

true if all of the contained collections are empty
isEmpty
()

Implements java.util.Collection.isEmpty.

Checks whether this composite collection is empty.
public Iterator<E>

Returns:

an IteratorChain instance which supports remove(). Iteration occurs over contained collections in the order they were added, but this behavior should not be relied upon.
iterator
()

Implements java.util.Collection.iterator.

Gets an iterator over all the collections in this composite.
public boolean

Returns:

true if the collection is changed
remove
(final Object
the object being removed
obj
)

Implements java.util.Collection.remove.

Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.
public boolean

Returns:

true if the collection was modified
removeAll
(final Collection<?>
the collection to remove
coll
)

Implements java.util.Collection.removeAll.

Removes the elements in the specified collection from this composite collection.
public void
removeComposited(final Collection<E>
collection to be removed
coll
)

Removes a collection from the those being decorated in this composite.

public boolean

Returns:

true if the collection was modified
removeIf
(final Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate from this composite collection.
public boolean

Returns:

true if the collection was modified
retainAll
(final Collection<?>
the collection to remove
coll
)

Implements java.util.Collection.retainAll.

Retains all the elements in the specified collection in this composite collection, removing all others.
public void
setMutator(final CompositeCollection.CollectionMutator<E>
the mutator to use
mutator
)

Specify a CollectionMutator strategy instance to handle changes.

public int

Returns:

total number of elements in all contained containers
size
()

Implements java.util.Collection.size.

Gets the size of this composite collection.
public Object[]

Returns:

an object array of all the elements in the collection
toArray
()

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this composite.
public <
the type of the elements in the collection
T
>
T[]

Returns:

an array of all the elements in the collection
toArray
(final T[]
the array to use, populating if possible
array
)

Implements java.util.Collection.toArray.

Returns an object array, populating the supplied array if possible.
public Collection<E>

Returns:

A new ArrayList containing all of the elements in this composite. The new collection is not backed by this composite.
toCollection
()

Returns a new collection containing all of the elements

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allback to summary
private final List<Collection<E>> all

Collections in the composite

mutatorback to summary
private CompositeCollection.CollectionMutator<E> mutator

CollectionMutator to handle changes to the collection

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

CompositeCollectionback to summary
public CompositeCollection()

Create an empty CompositeCollection.

CompositeCollectionback to summary
public CompositeCollection(final Collection<E> compositeCollection)

Create a Composite Collection with one collection.

Parameters
compositeCollection:Collection<E>

the Collection to be appended to the composite

CompositeCollectionback to summary
public CompositeCollection(final Collection<E> compositeCollection1, final Collection<E> compositeCollection2)

Create a Composite Collection with two collections.

Parameters
compositeCollection1:Collection<E>

the Collection to be appended to the composite

compositeCollection2:Collection<E>

the Collection to be appended to the composite

CompositeCollectionback to summary
public CompositeCollection(final Collection<E>... compositeCollections)

Create a Composite Collection with an array of collections.

Parameters
compositeCollections:Collection<E>[]

the collections to composite

Method Detail

addback to summary
public boolean add(final E obj)

Implements java.util.Collection.add.

Adds an object to the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.

Parameters
obj:E

the object to add

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
  • if CollectionMutator hasn't been set
  • 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
addAllback to summary
public boolean addAll(final Collection<? extends E> coll)

Implements java.util.Collection.addAll.

Adds a collection of elements to this collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.

Parameters
coll:Collection<? extends E>

the collection to add

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
  • if CollectionMutator hasn't been set
  • 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
addCompositedback to summary
public void addComposited(final Collection<E> compositeCollection)

Add these Collections to the list of collections in this composite

Parameters
compositeCollection:Collection<E>

the Collection to be appended to the composite

addCompositedback to summary
public void addComposited(final Collection<E> compositeCollection1, final Collection<E> compositeCollection2)

Add these Collections to the list of collections in this composite

Parameters
compositeCollection1:Collection<E>

the Collection to be appended to the composite

compositeCollection2:Collection<E>

the Collection to be appended to the composite

addCompositedback to summary
public void addComposited(final Collection<E>... compositeCollections)

Add these Collections to the list of collections in this composite

Parameters
compositeCollections:Collection<E>[]

the Collections to be appended to the composite

clearback to summary
public void clear()

Implements java.util.Collection.clear.

Removes all of the elements from this collection .

This implementation calls clear() on each collection.

Annotations
@Override
Exceptions
UnsupportedOperationException:
if clear is unsupported
containsback to summary
public boolean contains(final Object obj)

Implements java.util.Collection.contains.

Checks whether this composite collection contains the object.

This implementation calls contains() on each collection.

Parameters
obj:Object

the object to search for

Returns:boolean

true if obj is contained in any of the contained collections

Annotations
@Override
containsAllback to summary
public boolean containsAll(final Collection<?> coll)

Implements java.util.Collection.containsAll.

Checks whether this composite contains all the elements in the specified collection.

This implementation calls contains() for each element in the specified collection.

Parameters
coll:Collection<?>

the collection to check for

Returns:boolean

true if all elements contained

Annotations
@Override
getCollectionsback to summary
public List<Collection<E>> getCollections()

Gets the collections being decorated.

Returns:List<Collection<E>>

Unmodifiable list of all collections in this composite.

getMutatorback to summary
protected CompositeCollection.CollectionMutator<E> getMutator()

Get the collection mutator to be used for this CompositeCollection.

Returns:CompositeCollection.CollectionMutator<E>

CollectionMutator<E>

isEmptyback to summary
public boolean isEmpty()

Implements java.util.Collection.isEmpty.

Checks whether this composite collection is empty.

This implementation calls isEmpty() on each collection.

Returns:boolean

true if all of the contained collections are empty

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Implements java.util.Collection.iterator.

Gets an iterator over all the collections in this composite.

This implementation uses an IteratorChain.

Returns:Iterator<E>

an IteratorChain instance which supports remove(). Iteration occurs over contained collections in the order they were added, but this behavior should not be relied upon.

Annotations
@Override
See Also
IteratorChain
removeback to summary
public boolean remove(final Object obj)

Implements java.util.Collection.remove.

Removes an object from the collection, throwing UnsupportedOperationException unless a CollectionMutator strategy is specified.

Parameters
obj:Object

the object being removed

Returns:boolean

true if the collection is changed

Annotations
@Override
Exceptions
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
removeAllback to summary
public boolean removeAll(final Collection<?> coll)

Implements java.util.Collection.removeAll.

Removes the elements in the specified collection from this composite collection.

This implementation calls removeAll on each collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
if removeAll is unsupported
removeCompositedback to summary
public void removeComposited(final Collection<E> coll)

Removes a collection from the those being decorated in this composite.

Parameters
coll:Collection<E>

collection to be removed

removeIfback to summary
public boolean removeIf(final Predicate<? super E> filter)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate from this composite collection.

This implementation calls removeIf on each collection.

Parameters
filter:Predicate<? super E>

a predicate which returns true for elements to be removed

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
if removeIf is unsupported
Since
4.4
retainAllback to summary
public boolean retainAll(final Collection<?> coll)

Implements java.util.Collection.retainAll.

Retains all the elements in the specified collection in this composite collection, removing all others.

This implementation calls retainAll() on each collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
if retainAll is unsupported
setMutatorback to summary
public void setMutator(final CompositeCollection.CollectionMutator<E> mutator)

Specify a CollectionMutator strategy instance to handle changes.

Parameters
mutator:CompositeCollection.CollectionMutator<E>

the mutator to use

sizeback to summary
public int size()

Implements java.util.Collection.size.

Gets the size of this composite collection.

This implementation calls size() on each collection.

Returns:int

total number of elements in all contained containers

Annotations
@Override
toArrayback to summary
public Object[] toArray()

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this composite.

Returns:Object[]

an object array of all the elements in the collection

Annotations
@Override
toArrayback to summary
public <T> T[] toArray(final T[] array)

Implements java.util.Collection.toArray.

Returns an object array, populating the supplied array if possible. See Collection interface for full details.

Parameters
<T>
the type of the elements in the collection
array:T[]

the array to use, populating if possible

Returns:T[]

an array of all the elements in the collection

Annotations
@Override
@SuppressWarnings:unchecked
toCollectionback to summary
public Collection<E> toCollection()

Returns a new collection containing all of the elements

Returns:Collection<E>

A new ArrayList containing all of the elements in this composite. The new collection is not backed by this composite.

org.apache.commons.collections4.collection back to summary

public Interface CompositeCollection.CollectionMutator<E>

extends Serializable
Type Parameters
<E>
the element being held in the collection

Pluggable strategy to handle changes to the composite.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the collection is changed
add
(CompositeCollection<E>
the CompositeCollection being changed
composite
,
List<Collection<E>>
all of the Collection instances in this CompositeCollection
collections
,
E
the object being added
obj
)

Called when an object is to be added to the composite.

public boolean

Returns:

true if the collection is changed
addAll
(CompositeCollection<E>
the CompositeCollection being changed
composite
,
List<Collection<E>>
all of the Collection instances in this CompositeCollection
collections
,
Collection<? extends E>
the collection being added
coll
)

Called when a collection is to be added to the composite.

public boolean

Returns:

true if the collection is changed
remove
(CompositeCollection<E>
the CompositeCollection being changed
composite
,
List<Collection<E>>
all of the Collection instances in this CompositeCollection
collections
,
Object
the object being removed
obj
)

Called when an object is to be removed to the composite.

Method Detail

addback to summary
public boolean add(CompositeCollection<E> composite, List<Collection<E>> collections, E obj)

Called when an object is to be added to the composite.

Parameters
composite:CompositeCollection<E>

the CompositeCollection being changed

collections:List<Collection<E>>

all of the Collection instances in this CompositeCollection

obj:E

the object being added

Returns:boolean

true if the collection is changed

Exceptions
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
addAllback to summary
public boolean addAll(CompositeCollection<E> composite, List<Collection<E>> collections, Collection<? extends E> coll)

Called when a collection is to be added to the composite.

Parameters
composite:CompositeCollection<E>

the CompositeCollection being changed

collections:List<Collection<E>>

all of the Collection instances in this CompositeCollection

coll:Collection<? extends E>

the collection being added

Returns:boolean

true if the collection is changed

Exceptions
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
removeback to summary
public boolean remove(CompositeCollection<E> composite, List<Collection<E>> collections, Object obj)

Called when an object is to be removed to the composite.

Parameters
composite:CompositeCollection<E>

the CompositeCollection being changed

collections:List<Collection<E>>

all of the Collection instances in this CompositeCollection

obj:Object

the object being removed

Returns:boolean

true if the collection is changed

Exceptions
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