Class ListOfUnknownSize<E>

Type Parameters:
E - the type of elements in the list.
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>
Direct Known Subclasses:
GeoTiffStore.Components, WorldFileStore.Components

public abstract class ListOfUnknownSize<E> extends AbstractSequentialList<E>
An alternative to AbstractList for implementations having a costly size() method. This class overrides some methods in a way that avoid or reduce calls to size().

Despite extending AbstractSequentialList, this class expects implementations to override the random access method get(int) instead of listIterator(int).

Since:
1.0
Version:
1.0
  • Constructor Details

    • ListOfUnknownSize

      protected ListOfUnknownSize()
      For subclass constructors.
  • Method Details

    • sizeIfKnown

      protected int sizeIfKnown()
      Returns size() if its value is already known, or a negative value if the size is still unknown. The size may become known for example if it has been cached by the subclass. In such case, some ListOfUnknownSize methods will take a more efficient path.
      Returns:
      size() if its value is already known, or any negative value if it still costly to compute.
    • size

      public int size()
      Returns the number of elements in this list. The default implementation counts the number of elements for which exists(int) returns true. 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 List<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      the number of elements in this list.
    • isEmpty

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

      protected abstract boolean exists(int index)
      Returns true if an element exists at the given index. If an element at index i exists, then all elements at index 0 to i - 1 also exist. Those elements do not need to be computed immediately if their computation is deferred.
      Parameters:
      index - the index where to verify if an element exists.
      Returns:
      true if an element exists at the given index.
    • get

      public abstract E get(int index)
      Returns the element at the specified index. Invoking this method may trig computation of the element if their computation is deferred.
      Specified by:
      get in interface List<E>
      Overrides:
      get in class AbstractSequentialList<E>
      Parameters:
      index - position of the element to get in this list.
      Returns:
      the element at the given index.
      Throws:
      IndexOutOfBoundsException - if the given index is out of bounds.
    • removeAll

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

      public ListIterator<E> listIterator(int index)
      Returns a list iterator over the elements in this list. The default implementation invokes exists(int) and get(int). Write operations are not supported.
      Specified by:
      listIterator in interface List<E>
      Specified by:
      listIterator in class AbstractSequentialList<E>
      Parameters:
      index - index of first element to be returned from the list.
      Returns:
      a list iterator over the elements in this list.
      Throws:
      IndexOutOfBoundsException - if the given index is out of bounds.
    • 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 List<E>
      Overrides:
      toArray in class AbstractCollection<E>
      Returns:
      an array containing all list 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 List<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 list elements.
    • equals

      public boolean equals(Object object)
      Returns true if the given object is also a list and the two lists have the same content. This method avoids to invoke size() on this instance.
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface List<E>
      Overrides:
      equals in class AbstractList<E>
      Parameters:
      object - the object to compare with this list.
      Returns:
      true if the two list have the same content.