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

public Class CompositeSet<E>

extends Object
implements Set<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Set, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type of the elements in this set
Imports
java.io.Serializable, java.lang.reflect.Array, java.util.ArrayList, .Collection, .HashSet, .Iterator, .List, .Set, .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 set of other sets to provide a single unified view.

Changes made to this set will actually be made on the decorated set. Add operations require the use of a pluggable strategy. If no strategy is provided then add is unsupported.

From version 4.0, this class does not extend CompositeCollection anymore due to its input restrictions (only accepts Sets). See COLLECTIONS-424 for more details.

Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
CompositeSet.SetMutator<E>

Define callbacks for mutation operations.

Field Summary

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

Sets in the composite

private CompositeSet.SetMutator<E>
mutator

SetMutator to handle changes to the collection

private static final long
serialVersionUID

Serialization version

Constructor Summary

AccessConstructor and Description
public
CompositeSet()

Create an empty CompositeSet.

public
CompositeSet(final Set<E>
the initial set in the composite
set
)

Create a CompositeSet with just set composited.

public
CompositeSet(final Set<E>...
the initial sets in the composite
sets
)

Create a composite set with sets as the initial set of composited Sets.

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.Set.add.

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

Returns:

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

Implements java.util.Set.addAll.

Adds a collection of elements to this composite, throwing UnsupportedOperationException unless a SetMutator strategy is specified.
public synchronized void
addComposited(final Set<E>
the set to add
set
)

Add a Set to this composite.

public void
addComposited(final Set<E>
the first Set to be appended to the composite
set1
,
final Set<E>
the second Set to be appended to the composite
set2
)

Add these Sets to the list of sets in this composite.

public void
addComposited(final Set<E>...
the Sets to be appended to the composite
sets
)

Add these Sets to the list of sets in this composite

public void
clear()

Implements java.util.Set.clear.

Removes all of the elements from this composite set.
public boolean

Returns:

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

Implements java.util.Set.contains.

Checks whether this composite set contains the object.
public boolean

Returns:

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

Implements java.util.Set.containsAll.

Checks whether this composite contains all the elements in the specified collection.
public boolean
equals(final Object
object to be compared for equality with this set
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Set.equals.

Compares the specified object with this set for equality.
protected CompositeSet.SetMutator<E>

Returns:

the set mutator
getMutator
()

Get the set mutator to be used for this CompositeSet.

public List<Set<E>>

Returns:

Unmodifiable list of all sets in this composite.
getSets
()

Gets the sets being decorated.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Set.hashCode.

Returns the hash code value for this set.
public boolean

Returns:

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

Implements java.util.Set.isEmpty.

Checks whether this composite set 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.Set.iterator.

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

Returns:

true if the object is removed, false otherwise
remove
(final Object
object to be removed
obj
)

Implements java.util.Set.remove.

If a CollectionMutator is defined for this CompositeSet then this method will be called anyway.
public boolean

Returns:

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

Implements java.util.Set.removeAll.

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

Removes a set from those being decorated in this composite.

public boolean
removeIf(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.
public boolean

Returns:

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

Implements java.util.Set.retainAll.

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

Specify a SetMutator strategy instance to handle changes.

public int

Returns:

total number of elements in all contained containers
size
()

Implements java.util.Set.size.

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

Returns:

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

Implements java.util.Set.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.Set.toArray.

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

Returns:

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

Returns a new Set containing all of the elements.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

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

Sets in the composite

mutatorback to summary
private CompositeSet.SetMutator<E> mutator

SetMutator to handle changes to the collection

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

CompositeSetback to summary
public CompositeSet()

Create an empty CompositeSet.

CompositeSetback to summary
public CompositeSet(final Set<E> set)

Create a CompositeSet with just set composited.

Parameters
set:Set<E>

the initial set in the composite

CompositeSetback to summary
public CompositeSet(final Set<E>... sets)

Create a composite set with sets as the initial set of composited Sets.

Parameters
sets:Set<E>[]

the initial sets in the composite

Method Detail

addback to summary
public boolean add(final E obj)

Implements java.util.Set.add.

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

Parameters
obj:E

the object to add

Returns:boolean

true if the collection was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
if SetMutator hasn't been set or 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.Set.addAll.

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

Parameters
coll:Collection<? extends E>

the collection to add

Returns:boolean

true if the composite was modified

Annotations
@Override
Exceptions
UnsupportedOperationException:
if SetMutator hasn't been set or 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 synchronized void addComposited(final Set<E> set)

Add a Set to this composite.

Parameters
set:Set<E>

the set to add

Exceptions
IllegalArgumentException:
if a SetMutator is set, but fails to resolve a collision
UnsupportedOperationException:
if there is no SetMutator set
NullPointerException:
if set is null
See Also
SetMutator
addCompositedback to summary
public void addComposited(final Set<E> set1, final Set<E> set2)

Add these Sets to the list of sets in this composite.

Parameters
set1:Set<E>

the first Set to be appended to the composite

set2:Set<E>

the second Set to be appended to the composite

addCompositedback to summary
public void addComposited(final Set<E>... sets)

Add these Sets to the list of sets in this composite

Parameters
sets:Set<E>[]

the Sets to be appended to the composite

clearback to summary
public void clear()

Implements java.util.Set.clear.

Removes all of the elements from this composite set.

This implementation calls clear() on each set.

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

Implements java.util.Set.contains.

Checks whether this composite set contains the object.

This implementation calls contains() on each set.

Parameters
obj:Object

the object to search for

Returns:boolean

true if obj is contained in any of the contained sets

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

Implements java.util.Set.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
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Implements java.util.Set.equals.

Doc from java.util.Set.equals.

Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

Parameters
obj:Object

object to be compared for equality with this set

Returns:boolean

true if the specified object is equal to this set

Annotations
@Override
See Also
java.util.Set#equals
getMutatorback to summary
protected CompositeSet.SetMutator<E> getMutator()

Get the set mutator to be used for this CompositeSet.

Returns:CompositeSet.SetMutator<E>

the set mutator

getSetsback to summary
public List<Set<E>> getSets()

Gets the sets being decorated.

Returns:List<Set<E>>

Unmodifiable list of all sets in this composite.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Set.hashCode.

Doc from java.util.Set.hashCode.

Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object#hashCode.

Returns:int

the hash code value for this set

Annotations
@Override
See Also
java.util.Set#hashCode
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Set.isEmpty.

Checks whether this composite set is empty.

This implementation calls isEmpty() on each set.

Returns:boolean

true if all of the contained sets are empty

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

Implements java.util.Set.iterator.

Gets an iterator over all the sets 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.Set.remove.

If a CollectionMutator is defined for this CompositeSet then this method will be called anyway.

Parameters
obj:Object

object to be removed

Returns:boolean

true if the object is removed, false otherwise

Annotations
@Override
removeAllback to summary
public boolean removeAll(final Collection<?> coll)

Implements java.util.Set.removeAll.

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

This implementation calls removeAll on each collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if the composite was modified

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

Removes a set from those being decorated in this composite.

Parameters
set:Set<E>

set to be removed

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

Overrides default java.util.Collection.removeIf.

Doc from java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

Parameters
filter:Predicate<? super E>

a predicate which returns true for elements to be removed

Returns:boolean

true if any elements were removed

Annotations
@Override
Since
4.4
retainAllback to summary
public boolean retainAll(final Collection<?> coll)

Implements java.util.Set.retainAll.

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

This implementation calls retainAll() on each collection.

Parameters
coll:Collection<?>

the collection to remove

Returns:boolean

true if the composite was modified

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

Specify a SetMutator strategy instance to handle changes.

Parameters
mutator:CompositeSet.SetMutator<E>

the mutator to use

sizeback to summary
public int size()

Implements java.util.Set.size.

Gets the size of this composite set.

This implementation calls size() on each set.

Returns:int

total number of elements in all contained containers

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

Implements java.util.Set.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.Set.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
toSetback to summary
public Set<E> toSet()

Returns a new Set containing all of the elements.

Returns:Set<E>

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

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

public Interface CompositeSet.SetMutator<E>

extends Serializable

Define callbacks for mutation operations.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the collection is changed
add
(CompositeSet<E>
the CompositeSet being changed
composite
,
List<Set<E>>
all of the Set instances in this CompositeSet
sets
,
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
(CompositeSet<E>
the CompositeSet being changed
composite
,
List<Set<E>>
all of the Set instances in this CompositeSet
sets
,
Collection<? extends E>
the collection being added
coll
)

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

public void
resolveCollision(CompositeSet<E>
the CompositeSet being modified
comp
,
Set<E>
the Set already existing in the composite
existing
,
Set<E>
the Set being added to the composite
added
,
Collection<E>
the intersection of the existing and added sets
intersects
)

Called when a Set is added to the CompositeSet and there is a collision between existing and added sets.

Method Detail

addback to summary
public boolean add(CompositeSet<E> composite, List<Set<E>> sets, E obj)

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

Parameters
composite:CompositeSet<E>

the CompositeSet being changed

sets:List<Set<E>>

all of the Set instances in this CompositeSet

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(CompositeSet<E> composite, List<Set<E>> sets, Collection<? extends E> coll)

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

Parameters
composite:CompositeSet<E>

the CompositeSet being changed

sets:List<Set<E>>

all of the Set instances in this CompositeSet

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
resolveCollisionback to summary
public void resolveCollision(CompositeSet<E> comp, Set<E> existing, Set<E> added, Collection<E> intersects)

Called when a Set is added to the CompositeSet and there is a collision between existing and added sets.

If added and existing still have any intersects after this method returns an IllegalArgumentException will be thrown.

Parameters
comp:CompositeSet<E>

the CompositeSet being modified

existing:Set<E>

the Set already existing in the composite

added:Set<E>

the Set being added to the composite

intersects:Collection<E>

the intersection of the existing and added sets