001 package lrs.visitor; 002 003 import lrs.IAlgo; 004 import lrs.LRStruct; 005 import model.ILambda; 006 007 /** 008 * Applies the lambda specified as input to all elements in the LRStruct. 009 * 010 * @author Mathias Ricken 011 */ 012 public class Apply implements IAlgo { 013 /// singleton instance 014 public static final Apply Singleton = new Apply(); 015 016 /// private singleton ctor 017 private Apply() { 018 } 019 020 /** 021 * Operates on an empty LRStruct host, given an input object. 022 * 023 * @param host an empty LRStruct. 024 * @param inp ILambda to apply 025 * 026 * @return always null 027 */ 028 public Object emptyCase(LRStruct host, Object inp) { 029 return null; 030 } 031 032 /** 033 * Operates on a non-empty LRStruct host, given an input object. 034 * 035 * @param host a non-empty LRStruct. 036 * @param inp ILambda to apply 037 * 038 * @return always null 039 */ 040 public Object nonEmptyCase(LRStruct host, Object inp) { 041 ((ILambda)inp).apply(host.getFirst()); 042 return host.getRest().execute(this, inp); 043 } 044 }