Class NodeCachingLinkedList<E>

All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, List<E>

public class NodeCachingLinkedList<E> extends AbstractLinkedList<E> 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.

See Also:
  • Constructor Details

    • NodeCachingLinkedList

      Constructor that creates.
    • NodeCachingLinkedList

      public NodeCachingLinkedList(Collection<? extends E> coll)
      Constructor that copies the specified collection
      coll - the collection to copy
    • NodeCachingLinkedList

      public NodeCachingLinkedList(int maximumCacheSize)
      Constructor that species the maximum cache size.
      maximumCacheSize - the maximum cache size
  • Method Details

    • addNodeToCache

      protected void addNodeToCache(AbstractLinkedList.Node<E> node)
      Adds a node to the cache, if the cache isn't full. The node's contents are cleared, so they can be garbage collected.
      node - the node to add to the cache
    • createNode

      protected AbstractLinkedList.Node<E> createNode(E value)
      Creates a new node, either by reusing one from the cache or creating a new one.
      createNode in class AbstractLinkedList<E>
      value - value of the new node
      the newly created node
    • getMaximumCacheSize

      protected int getMaximumCacheSize()
      Gets the maximum size of the cache.
      the maximum cache size
    • 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.
      a node, or null if there are no nodes in the cache.
    • isCacheFull

      protected boolean isCacheFull()
      Checks whether the cache is full.
      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.
      removeAllNodes in class AbstractLinkedList<E>
    • removeNode

      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.
      removeNode in class AbstractLinkedList<E>
      node - the node to remove
    • setMaximumCacheSize

      protected void setMaximumCacheSize(int maximumCacheSize)
      Sets the maximum size of the cache.
      maximumCacheSize - the new maximum cache size
    • shrinkCacheToMaximumSize

      protected void shrinkCacheToMaximumSize()
      Reduce the size of the cache to the maximum, if necessary.