Class AbstractMostlySingletonSet<T extends @Signed Object>

java.lang.Object
org.plumelib.util.AbstractMostlySingletonSet<T>
Type Parameters:
T - the type of elements of the set
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>
Direct Known Subclasses:
IdentityMostlySingletonSet, MostlySingletonSet

public abstract class AbstractMostlySingletonSet<T extends @Signed Object> extends Object implements Set<T>
Base class for arbitrary-size sets that is very efficient (more efficient than HashSet) for 0 and 1 elements.

Does not support storing null.

This class exists because it has multiple subclasses (currently MostlySingletonSet and IdentityMostlySingletonSet).

  • Field Details

    • state

      The current state.
    • value

      protected @Nullable T extends @Signed Object value
      The current value, non-null when the state is SINGLETON.
    • set

      protected @Nullable Set<T extends @Signed Object> set
      The wrapped set, non-null when the state is ANY.
  • Constructor Details

    • AbstractMostlySingletonSet

      protected AbstractMostlySingletonSet(AbstractMostlySingletonSet.State s)
      Create an AbstractMostlySingletonSet.
      Parameters:
      s - the state
    • AbstractMostlySingletonSet

      protected AbstractMostlySingletonSet(AbstractMostlySingletonSet.State s, T v)
      Create an AbstractMostlySingletonSet.
      Parameters:
      s - the state
      v - the value
  • Method Details

    • checkRep

      protected void checkRep()
      Throw an exception if the internal representation is corrupted.
    • size

      public @org.checkerframework.checker.index.qual.NonNegative int size(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this)
      Specified by:
      size in interface Collection<T extends @Signed Object>
      Specified by:
      size in interface Set<T extends @Signed Object>
    • isEmpty

      public boolean isEmpty(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this)
      Specified by:
      isEmpty in interface Collection<T extends @Signed Object>
      Specified by:
      isEmpty in interface Set<T extends @Signed Object>
    • iterator

      @SideEffectFree public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T extends @Signed Object>
      Specified by:
      iterator in interface Iterable<T extends @Signed Object>
      Specified by:
      iterator in interface Set<T extends @Signed Object>
    • toString

      public String toString(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this)
      Overrides:
      toString in class Object
    • addAll

      public boolean addAll(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this, Collection<? extends T> c)
      Specified by:
      addAll in interface Collection<T extends @Signed Object>
      Specified by:
      addAll in interface Set<T extends @Signed Object>
    • toArray

      public @PolySigned Object[] toArray()
      Specified by:
      toArray in interface Collection<T extends @Signed Object>
      Specified by:
      toArray in interface Set<T extends @Signed Object>
    • toArray

      public <@KeyForBottom S> @Nullable S[] toArray(@PolyNull S[] a)
      Specified by:
      toArray in interface Collection<T extends @Signed Object>
      Specified by:
      toArray in interface Set<T extends @Signed Object>
    • remove

      public boolean remove(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this, @Nullable @UnknownSignedness Object o)
      Specified by:
      remove in interface Collection<T extends @Signed Object>
      Specified by:
      remove in interface Set<T extends @Signed Object>
    • containsAll

      public boolean containsAll(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this, @GuardSatisfied Collection<?> c)
      Specified by:
      containsAll in interface Collection<T extends @Signed Object>
      Specified by:
      containsAll in interface Set<T extends @Signed Object>
    • retainAll

      public boolean retainAll(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this, Collection<?> c)
      Specified by:
      retainAll in interface Collection<T extends @Signed Object>
      Specified by:
      retainAll in interface Set<T extends @Signed Object>
    • removeAll

      public boolean removeAll(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this, Collection<?> c)
      Specified by:
      removeAll in interface Collection<T extends @Signed Object>
      Specified by:
      removeAll in interface Set<T extends @Signed Object>
    • clear

      public void clear(@GuardSatisfied AbstractMostlySingletonSet<T extends @Signed Object> this)
      Specified by:
      clear in interface Collection<T extends @Signed Object>
      Specified by:
      clear in interface Set<T extends @Signed Object>