Class AbstractShrinkableList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.nasengolem.util.datastructures.AbstractShrinkableList<E>
- Type Parameters:
E
- the type of elements in this list
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
,SequencedCollection<E>
,Shrinkable
- Direct Known Subclasses:
CappedList
public abstract class AbstractShrinkableList<E>
extends AbstractList<E>
implements List<E>, Shrinkable
This class extends the
AbstractList
skeleton by implementing Shrinkable
.
This means, that the end of the list can be cut off in constant time. The following idioms utilize the
shrink(int)
method to increase performance when deleting the last elements of the list.
list.removeLast(numberOfElements); list.subList(from, size()).clear(); list.clear();
- Author:
- Paul Steinbach
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
removeLast
(int numberOfElements) Removes the last n elements from the list.protected void
removeRange
(int fromIndex, int toIndex) abstract int
shrink
(int newSize) Shrinks the list to a new smaller size.Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, get, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, set, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, 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.util.List
add, add, addAll, addAll, addFirst, addLast, clear, contains, containsAll, equals, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeFirst, replaceAll, retainAll, reversed, set, size, sort, spliterator, subList, toArray, toArray
-
Constructor Details
-
AbstractShrinkableList
protected AbstractShrinkableList()
-
-
Method Details
-
shrink
public abstract int shrink(int newSize) Shrinks the list to a new smaller size. All elements at indices beyond the new size won't be visible anymore.- Specified by:
shrink
in interfaceShrinkable
- Parameters:
newSize
- the new size of the list- Returns:
- the old size of the list
- Throws:
IllegalArgumentException
- if the new size is negative or greater than the current size
-
removeLast
- Specified by:
removeLast
in interfaceList<E>
- Specified by:
removeLast
in interfaceSequencedCollection<E>
-
removeRange
protected void removeRange(int fromIndex, int toIndex) - Overrides:
removeRange
in classAbstractList<E>
-
removeLast
public void removeLast(int numberOfElements) Removes the last n elements from the list.- Parameters:
numberOfElements
- the number of elements to remove- Throws:
IllegalStateException
- if the list does not contain enough elements
-