Class SetOfUnknownSize<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.apache.sis.internal.util.SetOfUnknownSize<E>
Type Parameters:
E - the type of elements in the set.
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>
Direct Known Subclasses:
DerivedSet, LazySet, OperationMethodSet

public abstract class SetOfUnknownSize<E> extends AbstractSet<E>
An alternative to AbstractSet for implementations having a costly size() method. This class overrides some methods in a way that avoid or reduce calls to size().
Since:
0.7
Version:
1.0
  • Constructor Details

    • SetOfUnknownSize

      protected SetOfUnknownSize()
      For subclass constructors.
  • Method Details

    • isSizeKnown

      protected boolean isSizeKnown()
      Returns true if the size() method is cheap. This is sometimes the case when size() has already been invoked and the subclasses cached the result.
      Returns:
      true if the size() method is cheap.
    • isEmpty

      public boolean isEmpty()
      Returns true if this set is empty. This method avoids to invoke size() unless it is cheap.
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:
      true if this set is empty.
    • size

      public int size()
      Returns the number of elements in this set. The default implementation counts the number of elements returned by the iterator. Subclasses are encouraged to cache this value if they know that the underlying storage is immutable.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      the number of elements in this set.
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes elements of the given collection from this set. This method avoids to invoke size().
      Specified by:
      removeAll in interface Collection<E>
      Specified by:
      removeAll in interface Set<E>
      Overrides:
      removeAll in class AbstractSet<E>
      Parameters:
      c - the collection containing elements to remove.
      Returns:
      true if at least one element has been removed.
    • spliterator

      public Spliterator<E> spliterator()
      Creates a Spliterator without knowledge of collection size.
      Returns:
      a Spliterator over the elements in this collection.
    • toArray

      public Object[] toArray()
      Returns the elements in an array.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Returns:
      an array containing all set elements.
    • toArray

      public <T> T[] toArray(T[] array)
      Returns the elements in the given array, or in a new array of the same type if it was necessary to allocate more space.
      Specified by:
      toArray in interface Collection<E>
      Specified by:
      toArray in interface Set<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Type Parameters:
      T - the type array elements.
      Parameters:
      array - where to store the elements.
      Returns:
      an array containing all set elements.
    • toArray

      static <T> T[] toArray(Iterator<?> it, T[] array, boolean trimToSize)
      Implementation of the public toArray() methods without call to size().
    • equals

      public boolean equals(Object object)
      Returns true if the given object is also a set and the two sets have the same content. This method avoids to invoke size() on this instance (but it still call that method on the other instance).
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface Set<E>
      Overrides:
      equals in class AbstractSet<E>
      Parameters:
      object - the object to compare with this set.
      Returns:
      true if the two set have the same content.