/* * Created on Oct 10, 2003 */ package listFW.visitor; import listFW.*; /** * Computes the length of a list using anonymous inner class. * * @author root */ public class Average implements IListAlgo { private static int _accLen; private static int _accSum; public static final Average Singleton = new Average(); private Average() { } /** * Returns 0 because the empty list has no elements. * @param host an empty list. * @param nu not used. * @return Integer */ public Object emptyCase(IEmptyList host, Object nu) { return new Integer(0); } /** * @param host a non-empty list. * @param nu not used. * @return Integer */ public Object nonEmptyCase(INEList host, Object nu) { _accLen = 1; _accSum = ((Integer)host.getFirst ()).intValue (); // call helper as an anonymous inner class: return host.getRest().execute(new IListAlgo() { public Object emptyCase(IEmptyList h, Object nu) { return new Integer (_accSum/_accLen); } public Object nonEmptyCase(INEList h, Object nu) { _accLen += 1; _accSum += ((Integer)h.getFirst ()).intValue (); return h.getRest ().execute (this, null); } }, // end of anonymous inner class. null); } }