Author: hlship
Date: Wed May 26 01:57:45 2010
New Revision: 948286

URL: http://svn.apache.org/viewvc?rev=948286&view=rev
Log:
Add a new Reducer interface and F.reduce().

Added:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Reducer.java
      - copied, changed from r948279, 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/func/FuncTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.java?rev=948286&r1=948285&r2=948286&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.java
 Wed May 26 01:57:45 2010
@@ -190,6 +190,7 @@ public class F
         return isNull.invert();
     }
 
+    /** Returns a Mapper that ignores its input value and always returns a 
predetermined result. */
     public static <S, T> Mapper<S, T> always(final T fixedResult)
     {
         return new AbstractMapper<S, T>()
@@ -202,7 +203,7 @@ public class F
     }
 
     /**
-     * Coercion factory that combines a Predicate with two {...@link Mapper}s; 
evaluating the predicate selects one of the
+     * Mapper factory that combines a Predicate with two {...@link Mapper}s; 
evaluating the predicate selects one of the
      * two mappers.
      * 
      * @param predicate
@@ -244,6 +245,7 @@ public class F
         return select(predicate, ifAccepted, rejectedMapper);
     }
 
+    /** The identity mapper simply returns the input unchanged. */
     public static <S> Mapper<S, S> identity()
     {
         return new AbstractMapper<S, S>()
@@ -255,7 +257,7 @@ public class F
         };
     }
 
-    /** Allows Coercion to boolean to be used as a Predicate. */
+    /** Allows Mapper that maps to boolean to be used as a Predicate. */
     public static <S> Predicate<S> toPredicate(final Mapper<S, Boolean> mapper)
     {
         return new AbstractPredicate<S>()
@@ -266,4 +268,27 @@ public class F
             };
         };
     }
+
+    public static <A, T> A reduce(Reducer<A, T> reducer, A initial, 
Collection<T> values)
+    {
+        Defense.notNull(reducer, "reducer");
+        Defense.notNull(values, "values");
+
+        A accumulator = initial;
+
+        for (T value : values)
+        {
+            accumulator = reducer.reduce(accumulator, value);
+        }
+
+        return accumulator;
+    }
+
+    public static Reducer<Integer, Integer> SUM_INTS = new Reducer<Integer, 
Integer>()
+    {
+        public Integer reduce(Integer accumulator, Integer value)
+        {
+            return accumulator + value;
+        };
+    };
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java?rev=948286&r1=948285&r2=948286&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
 Wed May 26 01:57:45 2010
@@ -17,8 +17,12 @@ package org.apache.tapestry5.ioc.util.fu
 /**
  * Used when filtering a collection of objects of a given type; the predicate 
is passed
  * each object in turn, and returns true to include the object in the result 
collection.
+ * <p>
+ * The {...@link F} class includes a number of Predicate factory methods.
  * 
  * @since 5.2.0
+ * @see F#filter(Predicate, java.util.List)
+ * @see F#remove(Predicate, java.util.List)
  */
 public interface Predicate<T>
 {

Copied: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Reducer.java
 (from r948279, 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java)
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Reducer.java?p2=tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Reducer.java&p1=tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java&r1=948279&r2=948286&rev=948286&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Predicate.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Reducer.java
 Wed May 26 01:57:45 2010
@@ -15,23 +15,14 @@
 package org.apache.tapestry5.ioc.util.func;
 
 /**
- * Used when filtering a collection of objects of a given type; the predicate 
is passed
- * each object in turn, and returns true to include the object in the result 
collection.
+ * A reducer takes an accumulator value and a single value from a collection 
and computes a new
+ * accumulator value.
+ * <A> type of accumulator
+ * <T> type of collection value
  * 
  * @since 5.2.0
  */
-public interface Predicate<T>
+public interface Reducer<A, T>
 {
-    /**
-     * Examines the object and determines whether to accept or reject it.
-     * 
-     * @return true to accept, false to reject
-     */
-    boolean accept(T object);
-
-    Predicate<T> invert();
-
-    Predicate<T> and(Predicate<? super T> right);
-
-    Predicate<T> or(Predicate<? super T> right);
+    A reduce(A accumulator, T value);
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/func/FuncTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/func/FuncTest.java?rev=948286&r1=948285&r2=948286&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/func/FuncTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/util/func/FuncTest.java
 Wed May 26 01:57:45 2010
@@ -226,4 +226,12 @@ public class FuncTest extends TestBase
         assertEquals(isNull.accept("foo"), false);
         assertEquals(isNotNull.accept("bar"), true);
     }
+
+    @Test
+    public void reduce()
+    {
+        int total = F.reduce(F.SUM_INTS, 0, F.map(stringToLength, "Mary", 
"had", "a", "little", "lamb"));
+
+        assertEquals(total, 18);
+    }
 }


Reply via email to