You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2010/05/26 03:57:49 UTC

svn commit: r948287 - in /tapestry/tapestry5/trunk/tapestry-ioc/src: main/java/org/apache/tapestry5/ioc/util/func/ test/java/org/apache/tapestry5/ioc/util/func/

Author: hlship
Date: Wed May 26 01:57:49 2010
New Revision: 948287

URL: http://svn.apache.org/viewvc?rev=948287&view=rev
Log:
Create a Flow, a representation of a series of operations on a collection of values

Added:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java   (with props)
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java   (with props)
Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/F.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=948287&r1=948286&r2=948287&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:49 2010
@@ -80,7 +80,7 @@ public class F
     /**
      * Performs an operation on each element of the source collection.
      */
-    public static <T> void each(Worker<T> operation, Collection<T> source)
+    public static <T> void each(Worker<? super T> operation, Collection<T> source)
     {
         for (T t : source)
         {
@@ -291,4 +291,14 @@ public class F
             return accumulator + value;
         };
     };
+
+    public static <T> Flow<T> flow(List<T> values)
+    {
+        return new FlowImpl<T>(values);
+    }
+
+    public static <T> Flow<T> flow(T... values)
+    {
+        return flow(Arrays.asList(values));
+    }
 }

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java?rev=948287&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java Wed May 26 01:57:49 2010
@@ -0,0 +1,51 @@
+// Copyright 2010 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.ioc.util.func;
+
+import java.util.List;
+
+/**
+ * A fluent interface for manipulating collections using map, reduce, filter and remove. Flows are
+ * immutable; operations create new flows to replace the old ones.
+ * <p>
+ * A future enhancement may be to make flows lazy.
+ * 
+ * @since 5.2.0
+ */
+public interface Flow<T>
+{
+    /** Maps a Flow into a new Flow with different type values. */
+    <X> Flow<X> map(Mapper<T, X> mapper);
+
+    /**
+     * Filters values, keeping only values where the predicate is true, returning a new Flow with just
+     * the remaining values.
+     */
+    Flow<T> filter(Predicate<? super T> predicate);
+
+    /** Removes values where the predicate returns true, returning a new Flow with just the remaining values. */
+    Flow<T> remove(Predicate<? super T> predicate);
+
+    /** Applies a reducer to the values of the Flow. */
+    <A> A reduce(Reducer<A, T> reducer, A initial);
+
+    /**
+     * Applies the worker to each value in the Flow, then returns the flow for further behaviors.
+     */
+    Flow<T> each(Worker<? super T> worker);
+
+    /** Converts the Flow into an unmodifiable list of values. */
+    List<T> toList();
+}

Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/Flow.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java?rev=948287&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java Wed May 26 01:57:49 2010
@@ -0,0 +1,61 @@
+// Copyright 2010 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.ioc.util.func;
+
+import java.util.Collections;
+import java.util.List;
+
+class FlowImpl<T> implements Flow<T>
+{
+    private final List<T> values;
+
+    public FlowImpl(List<T> values)
+    {
+        this.values = values;
+    }
+
+    public Flow<T> each(Worker<? super T> worker)
+    {
+        F.each(worker, values);
+
+        return this;
+    }
+
+    public Flow<T> filter(Predicate<? super T> predicate)
+    {
+        return new FlowImpl<T>(F.filter(predicate, values));
+    }
+
+    public Flow<T> remove(Predicate<? super T> predicate)
+    {
+        return new FlowImpl<T>(F.remove(predicate, values));
+    }
+
+    public <X> Flow<X> map(Mapper<T, X> mapper)
+    {
+        return new FlowImpl<X>(F.map(mapper, values));
+    }
+
+    public <A> A reduce(Reducer<A, T> reducer, A initial)
+    {
+        return F.reduce(reducer, initial, values);
+    }
+
+    public List<T> toList()
+    {
+        return Collections.unmodifiableList(values);
+    }
+
+}

Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/util/func/FlowImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=948287&r1=948286&r2=948287&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:49 2010
@@ -59,6 +59,12 @@ public class FuncTest extends TestBase
     }
 
     @Test
+    public void flow_map()
+    {
+        assertListsEquals(F.flow("Mary", "had", "a", "little", "lamb").map(stringToLength).toList(), 4, 3, 1, 6, 4);
+    }
+
+    @Test
     public void combine_mappers()
     {
         List<Boolean> even = F.map(stringToLength.combine(toEven), "Mary", "had", "a", "little", "lamb");
@@ -100,6 +106,29 @@ public class FuncTest extends TestBase
     }
 
     @Test
+    public void flow_each()
+    {
+        Flow<String> flow = F.flow("Mary", "had", "a", "little", "lamb");
+
+        final StringBuffer buffer = new StringBuffer();
+
+        Worker<String> worker = new AbstractWorker<String>()
+        {
+            public void work(String value)
+            {
+                if (buffer.length() > 0)
+                    buffer.append(" ");
+
+                buffer.append(value);
+            }
+        };
+
+        assertSame(flow.each(worker), flow);
+
+        assertEquals(buffer.toString(), "Mary had a little lamb");
+    }
+
+    @Test
     public void combine_workers()
     {
         final StringBuffer buffer = new StringBuffer();
@@ -156,6 +185,12 @@ public class FuncTest extends TestBase
     }
 
     @Test
+    public void flow_filter()
+    {
+        assertListsEquals(F.flow(1, 2, 3, 4, 5, 6, 7).filter(evenp).toList(), 2, 4, 6);
+    }
+
+    @Test
     public void remove()
     {
         List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
@@ -166,6 +201,14 @@ public class FuncTest extends TestBase
     }
 
     @Test
+    public void flow_remove()
+    {
+        List<Integer> output = F.flow(1, 2, 3, 4, 5, 6, 7).remove(evenp).toList();
+
+        assertListsEquals(output, 1, 3, 5, 7);
+    }
+
+    @Test
     public void filter_empty_is_the_empty_list()
     {
         List<Integer> input = Arrays.asList();
@@ -234,4 +277,12 @@ public class FuncTest extends TestBase
 
         assertEquals(total, 18);
     }
+
+    @Test
+    public void flow_reduce()
+    {
+        int total = F.flow("Mary", "had", "a", "little", "lamb").map(stringToLength).reduce(F.SUM_INTS, 0);
+
+        assertEquals(total, 18);
+    }
 }