Class 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's iterator() will iterate items in the ascending order.

This implementation is not thread-safe.

Author:
cp 2003.01.29
  • Field Details Link icon

    • EMPTY_ARRAY Link icon

      protected static final Object[] EMPTY_ARRAY
      An empty immutable array.
    • m_setOrig Link icon

      protected Set<E> m_setOrig
      The underlying set (assumed immutable).
    • m_setMod Link icon

      protected Set<E> m_setMod
      The removed or retained items.
    • m_fRetained Link icon

      protected boolean m_fRetained
      Toggles between whether the modifications are removed or retained.
  • Constructor Details Link icon

    • SubSet Link icon

      public SubSet(Set<? extends E> set)
      Construct this set based on an existing set.
      Parameters:
      set - the set to base this subset on
  • Method Details Link icon

    • getOriginal Link icon

      public Set<? extends E> getOriginal()
      Determine what items were in the original set.
      Returns:
      the set used to construct this SubSet
    • isModified Link icon

      public boolean isModified()
      Determine if the set has been modified.
      Returns:
      true if any items have been removed
    • isTrackingRetained Link icon

      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 Link icon

      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 Link icon

      public Set<E> getRemoved()
      Determine what items were removed from the subset.
      Returns:
      an immutable set of removed items
    • instantiateModificationSet Link icon

      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 Link icon

      protected Set<E> ensureRetained()
      Get a mutable set of items that are retained in the subset.
      Returns:
      a mutable set of retained items
    • ensureRemoved Link icon

      protected Set<E> ensureRemoved()
      Get a mutable set of items that are removed in the subset.
      Returns:
      a mutable set of removed items
    • resolve Link icon

      public void resolve()
      Apply the changes to the underlying set ("commit").
    • reset Link icon

      public void reset()
      Discard the changes to the set ("rollback").
    • resetState Link icon

      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 set
      setMod - the set of removed or retained entries, depending on the fRetained flag
      fRetained - if true setMod contains the retained entries; otherwise the removed entries
    • iterator Link icon

      public Iterator iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • isEmpty Link icon

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
    • size Link icon

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
    • contains Link icon

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
    • containsAll Link icon

      public boolean containsAll(Collection<?> col)
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface Set<E>
      Overrides:
      containsAll in class AbstractCollection<E>
    • add Link icon

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
    • addAll Link icon

      public boolean addAll(Collection<? extends E> col)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface Set<E>
      Overrides:
      addAll in class AbstractCollection<E>
    • remove Link icon

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
    • removeAll Link icon

      public boolean removeAll(Collection<?> col)
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
      Overrides:
      removeAll in class AbstractSet<E>
    • retainAll Link icon

      public boolean retainAll(Collection<?> col)
      Specified by:
      retainAll in interface Collection<E>
      Specified by:
      retainAll in interface Set<E>
      Overrides:
      retainAll in class AbstractCollection<E>
    • clear Link icon

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
    • toArray Link icon

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • toArray Link icon

      public Object[] toArray(Object[] ao)
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
    • clone Link icon

      public Object clone()
      Clone the subset.
      Overrides:
      clone in class Object
      Returns:
      a clone of this subset
    • retainAllInternal Link icon

      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 provided colMatch collection and are not excluded.
      Parameters:
      colOuter - collection to iterate
      colMatch - each element in colOuter should be present in this collection such that (colOuter ∩ colMatch)
      setExclude - optional set of excluded elements