package listFW.visitor; import listFW.*; /** * Computes the number of elements in a list. * @author D. X. Nguyen * @since Copyright 2002 - DXN All rights reserved */ public class GetLength implements IListAlgo { /** * Returns 0 because the empty list has no elements. * @param host an empty list. * @param inp not used. * @return Integer(0) */ public Object emptyCase(IEmptyList host, Object inp) { return new Integer(0); } /** * Recursively computes the length of host's rest, adds 1 to the result and * returns. * @param host a non-empty list. * @param inp not used. * @return Integer */ public Object nonEmptyCase(INEList host, Object inp) { // NOTE the use of "type-casting" below. Integer restLen = (Integer)host.getRest().execute(this, null); // The execute() method for IList returns an Object in general. // However, we do know that host.getRest().execute(...) returns an // Integer. Type-casting to an Integet tells the compiler to assume // that the returned Object is an Integer. return new Integer(1 + restLen.intValue()); } }