public class

NodeCachingLinkedList

extends AbstractLinkedList
implements Serializable
java.lang.Object
   ↳ org.apache.commons.collections.list.AbstractLinkedList
     ↳ org.apache.commons.collections.list.NodeCachingLinkedList

Class Overview

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.

Summary

Constants
int DEFAULT_MAXIMUM_CACHE_SIZE The default value for maximumCacheSize.
Fields
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.
[Expand]
Inherited Fields
From class org.apache.commons.collections.list.AbstractLinkedList
Public Constructors
NodeCachingLinkedList()
Constructor that creates.
NodeCachingLinkedList(Collection coll)
Constructor that copies the specified collection
NodeCachingLinkedList(int maximumCacheSize)
Constructor that species the maximum cache size.
Protected Methods
void addNodeToCache(AbstractLinkedList.Node node)
Adds a node to the cache, if the cache isn't full.
AbstractLinkedList.Node createNode(Object value)
Creates a new node, either by reusing one from the cache or creating a new one.
int getMaximumCacheSize()
Gets the maximum size of the cache.
AbstractLinkedList.Node getNodeFromCache()
Gets a node from the cache.
boolean isCacheFull()
Checks whether the cache is full.
void removeAllNodes()
Removes all the nodes from the list, storing as many as required in the cache for reuse.
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.
void setMaximumCacheSize(int maximumCacheSize)
Sets the maximum size of the cache.
void shrinkCacheToMaximumSize()
Reduce the size of the cache to the maximum, if necessary.
[Expand]
Inherited Methods
From class org.apache.commons.collections.list.AbstractLinkedList
From class java.lang.Object
From interface java.lang.Iterable
From interface java.util.Collection
From interface java.util.List

Constants

protected static final int DEFAULT_MAXIMUM_CACHE_SIZE

The default value for maximumCacheSize.

Constant Value: 20 (0x00000014)

Fields

protected int cacheSize

The size of the cache.

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.

protected int maximumCacheSize

The maximum size of the cache.

Public Constructors

public NodeCachingLinkedList ()

Constructor that creates.

public NodeCachingLinkedList (Collection coll)

Constructor that copies the specified collection

Parameters
coll the collection to copy

public NodeCachingLinkedList (int maximumCacheSize)

Constructor that species the maximum cache size.

Parameters
maximumCacheSize the maximum cache size

Protected Methods

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

protected AbstractLinkedList.Node createNode (Object value)

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

Parameters
value value of the new node
Returns
  • the newly created node

protected int getMaximumCacheSize ()

Gets the maximum size of the cache.

Returns
  • the maximum cache size

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.

protected boolean isCacheFull ()

Checks whether the cache is full.

Returns
  • true if 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.

Parameters
node the node to remove

protected void setMaximumCacheSize (int maximumCacheSize)

Sets the maximum size of the cache.

Parameters
maximumCacheSize the new maximum cache size

protected void shrinkCacheToMaximumSize ()

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