package containers; import java.util.Enumeration; import lrs.*; // for LRStruct and IAlgo /** * A container implemented using the LRStruct. * * Version 2. * * @author Alan L. Cox * @since 02/22/01 */ public class LRSContainer implements IContainer { private LRStruct _lrs = new LRStruct(); /** * If there is an object associated with key * then this object is returned else null is returned. * * @param key the Object key to find * @return the Object associated with the key */ public Object find(Object key) { return _lrs.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return null; } public Object nonEmptyCase(LRStruct host, Object input) { KeyValuePair pair = (KeyValuePair) host.getFirst(); if (input.equals(pair.getKey())) return pair.getValue(); else return host.getRest().execute(this, input); } }, key); } /** * Afterwards, find(key) returns null, and if there is * an object associated with key then this object is * returned else null is returned. * * @param key the Object key to remove * @return the Object associated with the key */ public Object remove(Object key) { return _lrs.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return null; } public Object nonEmptyCase(LRStruct host, Object input) { KeyValuePair pair = (KeyValuePair) host.getFirst(); if (input.equals(pair.getKey())) { host.removeFront(); return pair.getValue(); } else return host.getRest().execute(this, input); } }, key); } /** * (key, value) is stored in this container with no * duplication and such that find(key) returns value. * * @param key the Object key from the (key,value) to insert * @param value the Object value from the (key,value) to insert */ public void insert(Object key, Object value) { _lrs.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { host.insertFront(input); return null; } public Object nonEmptyCase(LRStruct host, Object input) { KeyValuePair pair = (KeyValuePair) host.getFirst(); if (((KeyValuePair) input).getKey().equals(pair.getKey())) host.setFirst(input); else host.getRest().execute(this, input); return null; } }, new KeyValuePair(key, value)); } /** * Returns an Enumeration of the container. * * @return an Enumeration of the container */ public Enumeration enumeration() { /** * We can't use an anonymous inner class to implement * this Enumeration because of the constructor. */ class LRSEnumeration implements Enumeration { private LRStruct _next; LRSEnumeration(LRStruct lrs) { _next = lrs; } public boolean hasMoreElements() { return Boolean.TRUE == _next.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return Boolean.FALSE; } public Object nonEmptyCase(LRStruct host, Object input) { return Boolean.TRUE; } }, null); } public Object nextElement() { return _next.execute(new IAlgo() { public Object emptyCase(LRStruct host, Object input) { return null; } public Object nonEmptyCase(LRStruct host, Object input) { Object object = _next.getFirst(); _next = _next.getRest(); return object; } }, null); } } return new LRSEnumeration(_lrs); } }