/** * 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 last element of the list and returns its content. * * @return the content of the last element of the list. */ public synchronized T pop() { T content = null; if (last != null) { Entry element = last; last = last.getPrevious(); content = element.getContent(); if (last == null) { first = null; } else { last.setNext(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(); }