Class CompositeSet<E>
- java.lang.Object
-
- org.apache.commons.collections4.set.CompositeSet<E>
-
- Type Parameters:
E
- the type of the elements in this set
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Set<E>
public class CompositeSet<E> extends java.lang.Object implements java.util.Set<E>, java.io.Serializable
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
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CompositeSet.SetMutator<E>
Define callbacks for mutation operations.
-
Constructor Summary
Constructors Constructor Description CompositeSet()
Create an empty CompositeSet.CompositeSet(java.util.Set<E> set)
Create a CompositeSet with justset
composited.CompositeSet(java.util.Set<E>... sets)
Create a composite set with sets as the initial set of composited Sets.
-
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 SetMutator strategy is specified.boolean
addAll(java.util.Collection<? extends E> coll)
Adds a collection of elements to this composite, throwing UnsupportedOperationException unless a SetMutator strategy is specified.void
addComposited(java.util.Set<E> set)
Add a Set to this composite.void
addComposited(java.util.Set<E>... sets)
Add these Sets to the list of sets in this compositevoid
addComposited(java.util.Set<E> set1, java.util.Set<E> set2)
Add these Sets to the list of sets in this composite.void
clear()
Removes all of the elements from this composite set.boolean
contains(java.lang.Object obj)
Checks whether this composite set contains the object.boolean
containsAll(java.util.Collection<?> coll)
Checks whether this composite contains all the elements in the specified collection.boolean
equals(java.lang.Object obj)
java.util.List<java.util.Set<E>>
getSets()
Gets the sets being decorated.int
hashCode()
boolean
isEmpty()
Checks whether this composite set is empty.java.util.Iterator<E>
iterator()
Gets an iterator over all the sets in this composite.boolean
remove(java.lang.Object obj)
If aCollectionMutator
is defined for this CompositeSet then this method will be called anyway.boolean
removeAll(java.util.Collection<?> coll)
Removes the elements in the specified collection from this composite set.void
removeComposited(java.util.Set<E> set)
Removes a set from those being decorated in this composite.boolean
removeIf(java.util.function.Predicate<? super E> filter)
boolean
retainAll(java.util.Collection<?> coll)
Retains all the elements in the specified collection in this composite set, removing all others.void
setMutator(CompositeSet.SetMutator<E> mutator)
Specify a SetMutator strategy instance to handle changes.int
size()
Gets the size of this composite set.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.Set<E>
toSet()
Returns a new Set containing all of the elements.
-
-
-
Constructor Detail
-
CompositeSet
public CompositeSet()
Create an empty CompositeSet.
-
CompositeSet
public CompositeSet(java.util.Set<E> set)
Create a CompositeSet with justset
composited.- Parameters:
set
- the initial set in the composite
-
CompositeSet
public CompositeSet(java.util.Set<E>... sets)
Create a composite set with sets as the initial set of composited Sets.- Parameters:
sets
- the initial sets in the composite
-
-
Method Detail
-
size
public int size()
Gets the size of this composite set.This implementation calls
size()
on each set.
-
isEmpty
public boolean isEmpty()
Checks whether this composite set is empty.This implementation calls
isEmpty()
on each set.
-
contains
public boolean contains(java.lang.Object obj)
Checks whether this composite set contains the object.This implementation calls
contains()
on each set.
-
iterator
public java.util.Iterator<E> iterator()
Gets an iterator over all the sets in this composite.This implementation uses an
IteratorChain
.- Specified by:
iterator
in interfacejava.util.Collection<E>
- Specified by:
iterator
in interfacejava.lang.Iterable<E>
- Specified by:
iterator
in interfacejava.util.Set<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.
-
toArray
public <T> T[] toArray(T[] array)
Returns an object array, populating the supplied array if possible. SeeCollection
interface for full details.
-
add
public boolean add(E obj)
Adds an object to the collection, throwing UnsupportedOperationException unless a SetMutator strategy is specified.- Specified by:
add
in interfacejava.util.Collection<E>
- Specified by:
add
in interfacejava.util.Set<E>
- Parameters:
obj
- the object to add- Returns:
true
if the collection was modified- Throws:
java.lang.UnsupportedOperationException
- if SetMutator hasn't been set or 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)
If aCollectionMutator
is defined for this CompositeSet then this method will be called anyway.
-
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.
-
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
Adds a collection of elements to this composite, throwing UnsupportedOperationException unless a SetMutator strategy is specified.- Specified by:
addAll
in interfacejava.util.Collection<E>
- Specified by:
addAll
in interfacejava.util.Set<E>
- Parameters:
coll
- the collection to add- Returns:
- true if the composite was modified
- Throws:
java.lang.UnsupportedOperationException
- if SetMutator hasn't been set or 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
-
removeIf
public boolean removeIf(java.util.function.Predicate<? super E> filter)
- Specified by:
removeIf
in interfacejava.util.Collection<E>
- Since:
- 4.4
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
Removes the elements in the specified collection from this composite set.This implementation calls
removeAll
on each collection.
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
Retains all the elements in the specified collection in this composite set, removing all others.This implementation calls
retainAll()
on each collection.
-
clear
public void clear()
Removes all of the elements from this composite set.This implementation calls
clear()
on each set.
-
setMutator
public void setMutator(CompositeSet.SetMutator<E> mutator)
Specify a SetMutator strategy instance to handle changes.- Parameters:
mutator
- the mutator to use
-
addComposited
public void addComposited(java.util.Set<E> set)
Add a Set to this composite.- Parameters:
set
- the set to add- Throws:
java.lang.IllegalArgumentException
- if a SetMutator is set, but fails to resolve a collisionjava.lang.UnsupportedOperationException
- if there is no SetMutator setjava.lang.NullPointerException
- ifset
is null- See Also:
CompositeSet.SetMutator
-
addComposited
public void addComposited(java.util.Set<E> set1, java.util.Set<E> set2)
Add these Sets to the list of sets in this composite.- Parameters:
set1
- the first Set to be appended to the compositeset2
- the second Set to be appended to the composite
-
addComposited
public void addComposited(java.util.Set<E>... sets)
Add these Sets to the list of sets in this composite- Parameters:
sets
- the Sets to be appended to the composite
-
removeComposited
public void removeComposited(java.util.Set<E> set)
Removes a set from those being decorated in this composite.- Parameters:
set
- set to be removed
-
toSet
public java.util.Set<E> toSet()
Returns a new Set containing all of the elements.- Returns:
- A new HashSet containing all of the elements in this composite. The new collection is not backed by this composite.
-
getSets
public java.util.List<java.util.Set<E>> getSets()
Gets the sets being decorated.- Returns:
- Unmodifiable list of all sets in this composite.
-
equals
public boolean equals(java.lang.Object obj)
-
-