tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r951266 - in /tapestry/tapestry5/trunk/tapestry-func/src: main/java/org/apache/tapestry5/func/ test/java/org/apache/tapestry5/func/
Date Fri, 04 Jun 2010 05:06:44 GMT
Author: hlship
Date: Fri Jun  4 05:06:43 2010
New Revision: 951266

URL: http://svn.apache.org/viewvc?rev=951266&view=rev
Log:
Add a Mapper2 abstract calls, and a Flow.map() method for mapping with values from two Flows

Added:
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapped2Value.java
      - copied, changed from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper2.java
      - copied, changed from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper2.java
      - copied, changed from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
Modified:
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/AbstractFlow.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Flow.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
    tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/MapperTest.java

Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/AbstractFlow.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/AbstractFlow.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/AbstractFlow.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/AbstractFlow.java
Fri Jun  4 05:06:43 2010
@@ -119,6 +119,17 @@ abstract class AbstractFlow<T> implement
         return F.lazy(new LazyMapper<T, X>(mapper, this));
     }
 
+    public <X, Y> Flow<Y> map(Mapper2<T, X, Y> mapper, Flow<? extends
X> flow)
+    {
+        assert mapper != null;
+        assert flow != null;
+
+        if (this.isEmpty() || flow.isEmpty())
+            return F.emptyFlow();
+
+        return F.lazy(new LazyMapper2<T, X, Y>(mapper, this, flow));
+    }
+
     public <A> A reduce(Reducer<A, T> reducer, A initial)
     {
         assert reducer != null;
@@ -210,4 +221,5 @@ abstract class AbstractFlow<T> implement
 
         return F.lazy(new LazyDrop<T>(length, this));
     }
+
 }

Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
Fri Jun  4 05:06:43 2010
@@ -204,6 +204,14 @@ public class F
         };
     };
 
+    public static Mapper2<Integer, Integer, Integer> ADD_INTS = new Mapper2<Integer,
Integer, Integer>()
+    {
+        public Integer map(Integer first, Integer second)
+        {
+            return first + second;
+        };
+    };
+
     /**
      * Extracts the values from the collection to form a {@link Flow}. The Collection
      * may change after the Flow is created without affecting the Flow.

Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Flow.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Flow.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Flow.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Flow.java
Fri Jun  4 05:06:43 2010
@@ -38,10 +38,18 @@ import java.util.List;
  */
 public interface Flow<T> extends Iterable<T>
 {
-    /** Maps a Flow into a new Flow with different type values. */
+    /** Maps a Flow into a new Flow with different type values. Mapping is a lazy operation.
*/
     <X> Flow<X> map(Mapper<T, X> mapper);
 
     /**
+     * Combines two Flows using a two-parameter Mapper. Each value of
+     * this Flow, and the corresponding value of the other flow are passed through the Mapper
+     * to provide the values of the output Flow. The length of the result Flow is
+     * the smaller of the lengths of the two input Flows. Mapping is a lazy operation.
+     */
+    <X, Y> Flow<Y> map(Mapper2<T, X, Y> mapper, Flow<? extends X>
flow);
+
+    /**
      * Given a {@link Mapper} that maps a T to a Flow<X>, this method will lazily concatenate
      * all the output flows into a single Flow<X>.
      */

Copied: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapped2Value.java
(from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapped2Value.java?p2=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapped2Value.java&p1=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java&r1=951204&r2=951266&rev=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapped2Value.java
Fri Jun  4 05:06:43 2010
@@ -14,20 +14,24 @@
 
 package org.apache.tapestry5.func;
 
-class LazyMappedValue<T, X> implements LazyValue<X>
+class LazyMapped2Value<T, X, Y> implements LazyValue<Y>
 {
-    private final T input;
+    private final Flow<T> left;
 
-    private final Mapper<T, X> mapper;
+    private final Flow<X> right;
 
-    public LazyMappedValue(T input, Mapper<T, X> mapper)
+    private final Mapper2<T, X, Y> mapper;
+
+    public LazyMapped2Value(Flow<T> left, Flow<X> right, Mapper2<T, X, Y>
mapper)
     {
-        this.input = input;
+        this.left = left;
+        this.right = right;
         this.mapper = mapper;
     }
 
-    public X get()
+    public Y get()
     {
-        return mapper.map(input);
+        return mapper.map(left.first(), right.first());
     }
+
 }

Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
Fri Jun  4 05:06:43 2010
@@ -16,18 +16,18 @@ package org.apache.tapestry5.func;
 
 class LazyMappedValue<T, X> implements LazyValue<X>
 {
-    private final T input;
+    private final Flow<T> flow;
 
     private final Mapper<T, X> mapper;
 
-    public LazyMappedValue(T input, Mapper<T, X> mapper)
+    public LazyMappedValue(Flow<T> input, Mapper<T, X> mapper)
     {
-        this.input = input;
+        this.flow = input;
         this.mapper = mapper;
     }
 
     public X get()
     {
-        return mapper.map(input);
+        return mapper.map(flow.first());
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
Fri Jun  4 05:06:43 2010
@@ -31,8 +31,9 @@ class LazyMapper<T, X> implements LazyFu
         if (flow.isEmpty())
             return null;
 
-        return new LazyContinuation<X>(new LazyMappedValue<T, X>(flow.first(),
mapper), new LazyMapper<T, X>(mapper,
-                flow.rest()));
+        LazyValue<X> nextValue = new LazyMappedValue<T, X>(flow, mapper);
+
+        return new LazyContinuation<X>(nextValue, new LazyMapper<T, X>(mapper,
flow.rest()));
     }
 
 }

Copied: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper2.java
(from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper2.java?p2=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper2.java&p1=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java&r1=951204&r2=951266&rev=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMapper2.java
Fri Jun  4 05:06:43 2010
@@ -14,25 +14,29 @@
 
 package org.apache.tapestry5.func;
 
-class LazyMapper<T, X> implements LazyFunction<X>
+class LazyMapper2<T, X, Y> implements LazyFunction<Y>
 {
-    private final Mapper<T, X> mapper;
+    private final Mapper2<T, X, Y> mapper;
 
-    private final Flow<T> flow;
+    private final Flow<T> left;
 
-    public LazyMapper(Mapper<T, X> mapper, Flow<T> flow)
+    private final Flow<X> right;
+
+    @SuppressWarnings("unchecked")
+    public LazyMapper2(Mapper2<T, X, Y> mapper, Flow<T> left, Flow<? extends
X> right)
     {
         this.mapper = mapper;
-        this.flow = flow;
+        this.left = left;
+        this.right = (Flow<X>) right;
     }
 
-    public LazyContinuation<X> next()
+    public LazyContinuation<Y> next()
     {
-        if (flow.isEmpty())
+        if (left.isEmpty() || right.isEmpty())
             return null;
 
-        return new LazyContinuation<X>(new LazyMappedValue<T, X>(flow.first(),
mapper), new LazyMapper<T, X>(mapper,
-                flow.rest()));
-    }
+        LazyValue<Y> nextValue = new LazyMapped2Value<T, X, Y>(left, right, mapper);
 
+        return new LazyContinuation<Y>(nextValue, new LazyMapper2<T, X, Y>(mapper,
left.rest(), right.rest()));
+    }
 }

Copied: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper2.java
(from r951204, tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java)
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper2.java?p2=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper2.java&p1=tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java&r1=951204&r2=951266&rev=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyMappedValue.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/Mapper2.java
Fri Jun  4 05:06:43 2010
@@ -14,20 +14,14 @@
 
 package org.apache.tapestry5.func;
 
-class LazyMappedValue<T, X> implements LazyValue<X>
+/**
+ * A generalization of {@link Mapper} for a two-input function.
+ */
+public abstract class Mapper2<A, B, C>
 {
-    private final T input;
-
-    private final Mapper<T, X> mapper;
-
-    public LazyMappedValue(T input, Mapper<T, X> mapper)
-    {
-        this.input = input;
-        this.mapper = mapper;
-    }
-
-    public X get()
-    {
-        return mapper.map(input);
-    }
+    /**
+     * Take, as input, two values (from two flows) and return a computed value of
+     * the third type.
+     */
+    public abstract C map(A first, B second);
 }

Modified: tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/MapperTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/MapperTest.java?rev=951266&r1=951265&r2=951266&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/MapperTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/MapperTest.java
Fri Jun  4 05:06:43 2010
@@ -146,6 +146,23 @@ public class MapperTest extends BaseFunc
 
         assertFlowValues(flow.take(3), 2, 4, 6);
         assertEquals(count.get(), 1);
+    }
+
+    @Test
+    public void map2_empty_is_the_empty_flow()
+    {
+        Flow<Integer> flow = F.flow(1, 2, 3);
+
+        assertSame(flow.map(F.ADD_INTS, filteredEmpty), F.EMPTY_FLOW);
+        assertSame(filteredEmpty.map(F.ADD_INTS, flow), F.EMPTY_FLOW);
+    }
+
+    @Test
+    public void map2()
+    {
+        Flow<Integer> left = F.range(10, 13);
+        Flow<Integer> right = F.series(20, 2);
 
+        assertFlowValues(left.map(F.ADD_INTS, right), 10 + 20, 11 + 22, 12 + 24);
     }
 }



Mime
View raw message