Package com.tangosol.util
Class SubSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- com.tangosol.util.SubSet<E>
-
- All Implemented Interfaces:
Cloneable
,Iterable<E>
,Collection<E>
,Set<E>
public class SubSet<E> extends AbstractSet<E> implements Cloneable
Implements a set which is based on another set, which is assumed to be immutable. The SubSet is assumed to be a subset of the underlying set, and optimizes for both remove and retain operations. If the underlying (original) set is sorted, the SubSet'siterator()
will iterate items in the ascending order.This implementation is not thread-safe.
- Author:
- cp 2003.01.29
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
SubSet.SubSetIterator
Iterator for the contents of a subset in the "removed" mode.
-
Field Summary
Fields Modifier and Type Field Description protected static Object[]
EMPTY_ARRAY
An empty immutable array.protected boolean
m_fRetained
Toggles between whether the modifications are removed or retained.protected Set<E>
m_setMod
The removed or retained items.protected Set<E>
m_setOrig
The underlying set (assumed immutable).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
boolean
addAll(Collection<? extends E> col)
void
clear()
Object
clone()
Clone the subset.boolean
contains(Object o)
boolean
containsAll(Collection<?> col)
protected Set<E>
ensureRemoved()
Get a mutable set of items that are removed in the subset.protected Set<E>
ensureRetained()
Get a mutable set of items that are retained in the subset.Set<? extends E>
getOriginal()
Determine what items were in the original set.Set<E>
getRemoved()
Determine what items were removed from the subset.Set<E>
getRetained()
Determine what items were added to the subset.protected Set<E>
instantiateModificationSet(int cSize)
Instantiate a new modification set containing either removed or retained items.boolean
isEmpty()
boolean
isModified()
Determine if the set has been modified.boolean
isTrackingRetained()
Determine if the SubSet is tracking retained items versus removed items.Iterator
iterator()
boolean
remove(Object o)
boolean
removeAll(Collection<?> col)
void
reset()
Discard the changes to the set ("rollback").protected void
resetState(Set setOrig, Set setMod, boolean fRetained)
Reset the state of the subset according to the specified parameters.void
resolve()
Apply the changes to the underlying set ("commit").boolean
retainAll(Collection<?> col)
protected void
retainAllInternal(Collection colOuter, Collection colMatch, Set setExclude)
Instantiate a new retained set with all elements in the specified collection that also exist in the providedcolMatch
collection and are not excluded.int
size()
Object[]
toArray()
Object[]
toArray(Object[] ao)
-
Methods inherited from class java.util.AbstractSet
equals, hashCode
-
Methods inherited from class java.util.AbstractCollection
toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
spliterator
-
-
-
-
Field Detail
-
EMPTY_ARRAY
protected static final Object[] EMPTY_ARRAY
An empty immutable array.
-
m_fRetained
protected boolean m_fRetained
Toggles between whether the modifications are removed or retained.
-
-
Method Detail
-
getOriginal
public Set<? extends E> getOriginal()
Determine what items were in the original set.- Returns:
- the set used to construct this SubSet
-
isModified
public boolean isModified()
Determine if the set has been modified.- Returns:
- true if any items have been removed
-
isTrackingRetained
public boolean isTrackingRetained()
Determine if the SubSet is tracking retained items versus removed items.- Returns:
- true if the SubSet is tracking just the retained items, false if the SubSet is tracking just the removed items
-
getRetained
public Set<E> getRetained()
Determine what items were added to the subset. Do not modify the returned set.- Returns:
- a set of retained items
-
getRemoved
public Set<E> getRemoved()
Determine what items were removed from the subset.- Returns:
- an immutable set of removed items
-
instantiateModificationSet
protected Set<E> instantiateModificationSet(int cSize)
Instantiate a new modification set containing either removed or retained items.- Parameters:
cSize
- an initial size of the modification set
-
ensureRetained
protected Set<E> ensureRetained()
Get a mutable set of items that are retained in the subset.- Returns:
- a mutable set of retained items
-
ensureRemoved
protected Set<E> ensureRemoved()
Get a mutable set of items that are removed in the subset.- Returns:
- a mutable set of removed items
-
resolve
public void resolve()
Apply the changes to the underlying set ("commit").
-
reset
public void reset()
Discard the changes to the set ("rollback").
-
resetState
protected void resetState(Set setOrig, Set setMod, boolean fRetained)
Reset the state of the subset according to the specified parameters.- Parameters:
setOrig
- the new original setsetMod
- the set of removed or retained entries, depending on thefRetained
flagfRetained
- if truesetMod
contains the retained entries; otherwise the removed entries
-
iterator
public Iterator iterator()
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
size
public int size()
- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
contains
public boolean contains(Object o)
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
containsAll
public boolean containsAll(Collection<?> col)
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
- Overrides:
containsAll
in classAbstractCollection<E>
-
add
public boolean add(E e)
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
-
addAll
public boolean addAll(Collection<? extends E> col)
- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceSet<E>
- Overrides:
addAll
in classAbstractCollection<E>
-
remove
public boolean remove(Object o)
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
removeAll
public boolean removeAll(Collection<?> col)
- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceSet<E>
- Overrides:
removeAll
in classAbstractSet<E>
-
retainAll
public boolean retainAll(Collection<?> col)
- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Overrides:
retainAll
in classAbstractCollection<E>
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toArray
public Object[] toArray(Object[] ao)
- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
clone
public Object clone()
Clone the subset.
-
retainAllInternal
protected void retainAllInternal(Collection colOuter, Collection colMatch, Set setExclude)
Instantiate a new retained set with all elements in the specified collection that also exist in the providedcolMatch
collection and are not excluded.- Parameters:
colOuter
- collection to iteratecolMatch
- each element in colOuter should be present in this collection such that(colOuter ∩ colMatch)
setExclude
- optional set of excluded elements
-
-