public class ListBasedRelation extends ListOfObjects implements Relation {
public ListBasedRelation() { super(); }
public void map(Object d, Object r) {
DomainRange found = locate(d);
if (found == null) {
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) delete();
ans = dropped.getRange();
}
return ans;
}
private DomainRange locate(Object d) {
reset();
while (!atEnd()) {
DomainRange dr = (DomainRange) getItem();
if (dr.matchesDomain(d)) return dr;
next();
}
return null;
}
}