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