package lrs.visitor; import lrs.LRStruct; import lrs.IAlgo; /** * Computes the length of a LRStruct host. The length of an empty LRStruct is 0. * @author Dung X. Nguyen */ public class Length implements IAlgo { /** * Singleton pattern. */ public final static Length Singleton = new Length (); /** * Hidden from all external client to implement the Singleton pattern. */ private Length () { } /** * Returns Integer (0). * @param host the LRStruct whose length is to be computed. * @param input not needed. * @return Integer (0). */ public Object forEmpty (LRStruct host, Object input) { return new Integer (0); } /** * Recurs on the tail of this LRStruct and add one to the result. * @param host the LRStruct whose length is to be computed. * @param input not needed. * @return an Integer representing 1 + the length of the tail of this LRStruct. */ public Object forNonEmpty (LRStruct host, Object input) { Integer tLen = (Integer)host.getRest().execute(this, null); return new Integer (1 + tLen.intValue()); } }