Top Description Fields Constructors Methods
org.apache.commons.collections4.list

public Class NodeCachingLinkedList<E>

extends AbstractLinkedList<E>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Collection

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

Field Summary

Modifier and TypeField and Description
private transient int
cacheSize

The size of the cache.

private static final int
private transient 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 final long
serialVersionUID

Serialization version

Inherited from org.apache.commons.collections4.list.AbstractLinkedList:
headermodCountsize

Constructor Summary

AccessConstructor and Description
public
NodeCachingLinkedList()

Constructor that creates.

public
NodeCachingLinkedList(final Collection<? extends E>
the collection to copy
coll
)

Constructor that copies the specified collection

public
NodeCachingLinkedList(final int
the maximum cache size
maximumCacheSize
)

Constructor that species the maximum cache size.

Method Summary

Modifier and TypeMethod and Description
protected void
addNodeToCache(final AbstractLinkedList.Node<E>
the node to add to the cache
node
)

Adds a node to the cache, if the cache isn't full.

protected AbstractLinkedList.Node<E>

Returns:

the newly created node
createNode
(final E
value of the new node
value
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.createNode.

Creates a new node, either by reusing one from the cache or creating a new one.
protected int

Returns:

the maximum cache size
getMaximumCacheSize
()

Gets the maximum size of the cache.

protected AbstractLinkedList.Node<E>

Returns:

a node, or null if there are no nodes in the cache.
getNodeFromCache
()

Gets a node from the cache.

protected boolean

Returns:

true if the cache is full
isCacheFull
()

Checks whether the cache is full.

private void
readObject(final ObjectInputStream
the input stream
in
)

Deserializes the data held in this object to the stream specified.

protected void
removeAllNodes()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeAllNodes.

Removes all the nodes from the list, storing as many as required in the cache for reuse.
protected void
removeNode(final AbstractLinkedList.Node<E>
the node to remove
node
)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeNode.

Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.
protected void
setMaximumCacheSize(final int
the new maximum cache size
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(final ObjectOutputStream
the output stream
out
)

Serializes the data held in this object to the stream specified.

Inherited from org.apache.commons.collections4.list.AbstractLinkedList:
addaddaddAlladdAlladdFirstaddLastaddNodeaddNodeAfteraddNodeBeforeclearcontainscontainsAllcreateHeaderNodecreateSubListIteratorcreateSubListListIteratordoReadObjectdoWriteObjectequalsgetgetFirstgetLastgetNodehashCodeindexOfinitisEmptyisEqualValueiteratorlastIndexOflistIteratorlistIteratorremoveremoveremoveAllremoveFirstremoveLastretainAllsetsizesubListtoArraytoArraytoStringupdateNode

Field Detail

cacheSizeback to summary
private transient int cacheSize

The size of the cache.

DEFAULT_MAXIMUM_CACHE_SIZEback to summary
private static final int DEFAULT_MAXIMUM_CACHE_SIZE

The default value for maximumCacheSize.

firstCachedNodeback to summary
private transient AbstractLinkedList.Node<E> firstCachedNode

The first cached node, or null if no nodes are cached. Cached nodes are stored in a singly-linked list with next pointing to the next element.

maximumCacheSizeback to summary
private int maximumCacheSize

The maximum size of the cache.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version

Constructor Detail

NodeCachingLinkedListback to summary
public NodeCachingLinkedList()

Constructor that creates.

NodeCachingLinkedListback to summary
public NodeCachingLinkedList(final Collection<? extends E> coll)

Constructor that copies the specified collection

Parameters
coll:Collection<? extends E>

the collection to copy

NodeCachingLinkedListback to summary
public NodeCachingLinkedList(final int maximumCacheSize)

Constructor that species the maximum cache size.

Parameters
maximumCacheSize:int

the maximum cache size

Method Detail

addNodeToCacheback to summary
protected void addNodeToCache(final AbstractLinkedList.Node<E> node)

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:AbstractLinkedList.Node<E>

the node to add to the cache

createNodeback to summary
protected AbstractLinkedList.Node<E> createNode(final E value)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.createNode.

Creates a new node, either by reusing one from the cache or creating a new one.

Parameters
value:E

value of the new node

Returns:AbstractLinkedList.Node<E>

the newly created node

Annotations
@Override
getMaximumCacheSizeback to summary
protected int getMaximumCacheSize()

Gets the maximum size of the cache.

Returns:int

the maximum cache size

getNodeFromCacheback to summary
protected AbstractLinkedList.Node<E> getNodeFromCache()

Gets a node from the cache. If a node is returned, then the value of cacheSize is decreased accordingly. The node that is returned will have null values for next, previous and element.

Returns:AbstractLinkedList.Node<E>

a node, or null if there are no nodes in the cache.

isCacheFullback to summary
protected boolean isCacheFull()

Checks whether the cache is full.

Returns:boolean

true if the cache is full

readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Deserializes the data held in this object to the stream specified.

Parameters
in:ObjectInputStream

the input stream

Exceptions
IOException:
if an error occurs while reading from the stream
ClassNotFoundException:
if an object read from the stream can not be loaded
removeAllNodesback to summary
protected void removeAllNodes()

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeAllNodes.

Removes all the nodes from the list, storing as many as required in the cache for reuse.

Annotations
@Override
removeNodeback to summary
protected void removeNode(final AbstractLinkedList.Node<E> node)

Overrides org.apache.commons.collections4.list.AbstractLinkedList.removeNode.

Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.

Parameters
node:AbstractLinkedList.Node<E>

the node to remove

Annotations
@Override
setMaximumCacheSizeback to summary
protected void setMaximumCacheSize(final int maximumCacheSize)

Sets the maximum size of the cache.

Parameters
maximumCacheSize:int

the new maximum cache size

shrinkCacheToMaximumSizeback to summary
protected void shrinkCacheToMaximumSize()

Reduce the size of the cache to the maximum, if necessary.

writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Serializes the data held in this object to the stream specified.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an error occurs while writing to the stream