public class NodeCachingLinkedList<E> extends AbstractLinkedList<E> implements java.io.Serializable
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.
AbstractLinkedList.LinkedListIterator<E>, AbstractLinkedList.LinkedSubList<E>, AbstractLinkedList.LinkedSubListIterator<E>, AbstractLinkedList.Node<E>
Modifier and Type | Field and Description |
---|---|
private int |
cacheSize
The size of the cache.
|
private static int |
DEFAULT_MAXIMUM_CACHE_SIZE
The default value for
maximumCacheSize . |
private AbstractLinkedList.Node<E> |
firstCachedNode
The first cached node, or
null if no nodes are cached. |
private int |
maximumCacheSize
The maximum size of the cache.
|
private static long |
serialVersionUID
Serialization version
|
header, modCount, size
Constructor and Description |
---|
NodeCachingLinkedList()
Constructor that creates.
|
NodeCachingLinkedList(java.util.Collection<? extends E> coll)
Constructor that copies the specified collection
|
NodeCachingLinkedList(int maximumCacheSize)
Constructor that species the maximum cache size.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addNodeToCache(AbstractLinkedList.Node<E> node)
Adds 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 int |
getMaximumCacheSize()
Gets the maximum size of the cache.
|
protected AbstractLinkedList.Node<E> |
getNodeFromCache()
Gets a node from the cache.
|
protected boolean |
isCacheFull()
Checks whether the cache is full.
|
private void |
readObject(java.io.ObjectInputStream in)
Deserializes the data held in this object to the stream specified.
|
protected void |
removeAllNodes()
Removes all the nodes from the list, storing as many as required in the
cache for reuse.
|
protected void |
removeNode(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 void |
setMaximumCacheSize(int maximumCacheSize)
Sets the maximum size of the cache.
|
protected void |
shrinkCacheToMaximumSize()
Reduce the size of the cache to the maximum, if necessary.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Serializes the data held in this object to the stream specified.
|
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, updateNode
private static final long serialVersionUID
private static final int DEFAULT_MAXIMUM_CACHE_SIZE
maximumCacheSize
.private transient AbstractLinkedList.Node<E> firstCachedNode
null
if no nodes are cached.
Cached nodes are stored in a singly-linked list with
next
pointing to the next element.private transient int cacheSize
private int maximumCacheSize
public NodeCachingLinkedList()
public NodeCachingLinkedList(java.util.Collection<? extends E> coll)
coll
- the collection to copypublic NodeCachingLinkedList(int maximumCacheSize)
maximumCacheSize
- the maximum cache sizeprotected int getMaximumCacheSize()
protected void setMaximumCacheSize(int maximumCacheSize)
maximumCacheSize
- the new maximum cache sizeprotected void shrinkCacheToMaximumSize()
protected AbstractLinkedList.Node<E> getNodeFromCache()
cacheSize
is decreased accordingly. The node that is returned
will have null
values for next, previous and element.null
if there are no nodes in the cache.protected boolean isCacheFull()
protected void addNodeToCache(AbstractLinkedList.Node<E> node)
node
- the node to add to the cacheprotected AbstractLinkedList.Node<E> createNode(E value)
createNode
in class AbstractLinkedList<E>
value
- value of the new nodeprotected void removeNode(AbstractLinkedList.Node<E> node)
removeNode
in class AbstractLinkedList<E>
node
- the node to removeprotected void removeAllNodes()
removeAllNodes
in class AbstractLinkedList<E>
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
- if an error occurs while writing to the streamprivate void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- the input streamjava.io.IOException
- if an error occurs while reading from the streamjava.lang.ClassNotFoundException
- if an object read from the stream can not be loaded