Author: hlship
Date: Thu Jun  3 06:44:02 2010
New Revision: 950887

URL: http://svn.apache.org/viewvc?rev=950887&view=rev
Log:
Add F.range() to create a lazy Flow between two integers

Added:
    
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyRange.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.java
   (with props)
    
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
   (with props)
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/test/java/org/apache/tapestry5/func/FuncTest.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=950887&r1=950886&r2=950887&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
 Thu Jun  3 06:44:02 2010
@@ -89,7 +89,7 @@ abstract class AbstractFlow<T> implement
 
     public Flow<T> concat(Flow<? extends T> other)
     {
-        return lazy(new LazyConcat<T>(this, other));
+        return F.lazy(new LazyConcat<T>(this, other));
     }
 
     /** Subclasses may override this for efficiency. */
@@ -109,19 +109,14 @@ abstract class AbstractFlow<T> implement
     {
         assert predicate != null;
 
-        return lazy(new LazyFilter<T>(predicate, this));
-    }
-
-    private static <Y> Flow<Y> lazy(LazyFunction<Y> function)
-    {
-        return new LazyFlow<Y>(function);
+        return F.lazy(new LazyFilter<T>(predicate, this));
     }
 
     public <X> Flow<X> map(Mapper<T, X> mapper)
     {
         assert mapper != null;
 
-        return lazy(new LazyMapper<T, X>(mapper, this));
+        return F.lazy(new LazyMapper<T, X>(mapper, this));
     }
 
     public <A> A reduce(Reducer<A, T> reducer, A initial)

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=950887&r1=950886&r2=950887&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
 Thu Jun  3 06:44:02 2010
@@ -229,4 +229,31 @@ public class F
 
         return new ArrayFlow<T>(values);
     }
+
+    /**
+     * 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,
+     * the Flow counts down instead.
+     * 
+     * @param lower
+     *            start of range (inclusive)
+     * @param upper
+     *            end of range (exclusive)
+     */
+    public static Flow<Integer> range(int lower, int upper)
+    {
+        if (lower == upper)
+            return F.emptyFlow();
+
+        if (lower < upper)
+            return lazy(new LazyRange(lower, upper, 1));
+
+        return lazy(new LazyRange(lower, upper, -1));
+    }
+
+    static <T> Flow<T> lazy(LazyFunction<T> function)
+    {
+        return new LazyFlow<T>(function);
+    }
 }

Added: 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyRange.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyRange.java?rev=950887&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyRange.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/main/java/org/apache/tapestry5/func/LazyRange.java
 Thu Jun  3 06:44:02 2010
@@ -0,0 +1,39 @@
+// 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;
+
+class LazyRange implements LazyFunction<Integer>
+{
+    private final int lower, upper, delta;
+
+    public LazyRange(int lower, int upper, int delta)
+    {
+        this.lower = lower;
+        this.upper = upper;
+        this.delta = delta;
+    }
+
+    public LazyContinuation<Integer> next()
+    {
+        if (delta < 0 && lower + delta < upper)
+            return null;
+
+        if (delta > 0 && lower + delta > upper)
+            return null;
+
+        return new LazyContinuation<Integer>(lower, new LazyRange(lower + 
delta, upper, delta));
+    }
+
+}

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

Added: 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.java?rev=950887&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.java
 Thu Jun  3 06:44:02 2010
@@ -0,0 +1,40 @@
+// 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.Arrays;
+import java.util.List;
+
+import org.testng.Assert;
+
+public class FuncAssert extends Assert
+{
+
+    protected <T> void assertFlowValues(Flow<T> actual, T... expected)
+    {
+        assertListsEquals(actual.toList(), expected);
+    }
+
+    protected <T> void assertListsEquals(List<T> actual, T... expected)
+    {
+        assertEquals(actual, Arrays.asList(expected));
+    }
+
+    protected void unreachable()
+    {
+        throw new RuntimeException("Should not be reachable.");
+    }
+
+}

Propchange: 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/FuncAssert.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=950887&r1=950886&r2=950887&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
 Thu Jun  3 06:44:02 2010
@@ -24,7 +24,7 @@ import java.util.Locale;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-public class FuncTest extends Assert
+public class FuncTest extends FuncAssert
 {
 
     private Mapper<String, Integer> stringToLength = new Mapper<String, 
Integer>()
@@ -53,16 +53,6 @@ public class FuncTest extends Assert
 
     private Flow<Integer> filteredEmpty = F.flow(1, 3, 5, 7).filter(evenp);
 
-    private <T> void assertListsEquals(List<T> actual, T... expected)
-    {
-        assertEquals(actual, Arrays.asList(expected));
-    }
-
-    private void unreachable()
-    {
-        throw new RuntimeException("Should not be reachable.");
-    }
-
     @Test
     public void map()
     {
@@ -76,13 +66,13 @@ public class FuncTest extends Assert
     @Test
     public void flow_map()
     {
-        assertListsEquals(F.flow("Mary", "had", "a", "little", 
"lamb").map(stringToLength).toList(), 4, 3, 1, 6, 4);
+        assertFlowValues(F.flow("Mary", "had", "a", "little", 
"lamb").map(stringToLength), 4, 3, 1, 6, 4);
     }
 
     @Test
     public void flow_reverse()
     {
-        assertListsEquals(F.flow(1, 2, 3).reverse().toList(), 3, 2, 1);
+        assertFlowValues(F.flow(1, 2, 3).reverse(), 3, 2, 1);
     }
 
     @Test
@@ -222,7 +212,7 @@ public class FuncTest extends Assert
     @Test
     public void flow_filter()
     {
-        assertListsEquals(F.flow(1, 2, 3, 4, 5, 6, 7).filter(evenp).toList(), 
2, 4, 6);
+        assertFlowValues(F.flow(1, 2, 3, 4, 5, 6, 7).filter(evenp), 2, 4, 6);
     }
 
     @Test
@@ -306,7 +296,7 @@ public class FuncTest extends Assert
     }
 
     @Test
-    public void reduce()
+    public void array_flow_reduce()
     {
         int total = F.flow(F.flow("Mary", "had", "a", "little", 
"lamb").map(stringToLength).toList()).reduce(
                 F.SUM_INTS, 0);
@@ -315,7 +305,7 @@ public class FuncTest extends Assert
     }
 
     @Test
-    public void flow_reduce()
+    public void general_flow_reduce()
     {
         int total = F.flow("Mary", "had", "a", "little", 
"lamb").map(stringToLength).reduce(F.SUM_INTS, 0);
 
@@ -341,7 +331,7 @@ public class FuncTest extends Assert
 
         Flow<Integer> updated = first.concat(F.flow(4, 5, 6));
 
-        assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+        assertFlowValues(updated, 1, 2, 3, 4, 5, 6);
     }
 
     @Test
@@ -360,7 +350,7 @@ public class FuncTest extends Assert
 
         Flow<Integer> updated = first.concat(Arrays.asList(4, 5, 6));
 
-        assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+        assertFlowValues(updated, 1, 2, 3, 4, 5, 6);
     }
 
     @Test
@@ -370,14 +360,13 @@ public class FuncTest extends Assert
 
         Flow<Integer> updated = first.append(4, 5, 6);
 
-        assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+        assertFlowValues(updated, 1, 2, 3, 4, 5, 6);
     }
 
     @Test
     public void sort_comparable_list()
     {
-        assertListsEquals(F.flow("fred", "barney", "wilma", 
"betty").sort().toList(), "barney", "betty", "fred",
-                "wilma");
+        assertFlowValues(F.flow("fred", "barney", "wilma", "betty").sort(), 
"barney", "betty", "fred", "wilma");
     }
 
     @Test
@@ -414,7 +403,7 @@ public class FuncTest extends Assert
             }
         };
 
-        assertListsEquals(flow.sort(comparator).toList(), "a", "bb", "ccc", 
"dddd", "eeeee");
+        assertFlowValues(flow.sort(comparator), "a", "bb", "ccc", "dddd", 
"eeeee");
     }
 
     @Test(expectedExceptions = ClassCastException.class)
@@ -449,7 +438,7 @@ public class FuncTest extends Assert
     @Test
     public void rest_of_non_empty_flow()
     {
-        assertListsEquals(F.flow("Mary", "had", "a", "little", 
"lamb").rest().toList(), "had", "a", "little", "lamb");
+        assertFlowValues(F.flow("Mary", "had", "a", "little", "lamb").rest(), 
"had", "a", "little", "lamb");
     }
 
     @Test
@@ -498,13 +487,13 @@ public class FuncTest extends Assert
     @Test
     public void sort_non_array_flow()
     {
-        assertListsEquals(filteredEmpty.append(7, 3, 9).sort().toList(), 3, 7, 
9);
+        assertFlowValues(filteredEmpty.append(7, 3, 9).sort(), 3, 7, 9);
     }
 
     @Test
     public void reverse_non_array_flow()
     {
-        assertListsEquals(filteredEmpty.append(1, 2, 3).reverse().toList(), 3, 
2, 1);
+        assertFlowValues(filteredEmpty.append(1, 2, 3).reverse(), 3, 2, 1);
     }
 
     @Test(expectedExceptions = UnsupportedOperationException.class)
@@ -625,7 +614,7 @@ public class FuncTest extends Assert
     {
         Flow<Integer> flow = F.flow(3, 1, 2);
 
-        assertListsEquals(flow.mapcat(sequencer).toList(), 3, 3, 3, 1, 2, 2);
+        assertFlowValues(flow.mapcat(sequencer), 3, 3, 3, 1, 2, 2);
     }
 
     @Test
@@ -663,7 +652,7 @@ public class FuncTest extends Assert
     {
         Flow<Integer> flow = F.flow(1, 2, 3);
 
-        assertListsEquals(flow.map(F.<Integer> stringValueOf()).toList(), "1", 
"2", "3");
+        assertFlowValues(flow.map(F.<Integer> stringValueOf()), "1", "2", "3");
     }
 
     @Test
@@ -671,7 +660,7 @@ public class FuncTest extends Assert
     {
         Flow<Integer> flow = F.flow(1, 3);
 
-        assertListsEquals(flow.concat(flow.filter(evenp)).toList(), 1, 3);
+        assertFlowValues(flow.concat(flow.filter(evenp)), 1, 3);
     }
 
 }

Added: 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java?rev=950887&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-func/src/test/java/org/apache/tapestry5/func/RangeTests.java
 Thu Jun  3 06:44:02 2010
@@ -0,0 +1,39 @@
+// 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 org.testng.annotations.Test;
+
+public class RangeTests extends FuncAssert
+{
+    @Test
+    public void empty_range_if_values_equal()
+    {
+        assertTrue(F.range(9, 9).isEmpty());
+    }
+
+    @Test
+    public void ascending_range()
+    {
+
+        assertFlowValues(F.range(5, 8), 5, 6, 7);
+    }
+
+    @Test
+    public void descending_range()
+    {
+        assertFlowValues(F.range(8, 5), 8, 7, 6);
+    }
+}

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


Reply via email to