package lrs; /** * Represents the non-empty state of a LStruct. * @author Dung X. Nguyen Copyright 2000 - All rights reserved. * @author Alan L. Cox * @since 02/13/01 */ class NonEmptyNode extends ANode { private Object _data; private LRStruct _tail; /** * Initializes this NonNullNode to contain dat and an empty tail list. * * @param data the data object to be stored in this NonNullNode. */ NonEmptyNode(Object data) { _data = data; _tail = new LRStruct(); } /** * Initializes this NonNullNode to contain dat and a given tail list. * * @param data the data object to be stored in this NonNullNode. * @param tail the LRStruct tail of this NonNullNode. */ NonEmptyNode(Object data, LRStruct tail) { _data = data; _tail = tail; } /** * Returns the first data object of the referencing LRStruct. * * @param owner the LRStruct referencing this NonEmptyNode. * @return the tail LRStruct of owner. */ Object getFirst(LRStruct owner) { return _data; } /** * Sets a new first data object for the referencing LRStruct. * * @param data the new data object for this NonEmptyNode. * @param owner the LRS referencing this NonEmptyNode. */ void setFirst(Object first, LRStruct owner) { _data = first; } /** * Returns the tail LRStruct of the referencing LRStruct. * * @param owner the LRStruct referencing this NonEmptyNode. * @return the tail LRStruct of owner. */ LRStruct getRest(LRStruct owner) { return _tail; } /** * Sets a new tail for the referencing LRStruct. * * @param tail the new tail for the owner LRStruct. * @param owner the LRS referencing this NonEmptyNode. */ void setRest(LRStruct tail, LRStruct owner) { _tail = tail; } /** * Inserts a data object at the front of the LRStruct owner. * * @param data the object to be inserted at the front. * @param owner the LRS referencing this NonNullNode. */ void insertFront(Object data, LRStruct owner) { owner.setHead(new NonEmptyNode(data, new LRStruct(this))); /* Details: LRStruct coOwner = new LRStruct(this); NonNullNode newNode = new NonNullNode(dat, coOwner); owner.setHead(newNode); "old" style: owner._head = newNode - cannot be done. */ } /** * Removes and returns the first data object for the referencing LRStruct. * * @param owner the LRStruct referencing this NonEmptyNode. * @return the front data of the LRStruct owner. */ Object removeFront(LRStruct owner) { owner.setHead(_tail.getHead()); // owner._head = _tail._head return _data; } /** * Executes the visitor's non-empty case and returns the output. * * @param visitor the algorithm to be executed. * @param input the input needed by the algorithm. * @param owner the LRStruct referencing this NonEmptyNode. */ Object execute(IAlgo visitor, Object input, LRStruct owner) { return visitor.nonEmptyCase(owner, input); } }