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.
| Modifier and Type | Field and Description |
|---|---|
| private transient int | cacheSize
The size of the cache. |
| private static final int | DEFAULT_MAXIMUM_CACHE_SIZE
The default value for |
| private transient AbstractLinkedList. | firstCachedNode
The first cached node, or |
| private int | maximumCacheSize
The maximum size of the cache. |
| private static final long | serialVersionUID
Serialization version |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| protected void | addNodeToCache(final AbstractLinkedList.
the node to add to the cache nodeAdds a node to the cache, if the cache isn't full. |
| protected AbstractLinkedList. | Returns: the newly created nodevalue of the new node value)Overrides org. |
| protected int | |
| protected AbstractLinkedList. | Returns: a node, ornull if there are no nodes in the cache.Gets a node from the cache. |
| protected boolean | |
| 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. |
| protected void | removeNode(final AbstractLinkedList.
the node to remove nodeOverrides org. |
| protected void | setMaximumCacheSize(final int
the new maximum cache size maximumCacheSize)Sets the maximum size of the cache. |
| protected void | |
| private void | writeObject(final ObjectOutputStream
the output stream out)Serializes the data held in this object to the stream specified. |
| cacheSize | back to summary |
|---|---|
| private transient int cacheSize The size of the cache. | |
| DEFAULT_MAXIMUM_CACHE_SIZE | back to summary |
|---|---|
| private static final int DEFAULT_MAXIMUM_CACHE_SIZE The default value for | |
| firstCachedNode | back to summary |
|---|---|
| private transient AbstractLinkedList. The first cached node, or | |
| maximumCacheSize | back to summary |
|---|---|
| private int maximumCacheSize The maximum size of the cache. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version | |
| NodeCachingLinkedList | back to summary |
|---|---|
| public NodeCachingLinkedList() Constructor that creates. | |
| NodeCachingLinkedList | back to summary |
|---|---|
| public NodeCachingLinkedList(final Collection<? extends E> coll) Constructor that copies the specified collection
| |
| NodeCachingLinkedList | back to summary |
|---|---|
| public NodeCachingLinkedList(final int maximumCacheSize) Constructor that species the maximum cache size.
| |
| addNodeToCache | back to summary |
|---|---|
| protected void addNodeToCache(final AbstractLinkedList. 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.
| |
| createNode | back to summary |
|---|---|
| protected AbstractLinkedList. Overrides org. Creates a new node, either by reusing one from the cache or creating a new one.
| |
| getMaximumCacheSize | back to summary |
|---|---|
| protected int getMaximumCacheSize() Gets the maximum size of the cache.
| |
| getNodeFromCache | back to summary |
|---|---|
| protected AbstractLinkedList. Gets a node from the cache. If a node is returned, then the value of
| |
| isCacheFull | back to summary |
|---|---|
| protected boolean isCacheFull() Checks whether the cache is full.
| |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Deserializes the data held in this object to the stream specified.
| |
| removeAllNodes | back to summary |
|---|---|
| protected void removeAllNodes() Overrides org. Removes all the nodes from the list, storing as many as required in the cache for reuse.
| |
| removeNode | back to summary |
|---|---|
| protected void removeNode(final AbstractLinkedList. Overrides org. Removes the node from the list, storing it in the cache for reuse if the cache is not yet full.
| |
| setMaximumCacheSize | back to summary |
|---|---|
| protected void setMaximumCacheSize(final int maximumCacheSize) Sets the maximum size of the cache.
| |
| shrinkCacheToMaximumSize | back to summary |
|---|---|
| protected void shrinkCacheToMaximumSize() Reduce the size of the cache to the maximum, if necessary. | |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Serializes the data held in this object to the stream specified.
| |