/** * Creates a new linked list. */ public LinkedList() { head = new LinkedListNode<>(); }
/** * Adds a node to the end of the list. * * @param node the node to add to the beginning of the list. */ public LinkedListNode<E> addLast(LinkedListNode<E> node) { return node.insert(head, head.previous); }
@Override public synchronized V remove(Object key) { checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED); DefaultCache.CacheObject<V> cacheObject = map.get(key); // If the object is not in cache, stop trying to remove it. if (cacheObject == null) { return null; } // remove from the hash map map.remove(key); // remove from the cache order list cacheObject.lastAccessedListNode.remove(); cacheObject.ageListNode.remove(); // remove references to linked list nodes cacheObject.ageListNode = null; cacheObject.lastAccessedListNode = null; // removed the object, so subtract its size from the total. cacheSize -= cacheObject.size; return cacheObject.object; }
/** * Returns a String representation of the linked list with a comma * delimited list of all the elements in the list. * * @return a String representation of the LinkedList. */ @Override public String toString() { LinkedListNode<E> node = head.next; StringBuilder buf = new StringBuilder(); while (node != head) { buf.append(node.toString()).append(", "); node = node.next; } return buf.toString(); } }
/** * Returns a String representation of the linked list with a comma * delimited list of all the elements in the list. * * @return a String representation of the LinkedList. */ @Override public String toString() { LinkedListNode<E> node = head.next; StringBuilder buf = new StringBuilder(); while (node != head) { buf.append(node.toString()).append(", "); node = node.next; } return buf.toString(); } }
/** * Erases all elements in the list and re-initializes it. */ public void clear() { //Remove all references in the list. LinkedListNode<E> node = getLast(); while (node != null) { node.remove(); node = getLast(); } //Re-initialize. head.next = head.previous = head; }
/** * Adds an object to the beginning of the list by automatically creating a * a new node and adding it to the beginning of the list. * * @param object the object to add to the beginning of the list. * @return the node created to wrap the object. */ public LinkedListNode<E> addFirst(E object) { return new LinkedListNode<>(object, head.next, head); }
/** * Adds a node to the beginning of the list. * * @param node the node to add to the beginning of the list. */ public LinkedListNode<E> addFirst(LinkedListNode<E> node) { return node.insert(head.next, head); }
/** * Removes the specified published item from the DB. * * @param item The published item to delete. */ public static void removePublishedItem(PublishedItem item) { String itemKey = item.getItemKey(); itemCache.remove(itemKey); synchronized (itemsPending) { itemsToDelete.addLast(item); LinkedListNode<RetryWrapper> itemToAdd = itemsPending.remove(itemKey); if (itemToAdd != null) itemToAdd.remove(); // drop from itemsToAdd linked list } }
/** * Adds an object to the end of the list by automatically creating a * a new node and adding it to the end of the list. * * @param object the object to add to the end of the list. * @return the node created to wrap the object. */ public LinkedListNode<E> addLast(E object) { return new LinkedListNode<>(object, head, head.previous); }
/** * Constructs a new linked list node. * * @param object the Object that the node represents. * @param next a reference to the next LinkedListNode in the list. * @param previous a reference to the previous LinkedListNode in the list. */ public LinkedListNode(E object, LinkedListNode<E> next, LinkedListNode<E> previous) { if (next != null && previous != null) { this.insert(next, previous); } this.object = object; }
@Override public synchronized V get(Object key) { checkNotNull(key, NULL_KEY_IS_NOT_ALLOWED); // First, clear all entries that have been in cache longer than the // maximum defined age. deleteExpiredEntries(); DefaultCache.CacheObject<V> cacheObject = map.get(key); if (cacheObject == null) { // The object didn't exist in cache, so increment cache misses. cacheMisses++; return null; } // The object exists in cache, so increment cache hits. Also, increment // the object's read count. cacheHits++; cacheObject.readCount++; // Remove the object from it's current place in the cache order list, // and re-insert it at the front of the list. cacheObject.lastAccessedListNode.remove(); lastAccessedList.addFirst((LinkedListNode<K>) cacheObject.lastAccessedListNode); return cacheObject.object; }
/** * Adds an object to the beginning of the list by automatically creating a * a new node and adding it to the beginning of the list. * * @param object the object to add to the beginning of the list. * @return the node created to wrap the object. */ public LinkedListNode<E> addFirst(E object) { return new LinkedListNode<>(object, head.next, head); }
/** * Adds a node to the end of the list. * * @param node the node to add to the beginning of the list. */ public LinkedListNode<E> addLast(LinkedListNode<E> node) { return node.insert(head, head.previous); }
itemNode.remove(); pendingIt.remove();
/** * Creates a new linked list. */ public LinkedList() { head = new LinkedListNode<>(); }
/** * Constructs a new linked list node. * * @param object the Object that the node represents. * @param next a reference to the next LinkedListNode in the list. * @param previous a reference to the previous LinkedListNode in the list. */ public LinkedListNode(E object, LinkedListNode<E> next, LinkedListNode<E> previous) { if (next != null && previous != null) { this.insert(next, previous); } this.object = object; }
/** * Creates and stores the published item in the database. * @param wrapper The published item, wrapped for retry */ private static void savePublishedItem(RetryWrapper wrapper) { boolean firstPass = (wrapper.getRetryCount() == 0); PublishedItem item = wrapper.get(); String itemKey = item.getItemKey(); itemCache.put(itemKey, item); log.debug("Added new (inbound) item to cache"); synchronized (itemsPending) { LinkedListNode<RetryWrapper> itemToReplace = itemsPending.remove(itemKey); if (itemToReplace != null) { itemToReplace.remove(); // remove duplicate from itemsToAdd linked list } LinkedListNode<RetryWrapper> listNode = firstPass ? itemsToAdd.addLast(wrapper) : itemsToAdd.addFirst(wrapper); itemsPending.put(itemKey, listNode); } // skip the flush step if this is a retry attempt if (firstPass && itemsPending.size() > MAX_ITEMS_FLUSH) { TaskEngine.getInstance().submit(new Runnable() { @Override public void run() { flushPendingItems(false); } }); } }
/** * Adds an object to the end of the list by automatically creating a * a new node and adding it to the end of the list. * * @param object the object to add to the end of the list. * @return the node created to wrap the object. */ public LinkedListNode<E> addLast(E object) { return new LinkedListNode<>(object, head, head.previous); }
/** * Adds a node to the beginning of the list. * * @param node the node to add to the beginning of the list. */ public LinkedListNode<E> addFirst(LinkedListNode<E> node) { return node.insert(head.next, head); }