package listFW.visitor; import listFW.*; /** * Merges the host and the input parameter inp into a sorted list. * Assumes host and inp are IList containing Integer sorted in ascending order. * @author DXN */ public class Merge implements IListAlgo { private listFW.IListFactory _fact; /** * This helper assumes the input parameter inp is a non=empty list. */ private IListAlgo mergeHelp = new IListAlgo() { public Object emptyCase(IEmptyList host, Object inp){ return inp; } /** * Now both host and inp are non-empty. */ public Object nonEmptyCase(INEList host, Object inp){ Integer f = (Integer)host.getFirst(); Integer i = (Integer)((INEList)inp).getFirst(); return f.intValue() < i.intValue()? _fact.makeNEList(f, (IList)host.getRest().execute(Merge.this, inp)): _fact.makeNEList(i, (IList)host.execute(Merge.this, ((INEList)inp).getRest())); } }; public Merge(IListFactory f) { _fact = f; } public Object emptyCase(IEmptyList host, Object inp){ return inp; } public Object nonEmptyCase(INEList host, Object inp){ return ((IList)inp).execute(mergeHelp, host); } }