Package org.apache.sis.internal.util
Class ListOfUnknownSize<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.AbstractSequentialList<E>
org.apache.sis.internal.util.ListOfUnknownSize<E>
- Type Parameters:
E
- the type of elements in the list.
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
,SequencedCollection<E>
- Direct Known Subclasses:
GeoTiffStore.Components
,WorldFileStore.Components
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
The iterator returned byAbstractList.listIterator()
. -
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if the given object is also a list and the two lists have the same content.protected abstract boolean
exists
(int index) Returnstrue
if an element exists at the given index.abstract E
get
(int index) Returns the element at the specified index.boolean
isEmpty()
Returnstrue
if this list is empty.listIterator
(int index) Returns a list iterator over the elements in this list.boolean
removeAll
(Collection<?> c) Removes elements of the given collection from this list.int
size()
Returns the number of elements in this list.protected int
Returnssize()
if its value is already known, or a negative value if the size is still unknown.Creates aSpliterator
without knowledge of collection size.Object[]
toArray()
Returns the elements in an array.<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.Methods inherited from class java.util.AbstractList
add, clear, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, retainAll, 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.util.List
addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, remove, removeFirst, removeLast, replaceAll, retainAll, reversed, sort
-
Constructor Details
-
ListOfUnknownSize
protected ListOfUnknownSize()For subclass constructors.
-
-
Method Details
-
sizeIfKnown
protected int sizeIfKnown()Returnssize()
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, someListOfUnknownSize
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 whichexists(int)
returnstrue
. Subclasses are encouraged to cache this value if they know that the underlying storage is immutable.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceList<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this list.
-
isEmpty
public boolean isEmpty()Returnstrue
if this list is empty. This method avoids to invokesize()
unless it is cheap.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
true
if this list is empty.
-
exists
protected abstract boolean exists(int index) Returnstrue
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
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 interfaceList<E>
- Overrides:
get
in classAbstractSequentialList<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
Removes elements of the given collection from this list. This method avoids to invokesize()
.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollection<E>
- Parameters:
c
- the collection containing elements to remove.- Returns:
true
if at least one element has been removed.
-
listIterator
Returns a list iterator over the elements in this list. The default implementation invokesexists(int)
andget(int)
. Write operations are not supported.- Specified by:
listIterator
in interfaceList<E>
- Specified by:
listIterator
in classAbstractSequentialList<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
Creates aSpliterator
without knowledge of collection size.- Returns:
- a
Spliterator
over the elements in this collection.
-
toArray
Returns the elements in an array.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<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 interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Type Parameters:
T
- the type array elements.- Parameters:
array
- where to store the elements.- Returns:
- an array containing all list elements.
-
equals
Returnstrue
if the given object is also a list and the two lists have the same content. This method avoids to invokesize()
on this instance.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceList<E>
- Overrides:
equals
in classAbstractList<E>
- Parameters:
object
- the object to compare with this list.- Returns:
true
if the two list have the same content.
-