commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r646593 - /commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/Algorithms.java
Date Wed, 09 Apr 2008 23:20:19 GMT
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 {



Mime
View raw message