package listFW.visitor; import listFW.*; /** * Helps computes the number of elements in a list * by accumulating the length. * @author D. X. Nguyen * @since Copyright 2002 - DXN All rights reserved */ public class GetLenHelp implements IListAlgo { public static final GetLenHelp Singleton = new GetLenHelp(); private GetLenHelp() { } /** * Returns the accumulated length acc because the * empty list marks the end * of the enclosing list. * @param host an empty list. * @param acc the accumulated length of the preceding list. * @return Integer */ public Object emptyCase(IEmptyList host, Object acc) { return acc; } /** * Passes the accumulated length (1) to the rest and * asks for help to * compute the length. * @param host a non-empty list. * @param acc the accumulated of the preceding list. * @return Integer */ public Object nonEmptyCase(INEList host, Object acc) { int accLen = 1 + ((Integer)acc).intValue(); return host.getRest().execute(this, new Integer(accLen)); } }