/** * Deserializes the data held in this object to the stream specified. * * @param in the input stream * @throws IOException if an error occurs while reading from the stream * @throws ClassNotFoundException if an object read from the stream can not be loaded */ private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); doReadObject(in); }
/** * Serializes the data held in this object to the stream specified. * * @param out the output stream * @throws IOException if an error occurs while writing to the stream */ private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); doWriteObject(out); }
/** * Reduce the size of the cache to the maximum, if necessary. */ protected void shrinkCacheToMaximumSize() { // Rich Dougherty: This could be more efficient. while (cacheSize > maximumCacheSize) { getNodeFromCache(); } }
/** * Removes the node from the list, storing it in the cache for reuse * if the cache is not yet full. * * @param node the node to remove */ @Override protected void removeNode(final Node<E> node) { super.removeNode(node); addNodeToCache(node); }
/** * Constructor that species the maximum cache size. * * @param maximumCacheSize the maximum cache size */ public NodeCachingLinkedList(final int maximumCacheSize) { super(); this.maximumCacheSize = maximumCacheSize; init(); // must call init() as use super(); }
/** * Sets the maximum size of the cache. * * @param maximumCacheSize the new maximum cache size */ protected void setMaximumCacheSize(final int maximumCacheSize) { this.maximumCacheSize = maximumCacheSize; shrinkCacheToMaximumSize(); }
/** * 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. * * @param node the node to add to the cache */ protected void addNodeToCache(final Node<E> node) { if (isCacheFull()) { // don't cache the node. return; } // clear the node's contents and add it to the cache. final Node<E> nextCachedNode = firstCachedNode; node.previous = null; node.next = nextCachedNode; node.setValue(null); firstCachedNode = node; cacheSize++; }
/** * Removes all the nodes from the list, storing as many as required in the * cache for reuse. * */ @Override protected void removeAllNodes() { // Add the removed nodes to the cache, then remove the rest. // We can add them to the cache before removing them, since // {@link AbstractLinkedList.removeAllNodes()} removes the // nodes by removing references directly from {@link #header}. final int numberOfNodesToCache = Math.min(size, maximumCacheSize - cacheSize); Node<E> node = header.next; for (int currentIndex = 0; currentIndex < numberOfNodesToCache; currentIndex++) { final Node<E> oldNode = node; node = node.next; addNodeToCache(oldNode); } super.removeAllNodes(); }
/** * Creates a new node, either by reusing one from the cache or creating * a new one. * * @param value value of the new node * @return the newly created node */ @Override protected Node<E> createNode(final E value) { final Node<E> cachedNode = getNodeFromCache(); if (cachedNode == null) { return super.createNode(value); } cachedNode.setValue(value); return cachedNode; }