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