/** * Moves the element to the back. * * <p>Could also be implemented as remove() and unpop(), but explicitely coding might be a bit * faster. * * @param element the entry to move to the back. */ public synchronized void moveLast(final Entry element) { if (element.getValid() && element.getNext() != null) { Entry next = element.getNext(); Entry prev = element.getPrevious(); next.setPrevious(prev); if (prev != null) { prev.setNext(next); } else { first = next; } last.setNext(element); element.setPrevious(last); element.setNext(null); last = element; } }
/** * Moves the element in front. * * <p>Could also be implemented as remove() and push(), but explicitely coding might be a bit * faster. * * @param element the entry to move in front. */ public synchronized void moveFirst(final Entry element) { if (element.getValid() && element.getPrevious() != null) { Entry prev = element.getPrevious(); Entry next = element.getNext(); prev.setNext(next); if (next != null) { next.setPrevious(prev); } else { last = prev; } first.setPrevious(element); element.setNext(first); element.setPrevious(null); first = element; } }
/** * Adds an object to the start of the list and returns the entry created for said object. The * entry can later be reused for moving the entry. * * @param object the object to prepend to the start of the list. * @return an entry for use when the object should be moved. */ public synchronized Entry push(final T object) { Entry entry = new Entry(object); if (size >= maxSize) { entry.setReplaced(pop()); } if (first == null) { first = last = entry; } else { first.setPrevious(entry); entry.setNext(first); first = entry; } size++; return entry; }
/** * Removes the first element of the list and returns its content. * * @return the content of the first element of the list. */ public synchronized T unpush() { T content = null; if (first != null) { Entry element = first; first = first.getNext(); content = element.getContent(); if (first == null) { last = null; } else { first.setPrevious(null); } size--; element.invalidate(); } return content; }
/** * Removes any element of the list and returns its content. * * @param element The element to remove */ public synchronized void remove(final Entry element) { if (element == null || !element.getValid()) { return; } Entry next = element.getNext(); Entry prev = element.getPrevious(); if (next != null) { next.setPrevious(prev); } else { last = prev; } if (prev != null) { prev.setNext(next); } else { first = next; } size--; element.invalidate(); }