You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2008/04/10 01:20:19 UTC

svn commit: r646593 - /commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java

Author: mbenson
Date: Wed Apr  9 16:20:17 2008
New Revision: 646593

URL: http://svn.apache.org/viewvc?rev=646593&view=rev
Log:
add a variation of the recurse method that allows the client to specify the continuing function type

Modified:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java

Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java?rev=646593&r1=646592&r2=646593&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java Wed Apr  9 16:20:17 2008
@@ -453,14 +453,26 @@
      * @return final result
      */
     public static final Object recurse(Function function) {
+        return recurse(function, function == null ? null : function.getClass());
+    }
+
+    /**
+     * Tail recursion for {@link Function functions}. If the {@link Function}
+     * returns another function of type <code>functionType</code>, that function
+     * is executed. Functions are executed until a non function value or a
+     * function of a different type is returned.
+     * @param function initial Function
+     * @param functionType as long as result is an instance, keep processing.
+     * @return final result
+     */
+    public static final Object recurse(Function function, Class functionType) {
         Object result = null;
-        Class recursiveFunctionClass = function.getClass();
 
         // if the function returns another function, execute it. stop executing
         // when the function doesn't return another function of the same type.
         while (true) {
             result = function.evaluate();
-            if (recursiveFunctionClass.isInstance(result)) {
+            if (functionType.isInstance(result)) {
                 function = (Function) result;
                 continue;
             } else {