Class SmallSet<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

    final class SmallSet<T>
    extends java.util.AbstractSet<T>
    An immutable set of at most two elements, optimized for speed compared to a generic set implementation.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  SmallSet.IteratorImpl<T>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T element1
      The first element of this set, maybe null.
      private T element2
      The second element of this set, maybe null.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      (package private) SmallSet()
      Constructs an empty set.
      (package private) SmallSet​(T element)
      Constructs a set with exactly one element.
      private SmallSet​(T element1, T element2)
      Constructs a new SmallSet.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Iterator<T> iterator()  
      int size()  
      (package private) java.util.Set<T> union​(SmallSet<T> otherSet)
      Returns the union of this set and of the given set.
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode, removeAll
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, contains, containsAll, isEmpty, remove, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        add, addAll, clear, contains, containsAll, isEmpty, remove, retainAll, spliterator, toArray, toArray
    • Field Detail

      • element1

        private final T element1
        The first element of this set, maybe null.
      • element2

        private final T element2
        The second element of this set, maybe null. If element1 is null then this field must be null, otherwise it must be different from element1.
    • Constructor Detail

      • SmallSet

        SmallSet()
        Constructs an empty set.
      • SmallSet

        SmallSet​(T element)
        Constructs a set with exactly one element.
        Parameters:
        element - the unique set element.
      • SmallSet

        private SmallSet​(T element1,
                         T element2)
        Constructs a new SmallSet.
        Parameters:
        element1 - see element1.
        element2 - see element2.
    • Method Detail

      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.Set<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.Set<T>
        Specified by:
        size in class java.util.AbstractCollection<T>
      • union

        java.util.Set<T> union​(SmallSet<T> otherSet)
        Returns the union of this set and of the given set.
        Parameters:
        otherSet - another small set.
        Returns:
        the union of this set and of otherSet.