package rac; /** * This policy creates an ordered integer insertion and retrieval behavior in * the RAC. The data supplied for storage and the that exists in the RAC when * this policy is installed are assumed to be of the Integer class. The data * is stored in order in the internal list (O(n) storage) with the largest value * at the top of the list. The largest value data is retrieved (O(1) retrieval). * @author S. Wong */ public class OrderedIntegerPolicy extends ARACPolicy { /** * A IRACVisitor used to perform an ordered insert into the internal list of * the RAC. The dataCase() method searches for a data value that is smaller * than the supplied data's value and then does a host.insertPrevious(). * The stopCase() simply does a host.insertPrevious(). */ private IRACVisitor orderedIntegerVisitor = new IRACVisitor () { public Object stopCase(RACStopNode host, Object param) { host.insertPrevious(param); return null; } public Object dataCase(RACDataNode host, Object param) { int pv = ((Integer)param).intValue(); int hv = ((Integer)host.getData()).intValue(); if (pv > hv) { host.insertPrevious(param); } else { host.getNext().execute(this, param); } return null; } }; /** * The RAC will dispatch its store(data) method through this method to * perform the data store operation. * @param data The data to be stored in the RAC. * @param stopNode The RACStopNode that is the head/tail of this internal * doubly-linked circular data list of the RACont. */ public void store(Object data, RACStopNode stopNode) { stopNode.getNext().execute(orderedIntegerVisitor, data); } /** * The RACont will dispatch its retrieve() method through this method to * perform the data retrieval operation. * @param stopNode The RACStopNode that is the head/tail of this internal * doubly-linked circular data list of the RACont. * @return The data value found. */ public Object retrieve(RACStopNode stopNode) { return stopNode.getNext().removeNode(); } /** * Performs a bubble sort of the data (largest value at the front of the * list). No type checking is performed. * @param stopNode The RACStopNode that is the head/tail of this internal * doubly-linked circular data list of the RACont. */ public void init(RACStopNode stopNode) { stopNode.getNext().execute(sortVisitor, null); } /** * A IRACVisitor used to initially sort the data in internal list of the RAC. * The data is sorted so that the largest value is at the front of the list. * A bubble sort routine is implemented. No type checking is performed. */ private IRACVisitor sortVisitor = new IRACVisitor() { public Object stopCase(RACStopNode host, Object param ) { return null; } public Object dataCase(RACDataNode host, Object param ) { host.execute(sortVisitorHelp, null); host.getNext().execute(this, null); return null; } }; /** * The IRACVisitor helper algorithm for implementing the bubble sort. */ private IRACVisitor sortVisitorHelp = new IRACVisitor() { public Object stopCase(RACStopNode host, Object param ) { return new Integer(Integer.MIN_VALUE); } public Object dataCase(RACDataNode host, Object param ) { Integer tmpInt = (Integer) host.getNext().execute(this, null); if(tmpInt.intValue() <((Integer) host.getData()).intValue()) { return host.getData(); } else { ((RACDataNode)host.getNext()).setData(host.getData()); host.setData(tmpInt); return tmpInt; } } }; }