package list.dl; import java.util.NoSuchElementException; import list.dl.algo.IAlgoLeft; import list.dl.algo.IAlgoRight; import list.dl.algo.GetLeftEnd; import list.dl.algo.GetRightEnd; class DataNode extends ANode { private Object _dat; /** @SBGen Variable (,theLeft,,64) */ private DLList _leftList; /** @SBGen Variable (,theRight,,64) */ private DLList _rightList; DataNode (Object dat, DLList leftList, DLList rightList) { _dat = dat; leftList.setRightNode (this); rightList.setLeftNode (this); } /** * Pre: this node is the left node of parent. */ Object leftFirst (DLList parent) { return _dat; } /** * Pre: this node is the right node of parent. */ Object rightFirst (DLList parent) { return _dat; } /** * Pre: this node is the left node of parent. */ DLList leftRest (DLList parent) { return _leftList; } /** * Pre: this node is the right node of parent. */ DLList rightRest (DLList parent) { return _rightList; } /** * Sets the left parent link. * @param dllist */ void setLeftParent (DLList dlList) { _leftList = dlList; } /** * Sets the right parent link. * @param dllist */ void setRightParent (DLList dlList) { _rightList = dlList; } /**
Pre: this node is the left node of parentList.
Post: this node is the left node of parentList.leftRest ().
* @param dat
* @param parentList
*/
void insertLeftFront (Object dat, DLList parentList)
{
new DataNode (dat, new DLList (this, RightEndNode.Singleton), parentList);
/* "Old" style details:
DLList newList = new DLList ();
DataNode newNode = new DataNode (dat, newList, parentList);
newList.setRightNode (newNode);
newList.setLeftNode (this);
_rightList = newList;
parentList.setLeftNode (newNode);
*/
}
/**
Pre: this node is the right node of parentList.
Post: this node is the right node of parent.rightRest ().
* @param dat
* @param parentList
*/
void insertRightFront (Object dat, DLList parentList)
{
new DataNode (dat, parentList, new DLList (LeftEndNode.Singleton, this));
}
/**
Pre: this node is the left node of parent. X is _leftList.leftNode ().
Post: parent.leftNode () is X; _leftList is empty.
*/
Object remLeftFirst (DLList parent)
{
parent.setLeftNode (_leftList.leftNode());
_leftList.setRightNode (RightEndNode.Singleton);
_leftList.setLeftNode (LeftEndNode.Singleton); //_leftList's invariant re-established.
return _dat;
}
/**
Pre: this node is the right node of parent. X is _rightList.rightNode ().
Post: parent.rightNode () is X; _rightList is empty.
*/
Object remRightFirst (DLList parent)
{
parent.setRightNode (_rightList.rightNode());
_rightList.setRightNode (RightEndNode.Singleton);
_rightList.setLeftNode (LeftEndNode.Singleton); //_rightList's invariant re-established.
return _dat;
}
/**
* Pre: this node is the left node of parent.
* @param dat
*/
void setLeftFirst (Object dat, DLList parent)
{
_dat = dat;
}
/**
* Pre: this node is the right node of parent.
* @param dat
*/
void setRightFirst (Object dat, DLList parent)
{
_dat = dat;
}
/**
* Pre: this node is the left node of parent.
* @param dlList
*/
void setLeftRest (DLList dlList, DLList parent)
{
_leftList.setRightNode (RightEndNode.Singleton);//_leftList's invariant re-established.
DLList rightEnd = (DLList)dlList.executeRight (GetRightEnd.Singleton, null);
rightEnd.setRightNode (this);
}
/**
* Pre: this node is the right node of parent.
* @param dlList
*/
void setRightRest (DLList dlList, DLList parent)
{
_rightList.setLeftNode (LeftEndNode.Singleton); //_rightList's invariant re-established.
DLList leftEnd = (DLList)dlList.executeLeft (GetLeftEnd.Singleton, null);
leftEnd.setLeftNode (this);
}
/**
* Pre: this node is the left node of parentList.
* @param algo
* @param input
* @param parentList
*/
Object executeLeft (IAlgoLeft algo, Object input, DLList parentList)
{
return algo.dataNodeCase (parentList, input);
}
/**
* Pre: this node is the right node of parentList.
* @param algo
* @param input
* @param parentList
*/
Object executeRight (IAlgoRight algo, Object input, DLList parentList)
{
return algo.dataNodeCase (parentList, input);
}
}