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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRemoves and returns the last element of the list.voidremoveLast(int numberOfElements) Removes the specified amount of elements from the end of the list.protected voidremoveRange(int fromIndex, int toIndex) abstract intshrink(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, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods 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:
shrinkin 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
Removes and returns the last element of the list.- Specified by:
removeLastin interfaceList<E>- Specified by:
removeLastin interfaceSequencedCollection<E>- Returns:
- the removed element
- Throws:
NoSuchElementException- -
-
removeRange
protected void removeRange(int fromIndex, int toIndex) - Overrides:
removeRangein classAbstractList<E>
-
removeLast
public void removeLast(int numberOfElements) Removes the specified amount of elements from the end of the list.- Parameters:
numberOfElements- the number of elements to remove- Throws:
IllegalStateException- if the list does not contain enough elements
-