001 package lrs;
002
003 /**
004 * Represents the non-empty state of a LStruct.
005 *
006 * @author Dung X. Nguyen Copyright 2001 - All rights reserved.
007 * @since 10/09/01
008 */
009
010 class NENode extends ANode {
011 /**
012 * The first data element of this LRStruct.
013 */
014 private Object _dat;
015 /**
016 * The rest of this LRStruct.
017 */
018 private LRStruct _tail;
019
020 /**
021 * Initializes this NENode to contain dat and a given tail list.
022 *
023 * @param dat the data object to be stored in this NENode.
024 * @param tail the LRStruct tail of this NENode.
025 */
026 NENode(Object dat, LRStruct tail) {
027 _dat = dat;
028 _tail = tail;
029 }
030
031 LRStruct getRest(LRStruct owner) {
032 return _tail;
033 }
034
035 Object getFirst(LRStruct owner) {
036 return _dat;
037 }
038
039 LRStruct setRest(LRStruct tail, LRStruct owner) {
040 _tail = tail;
041 return owner;
042 }
043
044 LRStruct setFirst(Object first, LRStruct owner) {
045 _dat = first;
046 return owner;
047 }
048
049 /**
050 * Inserts a data object at the front of the LRStruct owner.
051 *
052 * @param dat the object to be inserted at the front.
053 * @param owner the LRS referencing this NENode.
054 */
055 LRStruct insertFront(Object dat, LRStruct owner) {
056 return owner.setHead(new NENode(dat, new LRStruct(this)));
057 /* Details:
058 // LRStruct coOwner = new LRStruct (this);
059 // NENode newNode = new NENode (dat, coOwner);
060 // owner.setHead (newNode);
061 // "old" style: owner._head = newNode - cannot be done here.
062 */
063 }
064
065 Object removeFront(LRStruct owner) {
066 owner.setHead(_tail.getHead()); // owner._head = _tail._head
067 return _dat;
068 }
069
070 /**
071 * Calls the visitor's non-empty case.
072 */
073 Object execute(IAlgo algo, Object input, LRStruct owner) {
074 return algo.nonEmptyCase(owner, input);
075 }
076 }
077