org.apache.commons.collections.list

Class NodeCachingLinkedList

Implemented Interfaces:
List, Serializable

public class NodeCachingLinkedList
extends AbstractLinkedList
implements Serializable

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.

Version:
$Revision: 1.7 $ $Date: 2004/04/20 23:46:50 $
Authors:
Jeff Varszegi
Rich Dougherty
Phil Steitz
Stephen Colebourne
Since:
Commons Collections 3.0

Nested Class Summary

Nested classes/interfaces inherited from class org.apache.commons.collections.list.AbstractLinkedList

AbstractLinkedList.LinkedListIterator, AbstractLinkedList.LinkedSubList, AbstractLinkedList.LinkedSubListIterator, AbstractLinkedList.Node

Field Summary

protected static int
DEFAULT_MAXIMUM_CACHE_SIZE
The default value for maximumCacheSize.
protected int
cacheSize
The size of the cache.
protected AbstractLinkedList.Node
firstCachedNode
The first cached node, or null if no nodes are cached.
protected int
maximumCacheSize
The maximum size of the cache.

Fields inherited from class org.apache.commons.collections.list.AbstractLinkedList

header, modCount, size

Constructor Summary

NodeCachingLinkedList()
Constructor that creates.
NodeCachingLinkedList(Collection coll)
Constructor that copies the specified collection
NodeCachingLinkedList(int maximumCacheSize)
Constructor that species the maximum cache size.

Method Summary

protected void
addNodeToCache(AbstractLinkedList.Node node)
Adds a node to the cache, if the cache isn't full.
protected AbstractLinkedList.Node
createNode(Object 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
getNodeFromCache()
Gets a node from the cache.
protected boolean
isCacheFull()
Checks whether the cache is full.
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 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.

Methods inherited from class org.apache.commons.collections.list.AbstractLinkedList

add, add, addAll, addAll, addFirst, addLast, addNode, addNodeAfter, addNodeBefore, clear, contains, containsAll, createHeaderNode, createNode, createSubListIterator, createSubListListIterator, doReadObject, doWriteObject, equals, get, getFirst, getLast, getNode, hashCode, indexOf, init, isEmpty, isEqualValue, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeAllNodes, removeFirst, removeLast, removeNode, retainAll, set, size, subList, toArray, toArray, toString, updateNode

Field Details

DEFAULT_MAXIMUM_CACHE_SIZE

protected static final int DEFAULT_MAXIMUM_CACHE_SIZE
Field Value:
20

cacheSize

protected int cacheSize
The size of the cache.

firstCachedNode

protected AbstractLinkedList.Node 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.

maximumCacheSize

protected int maximumCacheSize
The maximum size of the cache.

Constructor Details

NodeCachingLinkedList

public NodeCachingLinkedList()
Constructor that creates.

NodeCachingLinkedList

public NodeCachingLinkedList(Collection coll)
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

addNodeToCache

protected void addNodeToCache(AbstractLinkedList.Node 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 - the node to add to the cache

createNode

protected AbstractLinkedList.Node createNode(Object value)
Creates a new node, either by reusing one from the cache or creating a new one.
Overrides:
createNode in interface AbstractLinkedList
Parameters:
value - value of the new node
Returns:
the newly created node

getMaximumCacheSize

protected int getMaximumCacheSize()
Gets the maximum size of the cache.
Returns:
the maximum cache size

getNodeFromCache

protected AbstractLinkedList.Node 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:
a node, or null if there are no nodes in the cache.

isCacheFull

protected boolean isCacheFull()
Checks whether the cache is full.
Returns:
true if the cache is full

removeAllNodes

protected void removeAllNodes()
Removes all the nodes from the list, storing as many as required in the cache for reuse.
Overrides:
removeAllNodes in interface AbstractLinkedList

removeNode

protected void removeNode(AbstractLinkedList.Node node)
Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.
Overrides:
removeNode in interface AbstractLinkedList
Parameters:
node - the node to remove

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.

Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.