Class CappedList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.nasengolem.util.datastructures.AbstractShrinkableList<E>
org.nasengolem.util.datastructures.CappedList<E>
- Type Parameters:
E- the type of the elements in this list
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>,RandomAccess,SequencedCollection<E>,Shrinkable
public class CappedList<E>
extends AbstractShrinkableList<E>
implements List<E>, RandomAccess, Shrinkable
Random-Access
List implementation with a fixed capacity. This list can be used whenever the maximum size of a list is known in
advance, making dynamic growth unnecessary.
Although the capacity is fixed, the list behaves as if it was dynamic. This means that methods like
add are available, but will throw an IllegalStateException if the list is full. Adding elements to a full CappedList
should therefore usually be avoided.
Some use cases for this list are datastructures that are limited due to real-world constraints or datastructures that get (partially) cleared on a regular basis.
- Author:
- Paul Steinbach
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCappedList(int capacity) Constructs a newCappedResizableListwith the specified capacity.CappedList(Collection<? extends E> collection, int capacity) Constructs aCappedResizableListwith the specified capacity.CappedList(CappedList<? extends E> cappedList) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list, if the list is not full already.booleanaddAll(int index, Collection<? extends E> c) Inserts all elements of the specified collection into this list, starting at the specified position.booleanaddAll(Collection<? extends E> c) Appends all elements of the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.intcapacity()Gets the capacity of this list.get(int index) booleanisFull()Returnstrueif this list is full.remove(int index) Removes the element at the specified position in this list.intshrink(int newSize) Shrinks the list to a new smaller size.intsize()Methods inherited from class org.nasengolem.util.datastructures.AbstractShrinkableList
removeLast, removeLast, removeRangeMethods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, subListMethods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, 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, addFirst, addLast, clear, contains, containsAll, equals, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, subList, toArray, toArray
-
Constructor Details
-
CappedList
public CappedList(int capacity) Constructs a newCappedResizableListwith the specified capacity.- Parameters:
capacity- the capacity defining the maximum number of elements the list can store- Throws:
IllegalArgumentException- if the capacity is negative
-
CappedList
Copy constructor. Constructs aCappedResizableListwith the same capacity and elements as the givenCappedResizableList.- Parameters:
cappedList- theCappedResizableListto copy- Throws:
NullPointerException- if the cappedList is null
-
CappedList
Constructs aCappedResizableListwith the specified capacity. The list contains the elements of the specified collection, in the order they are returned by the collection's iterator.- Parameters:
collection- the collection whose elements are to be placed into this listcapacity- the capacity defining the maximum number of elements the list can store- Throws:
IllegalArgumentException- if the capacity is smaller than the collections sizeNullPointerException- if the collection is null
-
-
Method Details
-
shrink
public 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- Specified by:
shrinkin classAbstractShrinkableList<E>- 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
-
get
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this list
-
set
-
isFull
public boolean isFull()Returnstrueif this list is full. This means that no further elements can be added.- Returns:
trueif this collection if full. This implementation returnssize() == capacity().
-
add
Inserts the specified element at the specified position in this list, if the list is not full already. The element currently at that position (if any) and any subsequent elements will then be shifted to the right (adds one to their indices). -
addAll
Inserts all elements of the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection's iterator. -
addAll
Appends all elements of the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
c- collection containing elements to be added to this list- Returns:
trueif this list changed as a result of the call- Throws:
NullPointerException- if the specified collection is nullIllegalStateException- if the list reached its capacity
-
remove
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). -
capacity
public int capacity()Gets the capacity of this list. The capacity is the maximum number of elements this list can store.- Returns:
- the capacity of this list
-