// (c) Tuomas J. Lukka package org.fenfire.functional; import org.fenfire.util.*; import org.fenfire.swamp.*; import java.util.*; /** Helper classes for testing the Functional API. * All subclasses have the impure static member "counter" * which counts the number of invocations. While testing * whether something is cached, some impurity is vital :( * The member is static because the instances are * created inside the Functional API. */ public class FunctionalTest { public static boolean dbg = true; private static void p(String s) { System.out.println(s); } /** A function that appends its constructor parameter to * its parameter. */ static public class G0 implements PureFunction { static public int counter; String param; public G0(String param) { this.param = param; } public Object f(Object o) { counter++; if(dbg) p("G0.f - "+this.param+": "+o); return ((String)o) + this.param; } } /** A function that calls another function with its parameter * and appends its constructor parameter to the result. */ static public class G1 implements PureFunction { static public int counter = 0; String param; Function f; public G1(String param, Function f) { this.param = param; this.f = f; } public Object f(Object o) { counter++; Object other = this.f.f(o); if(dbg) p("G1.f - "+this.param+" - "+other+": "+o); return ((String)(other)) + this.param; } } /** A node function that appends its constructor parameter to * its parameter. */ static public class G0_Node implements PureNodeFunction { static public int counter = 0; String param; public G0_Node(String param) { this.param = param; } public Object f(ConstGraph g, Object o) { counter++; return ((String)o) + this.param; } } /** A node function that calls another function with its parameter * and appends its constructor parameter to the result. */ static public class G1_Node implements PureNodeFunction { static public int counter = 0; String param; NodeFunction f; public G1_Node(String param, NodeFunction f) { this.param = param; this.f = f; } public Object f(ConstGraph g, Object o) { counter++; return ((String)(this.f.f(g, o))) + this.param; } } /** A node function returning set of third components * in triples of two given nodes. */ static public class TripleSet_Node implements PureNodeFunction { Object o2; public TripleSet_Node(Object o2) { this.o2 = o2; } public Object f(ConstGraph g, Object o) { Set res = new HashSet(); for(Iterator iter = g.findN_11X_Iter(o, o2); iter.hasNext();) res.add(iter.next()); return res; } } }