001    package lrs.visitor;
002    
003    import lrs.IAlgo;
004    import lrs.LRStruct;
005    
006    /**
007     * Return the length of the list.
008     *
009     * @author Mathias Ricken
010     */
011    public class GetLength implements IAlgo {
012        /// singleton instance
013        public static final GetLength Singleton = new GetLength();
014    
015        /// private singleton ctor
016        private GetLength() {
017        }
018    
019        /**
020         * Operates on an empty LRStruct host, given an input object.
021         *
022         * @param host an empty LRStruct.
023         * @param inp  unused
024         *
025         * @return length of the list
026         */
027        public Object emptyCase(LRStruct host, Object inp) {
028            return new Integer(0);
029        }
030    
031        /**
032         * Operates on a non-empty LRStruct host, given an input object.
033         *
034         * @param host a non-empty LRStruct.
035         * @param inp  unused
036         *
037         * @return an length of the list
038         */
039        public Object nonEmptyCase(LRStruct host, Object inp) {
040            return host.getRest().execute(new IAlgo() {
041                /**
042                 * Operates on an empty LRStruct host, given an input object.
043                 *
044                 * @param host an empty LRStruct.
045                 * @param inp  input object needed by this IAlgo.
046                 *
047                 * @return an appropriate output object.
048                 */
049                public Object emptyCase(LRStruct host, Object inp) {
050                    return inp;
051                }
052    
053                /**
054                 * Operates on a non-empty LRStruct host, given an input object.
055                 *
056                 * @param host a non-empty LRStruct.
057                 * @param inp  input object needed by this IAlgo.
058                 *
059                 * @return an appropriate output object.
060                 */
061                public Object nonEmptyCase(LRStruct host, Object inp) {
062                    return host.getRest().execute(this, new Integer(((Integer)inp).intValue() + 1));
063                }
064            }, new Integer(1));
065        }
066    }