public class Relation { protected ListOfObjects assoc; public Relation() { assoc = new ListOfObjects(); } public void map(Object d, Object r) { DomainRange found = locate(d); if (found == null) { assoc.append(new DomainRange(d, r)); // placement is arbitrary } else found.setRange(r); // replace what was there } public Object lookup(Object d) { DomainRange dr = locate(d); if (dr != null) return dr.getRange(); else return null; } public Object drop(Object d) { Object ans = null; if (locate(d) != null) { // NB: count on marker positioned correctly DomainRange dropped = (DomainRange) assoc.delete(); ans = dropped.getRange(); } return ans; } private DomainRange locate(Object d) { assoc.reset(); while (!assoc.atEnd()) { DomainRange dr = (DomainRange) assoc.getItem(); if (dr.matchesDomain(d)) return dr; assoc.next(); } return null; } public static void selfTest() { Object d1 = new String("d1"); Object d2 = new String("d2"); Object d3 = new String("d3"); Object r1 = new String("r1"); Object r2 = new String("r2"); Relation r = new Relation(); r.map(d1, r2); r.map(d2, r2); r.map(d3, r1); if (r.lookup(d1) == r2) System.out.println("d1,r2 correct"); else System.out.println("incorrect"); if (r.lookup(d2) == r2) System.out.println("d2,r2 correct"); else System.out.println("incorrect"); if (r.lookup(d3) == r1) System.out.println("d3,r1 correct"); else System.out.println("incorrect"); r.map(d3,r2); r.drop(d1); if (r.lookup(d3) == r2) System.out.println("d3,r2 correct"); else System.out.println("incorrect"); if (r.lookup(d1) == null) System.out.println("d3 dropped correct"); else System.out.println("incorrect"); } public static void main(String args[]) { selfTest(); } }