Author: hlship
Date: Fri May 28 01:20:36 2010
New Revision: 949058
URL: http://svn.apache.org/viewvc?rev=949058&view=rev
Log:
Add new methods for appending to a Flow, or concatinating another Flow or List
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/Flow.java
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/FlowImpl.java
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/func/FuncTest.java
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/Flow.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/Flow.java?rev=949058&r1=949057&r2=949058&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/Flow.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/Flow.java
Fri May 28 01:20:36 2010
@@ -56,4 +56,15 @@ public interface Flow<T>
/** Returns a new flow with the same elements but in reverse order. */
Flow<T> reverse();
+
+ /** Returns true if the Flow contains no values. */
+ boolean isEmpty();
+
+ /** Returns a new Flow with the other Flow's elements appended to this
Flow's. */
+ Flow<T> concat(Flow<? extends T> other);
+
+ /** Returns a new Flow with the values in the list appended to this Flow.
*/
+ Flow<T> concat(List<? extends T> list);
+
+ <V extends T> Flow<T> append(V... values);
}
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/FlowImpl.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/FlowImpl.java?rev=949058&r1=949057&r2=949058&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/FlowImpl.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/func/FlowImpl.java
Fri May 28 01:20:36 2010
@@ -113,6 +113,9 @@ class FlowImpl<T> implements Flow<T>
public Flow<T> reverse()
{
+ if (values.size() < 2)
+ return this;
+
List<T> newValues = CollectionFactory.newList(values);
Collections.reverse(newValues);
@@ -120,4 +123,32 @@ class FlowImpl<T> implements Flow<T>
return new FlowImpl<T>(newValues);
}
+ public boolean isEmpty()
+ {
+ return values.isEmpty();
+ }
+
+ public Flow<T> concat(Flow<? extends T> other)
+ {
+ Defense.notNull(other, "other");
+
+ if (other.isEmpty())
+ return this;
+
+ List<T> newValues = new ArrayList<T>(values);
+ newValues.addAll(other.toList());
+
+ return new FlowImpl<T>(newValues);
+ }
+
+ public Flow<T> concat(List<? extends T> list)
+ {
+ return concat(F.flow(list));
+ }
+
+ public <V extends T> Flow<T> append(V... values)
+ {
+ return concat(F.flow(values));
+ }
+
}
Modified:
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/func/FuncTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/func/FuncTest.java?rev=949058&r1=949057&r2=949058&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/func/FuncTest.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/func/FuncTest.java
Fri May 28 01:20:36 2010
@@ -266,7 +266,8 @@ public class FuncTest extends TestBase
@Test
public void reduce()
{
- int total = F.flow(F.flow("Mary", "had", "a", "little",
"lamb").map(stringToLength).toList()).reduce(F.SUM_INTS, 0);
+ int total = F.flow(F.flow("Mary", "had", "a", "little",
"lamb").map(stringToLength).toList()).reduce(
+ F.SUM_INTS, 0);
assertEquals(total, 18);
}
@@ -278,4 +279,55 @@ public class FuncTest extends TestBase
assertEquals(total, 18);
}
+
+ @Test
+ public void reverse_a_short_list_is_same_object()
+ {
+ Flow<Integer> empty = F.flow();
+
+ assertSame(empty.reverse(), empty);
+
+ Flow<Integer> one = F.flow(1);
+
+ assertSame(one.reverse(), one);
+ }
+
+ @Test
+ public void concat_flows()
+ {
+ Flow<Integer> first = F.flow(1, 2, 3);
+
+ Flow<Integer> updated = first.concat(F.flow(4, 5, 6));
+
+ assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+ }
+
+ @Test
+ public void concat_empty_list_is_same()
+ {
+ Flow<Integer> first = F.flow(1, 2, 3);
+ Flow<Integer> empty = F.flow();
+
+ assertSame(first.concat(empty), first);
+ }
+
+ @Test
+ public void concat_list_onto_flow()
+ {
+ Flow<Integer> first = F.flow(1, 2, 3);
+
+ Flow<Integer> updated = first.concat(Arrays.asList(4, 5, 6));
+
+ assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+ }
+
+ @Test
+ public void append_values_onto_flow()
+ {
+ Flow<Integer> first = F.flow(1, 2, 3);
+
+ Flow<Integer> updated = first.append(4, 5, 6);
+
+ assertListsEquals(updated.toList(), 1, 2, 3, 4, 5, 6);
+ }
}