Class NodeCachingLinkedList<E>
java.lang.Object
org.apache.commons.collections4.list.AbstractLinkedList<E>
org.apache.commons.collections4.list.NodeCachingLinkedList<E>
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>
A
List implementation that stores a cache of internal Node objects
in an effort to reduce wasteful object creation.
A linked list creates one Node for each item of data added. This can result in a lot of object creation and garbage collection. This implementation seeks to avoid that by maintaining a store of cached nodes.
This implementation is suitable for long-lived lists where both add and remove are used. Short-lived lists, or lists which only grow will have worse performance using this class.
Note that this implementation is not synchronized.
- Since:
- 3.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractLinkedList
AbstractLinkedList.LinkedListIterator<E>, AbstractLinkedList.LinkedSubList<E>, AbstractLinkedList.LinkedSubListIterator<E>, AbstractLinkedList.Node<E> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intThe size of the cache.private static final intThe default value formaximumCacheSize.private AbstractLinkedList.Node<E> The first cached node, ornullif no nodes are cached.private intThe maximum size of the cache.private static final longSerialization versionFields inherited from class AbstractLinkedList
header, modCount, size -
Constructor Summary
ConstructorsConstructorDescriptionConstructor that creates.NodeCachingLinkedList(int maximumCacheSize) Constructor that species the maximum cache size.NodeCachingLinkedList(Collection<? extends E> coll) Constructor that copies the specified collection -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdds a node to the cache, if the cache isn't full.protected AbstractLinkedList.Node<E> createNode(E value) Creates a new node, either by reusing one from the cache or creating a new one.protected intGets the maximum size of the cache.protected AbstractLinkedList.Node<E> Gets a node from the cache.protected booleanChecks whether the cache is full.private voidDeserializes the data held in this object to the stream specified.protected voidRemoves all the nodes from the list, storing as many as required in the cache for reuse.protected voidremoveNode(AbstractLinkedList.Node<E> node) Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.protected voidsetMaximumCacheSize(int maximumCacheSize) Sets the maximum size of the cache.protected voidReduce the size of the cache to the maximum, if necessary.private voidSerializes the data held in this object to the stream specified.Methods inherited from class AbstractLinkedList
add, add, addAll, addAll, addFirst, addLast, addNode, addNodeAfter, addNodeBefore, clear, contains, containsAll, createHeaderNode, createSubListIterator, createSubListListIterator, doReadObject, doWriteObject, equals, get, getFirst, getLast, getNode, hashCode, indexOf, init, isEmpty, isEqualValue, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeFirst, removeLast, retainAll, set, size, subList, toArray, toArray, toString, updateNodeMethods inherited from interface Collection
parallelStream, removeIf, streamMethods inherited from interface List
replaceAll, sort, spliterator
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version- See Also:
-
DEFAULT_MAXIMUM_CACHE_SIZE
private static final int DEFAULT_MAXIMUM_CACHE_SIZEThe default value formaximumCacheSize.- See Also:
-
firstCachedNode
The first cached node, ornullif no nodes are cached. Cached nodes are stored in a singly-linked list withnextpointing to the next element. -
cacheSize
private transient int cacheSizeThe size of the cache. -
maximumCacheSize
private int maximumCacheSizeThe maximum size of the cache.
-
-
Constructor Details
-
NodeCachingLinkedList
public NodeCachingLinkedList()Constructor that creates. -
NodeCachingLinkedList
Constructor that copies the specified collection- Parameters:
coll- the collection to copy
-
NodeCachingLinkedList
public NodeCachingLinkedList(int maximumCacheSize) Constructor that species the maximum cache size.- Parameters:
maximumCacheSize- the maximum cache size
-
-
Method Details
-
getMaximumCacheSize
protected int getMaximumCacheSize()Gets the maximum size of the cache.- Returns:
- the maximum cache size
-
setMaximumCacheSize
protected void setMaximumCacheSize(int maximumCacheSize) Sets the maximum size of the cache.- Parameters:
maximumCacheSize- the new maximum cache size
-
shrinkCacheToMaximumSize
protected void shrinkCacheToMaximumSize()Reduce the size of the cache to the maximum, if necessary. -
getNodeFromCache
Gets a node from the cache. If a node is returned, then the value ofcacheSizeis decreased accordingly. The node that is returned will havenullvalues for next, previous and element.- Returns:
- a node, or
nullif there are no nodes in the cache.
-
isCacheFull
protected boolean isCacheFull()Checks whether the cache is full.- Returns:
- true if the cache is full
-
addNodeToCache
Adds a node to the cache, if the cache isn't full. The node's contents are cleared to so they can be garbage collected.- Parameters:
node- the node to add to the cache
-
createNode
Creates a new node, either by reusing one from the cache or creating a new one.- Overrides:
createNodein classAbstractLinkedList<E>- Parameters:
value- value of the new node- Returns:
- the newly created node
-
removeNode
Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.- Overrides:
removeNodein classAbstractLinkedList<E>- Parameters:
node- the node to remove
-
removeAllNodes
protected void removeAllNodes()Removes all the nodes from the list, storing as many as required in the cache for reuse.- Overrides:
removeAllNodesin classAbstractLinkedList<E>
-
writeObject
Serializes the data held in this object to the stream specified.- Parameters:
out- the output stream- Throws:
IOException- if an error occurs while writing to the stream
-
readObject
Deserializes the data held in this object to the stream specified.- Parameters:
in- the input stream- Throws:
IOException- if an error occurs while reading from the streamClassNotFoundException- if an object read from the stream can not be loaded
-