package list.dl; import java.util.NoSuchElementException; import list.dl.algo.IAlgoLeft; import list.dl.algo.IAlgoRight; /** * Singleton pattern representing the left end node of a doubly-linked list. */ class LeftEndNode extends ANode { final public static LeftEndNode Singleton = new LeftEndNode (); private LeftEndNode () { } Object leftFirst (DLList parent) // Pre: this node is the left node of parent. { throw new NoSuchElementException ("LeftEndNode.leftFirst: Left end has no data."); } DLList leftRest (DLList parent) // Pre: this node is the left node of parent. { throw new NoSuchElementException ("LeftEndNode.leftRest: Left end has no left tail."); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param parent * @return */ Object rightFirst (DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.rightFirst ()"); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param parent * @return */ DLList rightRest(DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.rightRest ()"); } /** * Throws IllegalStateException. * @param dllist */ void setLeftParent (DLList dllist) { throw new IllegalStateException ("Should never call LeftEnd.setLeftParent ()"); } /** * Does nothing. * @param dllist */ void setRightParent (DLList dllist) { } /**
   Pre: this node is the left node of parent.
   Post: this node is the left node of parent.leftRest ().
   
* @param dat */ void insertLeftFront (Object dat, DLList parent) { new DataNode (dat, new DLList (), parent); /* Old style: DLList newList = new DLList (); DataNode newNode = new DataNode (dat, newList, parent); newList.setRightNode (newNode); parent.setLeftNode (newNode); */ } Object remLeftFirst (DLList parent) // Pre: this node is the left node of parent. { throw new NoSuchElementException ("LeftEndNode.remLeftFirst: Left end has no data."); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param dat the element to be inserted to the right front of the parent. * @param parent the DLList that holds this ANode. */ void insertRightFront (Object dat, DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.insertRightFront ()"); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param parent the DLList that holds this ANode. * @return */ Object remRightFirst (DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.remRightFirst ()"); } /** * Throws NoSuchElementException. * @param dat */ void setLeftFirst (Object dat, DLList parent) // Pre: this node is the left node of parent. { throw new NoSuchElementException ("LeftEndNode.setLeftFirst: Left end has no data."); } /** * Throws NoSuchElementException. * @param dllist */ void setLeftRest (DLList dllist, DLList parent) // Pre: this node is the left node of parent. { throw new NoSuchElementException ("LeftEndNode.setLeftRest: Left end has no left tail."); } /** * @param algo * @param input * @param parentList */ Object executeLeft (IAlgoLeft algo, Object input, DLList parentList) // Pre: this node is the left node of parentList. { return algo.leftEndCase (parentList, input); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param dat the element that is to replace the current first element on the left of the parent list. * @param parent the DLList that holds this ANode. */ void setRightFirst (Object dat, DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.setRightFirst ()"); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param dllist the new tail that is to replace the current one. * @param parent the DLList that holds this ANode. */ void setRightRest (DLList dllist, DLList parent) { throw new IllegalStateException ("Should never call LeftEnd.setRightRest ()"); } /** * Throws IllegalStateException. Can never be a the right node of a DLList * @param algo * @param input * @param parentList * @return */ Object executeRight (IAlgoRight algo, Object input, DLList parentList) { throw new IllegalStateException ("Should never call LeftEnd.executeRight ()"); } }