/** * Circular IList implementation that uses an IList to store its elements. * @author Jeff Raab */ public class CircularIList implements IList { /** IList used to store elements of this. */ protected IList elements; /** * Constructs a circular list * that uses the given list to store its elements. * @param aList list used to store the elements */ public CircularIList(IList aList) { elements = aList; } /** Returns the first position in this. */ public Position first() { return elements.first(); } /** Returns the last position in this. */ public Position last() { return elements.last(); } /** * Returns the position in this * that comes before the given position. * @param aPosition position for which previous position is desired */ public Position before(Position aPosition) { if (isFirst(aPosition)) { return last(); } return elements.before(aPosition); } /** * Returns the position in this * that comes before the given position. * @param aPosition position for which previous position is desired */ public Position after(Position aPosition) { if (isLast(aPosition)) { return first(); } return elements.after(aPosition); } /** * Returns whether or not the given position is first in this. * @param aPosition position to test */ public boolean isFirst(Position aPosition) { return elements.isFirst(aPosition); } /** * Returns whether or not the given position is last in this. * @param aPosition position to test */ public boolean isLast(Position aPosition) { return elements.isLast(aPosition); } /** * Inserts the given object at the beginning of this, * increasing the size of this by 1. * @param anObject object to insert */ public IList insertFirst(Object anObject) { elements = elements.insertFirst(anObject); return this; } /** * Inserts the given object at the end of this, * increasing the size of this by 1. * @param anObject object to insert */ public IList insertLast(Object anObject) { elements = elements.insertLast(anObject); return this; } /** * Inserts the given object before the given position in this, * increasing the size of this by 1. * @param aPosition position to insert before * @param anObject object to insert */ public IList insertBefore(Position aPosition, Object anObject) { elements = elements.insertBefore(aPosition, anObject); return this; } /** * Inserts the given object after the given position in this, * increasing the size of this by 1. * @param aPosition position to insert before * @param anObject object to insert */ public IList insertAfter(Position aPosition, Object anObject) { elements = elements.insertAfter(aPosition, anObject); return this; } /** Removes the first element of this. */ public IList removeFirst() { elements = elements.removeFirst(); return this; } /** Removes the last element of this. */ public IList removeLast() { elements = elements.removeLast(); return this; } /** * Removes the given position from this. * @param aPosition position to remove */ public IList remove(Position aPosition) { elements = elements.remove(aPosition); return this; } /** * Returns the element in the given position of this * and replaces it with the given object. * @param aPosition position of element to replace * @param anObject object to store in the given position */ public Object replaceElement(Position aPosition, Object anObject) { return elements.replaceElement(aPosition, anObject); } /** Returns the size of this. */ public int size() { return elements.size(); } /** Returns whether or not this is empty. */ public boolean isEmpty() { return elements.isEmpty(); } }