import tester.Tester; /* ...................................................................................... : : v : +---------------------------+ : | IListVisitor | : +---------------------------+ : | forMT() | : | forCons(Cons that) | : +---------------------------+ : / \ : --- : | : --------------------------------------------------------------------------- : | | | : +----------------------------------+ +-------------------------------+ +-------------------------------------+ : | OccursVisitor R:Integer | | SubstVisito R:IList | | RemtVisitor R:IList | : +----------------------------------+ +-------------------------------+ +-------------------------------------+ : | T item | | T rem | | Object rem | : +----------------------------------+ | T add | +-------------------------------------+ : | R forMT(){ | +-------------------------------+ | R forMT(){ | : | return new Integer(0);} | | R forMT(){ | | return new Integer(0);} | : | | | return new MT();} | | | : | R forCons(Cons that){ | | | | R forCons(Cons that){ | : | if (that.first.equals(item)) | | R forCons(Cons that){ | | if (that.first.equals(rem)){ | : | return new Integer(((Integer) | | if (that.first.equals(rem)){ | | return that.rest.accept(this); | : | (that.rest.accept(this))) | | that.first = add; | | else{ | : | .intValue() + 1); | | that.rest.accept(this); | | return new Top(that.t, | : | else | | return that; } | | (IList)that.rest.accept(this));} | : | return that.rest.accept(this); | | else{ | | } | : | } | | that.rest.accept(this); | +-------------------------------------+ : +----------------------------------+ | return that;} } | : +-------------------------------+ : : +-----------------------------------------------+ : | IList |<---------------------+ : +-----------------------------------------------+ | : | abstract R accept(IListVisitor ask) |....................................................... +-----------------------------------------------+ | / \ | --- | | | ------------------------------------- | | | | +----------------------------------+ +----------------------------------+ | | MT | | Cons | | +----------------------------------+ +----------------------------------+ | | R accept(IListVisitor ask){ | | T first | | | return ask.forBot();} | | IList rest |--+ +----------------------------------+ +-+--------------------------------+ | R accept(IListVisitor ask){ | | return ask.forTop();} | +----------------------------------+ */ interface IListVisitor{ R forMT(); R forCons(Cons that); } class OccursVisitorT implements IListVisitor{ T item; OccursVisitorT(T item){ this.item = item; } public Integer forMT(){ return new Integer(0); } public Integer forCons(Cons that){ if (that.first.equals(item)) return new Integer(((Integer)(that.rest.accept(this))).intValue() + 1); else return (Integer)that.rest.accept(this); } } class SubstVisitorT implements IListVisitor, T>{ T rem; T add; SubstVisitorT(T rem, T add){ this.rem = rem; this.add = add; } public IList forMT(){ return new MT(); } public IList forCons(Cons that){ if (that.first.equals(rem)){ that.first = add; that.rest.accept(this); return (IList)that; } else{ that.rest.accept(this); return that; } } } class RemVisitorT implements IListVisitor, T>{ T rem; RemVisitorT(T rem){ this.rem = rem; } public IList forMT(){ return new MT(); } public IList forCons(Cons that){ if (that.first.equals(rem)) return (IList)that.rest.accept(this); else return new Cons(that.first, (IList)that.rest.accept(this)); } } interface IList{ abstract R accept(IListVisitor ask); } class MT implements IList{ MT(){} public R accept(IListVisitor ask){ return ask.forMT(); } } class Cons implements IList{ T first; IList rest; Cons(T first, IList rest){ this.first = first; this.rest = rest; } public R accept(IListVisitor ask){ return ask.forCons(this); } } interface IListMan{ int addItem(T item); int remItem(T rem); int substItem(T rem, T add); int occItem(T item); } class ManageIListMan implements IListMan{ IList alist = new MT(); public int addItem(T item){ alist = new Cons(item, alist); return occItem(item); } public int remItem(T rem){ alist = (IList)alist.accept(new RemVisitorT(rem)); return occItem(rem); } public int substItem(T rem, T add){ alist = (IList)alist.accept(new SubstVisitorT(rem, add)); return occItem(add); } public int occItem(T item){ return ((Integer)alist.accept(new OccursVisitorT(item))).intValue(); } } class ExamplesT{ IListMan makeACATM(){ IListMan tmp = new ManageIListMan(); tmp.addItem("anchovy"); tmp.addItem("cheese"); tmp.addItem("anchovy"); tmp.addItem("tuna"); tmp.addItem("mushroom"); return tmp; } IListMan makeCTM(){ IListMan tmp = new ManageIListMan(); tmp.addItem("cheese"); tmp.addItem("tuna"); tmp.addItem("mushroom"); return tmp; } IListMan makeOCTM(){ IListMan tmp = new ManageIListMan(); tmp.addItem("olive"); tmp.addItem("cheese"); tmp.addItem("tuna"); tmp.addItem("mushroom"); return tmp; } IListMan makeOCOTM(){ IListMan tmp = new ManageIListMan(); tmp.addItem("olive"); tmp.addItem("cheese"); tmp.addItem("olive"); tmp.addItem("tuna"); tmp.addItem("mushroom"); return tmp; } public void testAdd(Tester t){ IListMan tmp = makeCTM(); t.checkExpect(tmp.addItem("olive"), 1); t.checkExpect(tmp.addItem("mushroom"), 2); } public void testRem(Tester t){ IListMan tmp = makeOCTM(); t.checkExpect(tmp.remItem("olive"), 0); tmp = makeACATM(); t.checkExpect(tmp.remItem("anchovy"), 0); } public void testSubst(Tester t){ IListMan tmp = makeOCOTM(); t.checkExpect(tmp.substItem("anchovy", "olive"), 2); tmp = makeACATM(); t.checkExpect(tmp.substItem("anchovy", "olive"), 2); } public void testOccur(Tester t){ IListMan tmp = makeOCOTM(); t.checkExpect(tmp.occItem("mushroom"), 1); t.checkExpect(tmp.occItem("olive"), 2); } }