/** * 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 */ protected void removeNode(Node node) { super.removeNode(node); addNodeToCache(node); }
/** * Deserializes the data held in this object to the stream specified. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); doReadObject(in); }
/** * Serializes the data held in this object to the stream specified. */ private void writeObject(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(); } }
/** * Sets the maximum size of the cache. * * @param maximumCacheSize the new maximum cache size */ protected void setMaximumCacheSize(int maximumCacheSize) { this.maximumCacheSize = maximumCacheSize; shrinkCacheToMaximumSize(); }
/** * Constructor that species the maximum cache size. * * @param maximumCacheSize the maximum cache size */ public NodeCachingLinkedList(int maximumCacheSize) { super(); this.maximumCacheSize = maximumCacheSize; init(); // must call init() as use super(); }
/** * 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(Node node) { if (isCacheFull()) { // don't cache the node. return; } // clear the node's contents and add it to the cache. Node nextCachedNode = firstCachedNode; node.previous = null; node.next = nextCachedNode; node.setValue(null); firstCachedNode = node; cacheSize++; }
/** * 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(); } }
/** * Sets the maximum size of the cache. * * @param maximumCacheSize the new maximum cache size */ protected void setMaximumCacheSize(int maximumCacheSize) { this.maximumCacheSize = maximumCacheSize; shrinkCacheToMaximumSize(); }
/** * Constructor that species the maximum cache size. * * @param maximumCacheSize the maximum cache size */ public NodeCachingLinkedList(int maximumCacheSize) { super(); this.maximumCacheSize = maximumCacheSize; init(); // must call init() as use super(); }
/** * 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(Node node) { if (isCacheFull()) { // don't cache the node. return; } // clear the node's contents and add it to the cache. Node nextCachedNode = firstCachedNode; node.previous = null; node.next = nextCachedNode; node.setValue(null); firstCachedNode = node; cacheSize++; }
/** * 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 */ protected Node createNode(Object value) { Node cachedNode = getNodeFromCache(); if (cachedNode == null) { return super.createNode(value); } else { cachedNode.setValue(value); return cachedNode; } }
/** * 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 */ protected void removeNode(Node node) { super.removeNode(node); addNodeToCache(node); }
/** * Sets the maximum size of the cache. * * @param maximumCacheSize the new maximum cache size */ protected void setMaximumCacheSize(int maximumCacheSize) { this.maximumCacheSize = maximumCacheSize; shrinkCacheToMaximumSize(); }
/** * Deserializes the data held in this object to the stream specified. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); doReadObject(in); }
/** * Constructor that species the maximum cache size. * * @param maximumCacheSize the maximum cache size */ public NodeCachingLinkedList(int maximumCacheSize) { super(); this.maximumCacheSize = maximumCacheSize; init(); // must call init() as use super(); }
/** * Serializes the data held in this object to the stream specified. */ private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); doWriteObject(out); }
/** * 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(Node node) { if (isCacheFull()) { // don't cache the node. return; } // clear the node's contents and add it to the cache. Node nextCachedNode = firstCachedNode; node.previous = null; node.next = nextCachedNode; node.setValue(null); firstCachedNode = node; cacheSize++; }
/** * 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 */ protected Node createNode(Object value) { Node cachedNode = getNodeFromCache(); if (cachedNode == null) { return super.createNode(value); } else { cachedNode.setValue(value); return cachedNode; } }
/** * Removes all the nodes from the list, storing as many as required in the * cache for reuse. * */ 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}. int numberOfNodesToCache = Math.min(size, maximumCacheSize - cacheSize); Node node = header.next; for (int currentIndex = 0; currentIndex < numberOfNodesToCache; currentIndex++) { Node oldNode = node; node = node.next; addNodeToCache(oldNode); } super.removeAllNodes(); }