tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r953498 - in /tapestry/tapestry5/trunk/tapestry-func/src: main/java/org/apache/tapestry5/func/AbstractFlow.java main/java/org/apache/tapestry5/func/Flow.java test/java/org/apache/tapestry5/func/FuncTest.java
Date Thu, 10 Jun 2010 23:30:27 GMT
Author: hlship
Date: Thu Jun 10 23:30:26 2010
New Revision: 953498

URL: http://svn.apache.org/viewvc?rev=953498&view=rev
Log:
Add method for converting a Flow directly to an object array

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/Flow.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=953498&r1=953497&r2=953498&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 10 23:30:26 2010
@@ -14,7 +14,9 @@
 
 package org.apache.tapestry5.func;
 
+import java.lang.reflect.Array;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
@@ -202,6 +204,18 @@ abstract class AbstractFlow<T> implement
         return Collections.unmodifiableList(toMutableList());
     }
 
+    @SuppressWarnings("unchecked")
+    public T[] toArray(Class<T> type)
+    {
+        assert type != null;
+
+        List<T> list = toMutableList();
+
+        Object array = Array.newInstance(type, list.size());
+
+        return list.toArray((T[]) array);
+    }
+
     public int count()
     {
         return isEmpty() ? 0 : 1 + rest().count();

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=953498&r1=953497&r2=953498&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
Thu Jun 10 23:30:26 2010
@@ -87,6 +87,12 @@ public interface Flow<T> extends Iterabl
      */
     List<T> toList();
 
+    /**
+     * Converts the Flow into an array of values (due to type erasure, you have to remind
the Flow about the
+     * type).
+     */
+    T[] toArray(Class<T> type);
+
     /** Returns a new flow with the same elements but in reverse order. */
     Flow<T> reverse();
 

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=953498&r1=953497&r2=953498&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 10 23:30:26 2010
@@ -25,7 +25,6 @@ import org.testng.annotations.Test;
 
 public class FuncTest extends BaseFuncTest
 {
-   
 
     @Test
     public void flow_reverse()
@@ -490,7 +489,6 @@ public class FuncTest extends BaseFuncTe
         assertListsEquals(result, "a", "had", "Mary", "lamb");
     }
 
- 
     @Test
     public void each_on_empty_flow()
     {
@@ -530,7 +528,6 @@ public class FuncTest extends BaseFuncTe
         }, initial), initial);
     }
 
-
     @Test
     public void count_of_the_empty_flow_is_zero()
     {
@@ -552,7 +549,6 @@ public class FuncTest extends BaseFuncTe
         assertEquals(flow.filter(F.notNull()).count(), 5);
     }
 
- 
     @Test
     public void concat_empty_list()
     {
@@ -561,4 +557,12 @@ public class FuncTest extends BaseFuncTe
         assertFlowValues(flow.concat(flow.filter(evenp)), 1, 3);
     }
 
+    @Test
+    public void to_array()
+    {
+        Flow<Integer> flow = F.range(1, 10).drop(2).take(3);
+
+        assertTrue(Arrays.equals(flow.toArray(Integer.class), new Integer[]
+        { 3, 4, 5 }));
+    }
 }



Mime
View raw message