You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/08/18 18:27:31 UTC

svn commit: r986778 - in /tapestry/tapestry5/trunk/tapestry-func/src: main/java/org/apache/tapestry5/func/F.java main/java/org/apache/tapestry5/func/LazyIterator.java test/java/org/apache/tapestry5/func/FuncTest.java

Author: hlship
Date: Wed Aug 18 16:27:31 2010
New Revision: 986778

URL: http://svn.apache.org/viewvc?rev=986778&view=rev
Log:
TAP5-1250: Add support for creating a Flow from an Iterable (not just Collection and object array)

Added:
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterator.java   (with props)
Modified:
    tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/F.java
    tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java

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=986778&r1=986777&r2=986778&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 Wed Aug 18 16:27:31 2010
@@ -15,6 +15,7 @@
 package org.apache.tapestry5.func;
 
 import java.util.Collection;
+import java.util.Iterator;
 
 /**
  * Functional operations on collections with generics support. The core interface is {@link Flow} to which operations
@@ -29,9 +30,9 @@ import java.util.Collection;
  * 
  * @since 5.2.0
  */
+@SuppressWarnings("all")
 public class F
 {
-    @SuppressWarnings("unchecked")
     final static Flow<?> EMPTY_FLOW = new EmptyFlow();
 
     @SuppressWarnings("unchecked")
@@ -174,9 +175,9 @@ public class F
     /**
      * Override of {@link #select(Predicate, Mapper)} where rejected values are replaced with a fixed value.
      */
-    public static <S, T> Mapper<S, T> select(Predicate<? super S> predicate, Mapper<S, T> ifAccepted, T ifRejected)
+    public static <S, T> Mapper<S, T> select(Predicate<? super S> predicate, Mapper<S, T> ifAccepted, T ifRejectedValue)
     {
-        Mapper<S, T> rejectedMapper = always(ifRejected);
+        Mapper<S, T> rejectedMapper = always(ifRejectedValue);
 
         return select(predicate, ifAccepted, rejectedMapper);
     }
@@ -250,6 +251,18 @@ public class F
     }
 
     /**
+     * Creates a lazy Flow from the {@link Iterator} obtained from the iterable. The Flow
+     * will be threadsafe as long as the iterable yields a new Iterator on each invocation <em>and</em> the underlying
+     * iterable object is not modified while the Flow is evaluating. In other words, not extremely threadsafe.
+     */
+    public static <T> Flow<T> flow(Iterable<T> iterable)
+    {
+        assert iterable != null;
+
+        return lazy(new LazyIterator<T>(iterable.iterator()));
+    }
+
+    /**
      * Creates a lazy Flow that returns integers in the given range. The range starts
      * with the lower value and counts by 1 up to the upper range (which is not part of
      * the Flow). If lower equals upper, the Flow is empty. If upper is less than lower,

Added: tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterator.java?rev=986778&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterator.java (added)
+++ tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyIterator.java Wed Aug 18 16:27:31 2010
@@ -0,0 +1,38 @@
+// 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.func;
+
+import java.util.Iterator;
+
+/**
+ * Converts an {@link Iterator} into a {@link LazyFunction}.
+ */
+class LazyIterator<T> implements LazyFunction<T>
+{
+    private final Iterator<T> iterator;
+
+    public LazyIterator(Iterator<T> iterator)
+    {
+        this.iterator = iterator;
+    }
+
+    public LazyContinuation<T> next()
+    {
+        if (!iterator.hasNext())
+            return null;
+
+        return new LazyContinuation<T>(iterator.next(), this);
+    }
+}

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

Modified: tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java?rev=986778&r1=986777&r2=986778&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncTest.java Wed Aug 18 16:27:31 2010
@@ -233,8 +233,8 @@ public class FuncTest extends BaseFuncTe
         // Converting to null and then filtering out nulls is the hard way to do filter or remove,
         // but exercises the code we want to test.
 
-        List<String> filtered = F.flow("Mary", "had", "a", "little", "lamb").map(F.select(combinedp, identity)).remove(
-                isNull).toList();
+        List<String> filtered = F.flow("Mary", "had", "a", "little", "lamb").map(F.select(combinedp, identity))
+                .remove(isNull).toList();
 
         assertListsEquals(filtered, "Mary", "little", "lamb");
     }
@@ -565,4 +565,21 @@ public class FuncTest extends BaseFuncTe
         assertTrue(Arrays.equals(flow.toArray(Integer.class), new Integer[]
         { 3, 4, 5 }));
     }
+
+    @Test
+    public void lazy_flow_from_iterable()
+    {
+        Iterable<Integer> iterable = new Iterable<Integer>()
+        {
+
+            public Iterator<Integer> iterator()
+            {
+                return Arrays.asList(9, 7, 1).iterator();
+            }
+        };
+
+        Flow<Integer> flow = F.flow(iterable);
+
+        assertFlowValues(flow, 9, 7, 1);
+    }
 }