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/04/19 16:00:09 UTC

svn commit: r935575 - in /tapestry/tapestry5/trunk/tapestry-ioc/src: main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java

Author: hlship
Date: Mon Apr 19 14:00:08 2010
New Revision: 935575

URL: http://svn.apache.org/viewvc?rev=935575&view=rev
Log:
Add the start of a collection of functional operations for collections

Added:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java   (with props)
    tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java   (with props)

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java?rev=935575&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java Mon Apr 19 14:00:08 2010
@@ -0,0 +1,53 @@
+// 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.internal.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tapestry5.ioc.services.Coercion;
+
+/**
+ * Functional operations on collections with generics support. Tending to use the equivalent names from
+ * Clojure.
+ * 
+ * @since 5.2.0
+ */
+public class FunctionalCollections
+{
+
+    /**
+     * Functional map (i.e., transform operation) from a List<S&g;t to List<T>.
+     */
+    public static <S, T> List<T> map(List<S> source, Coercion<S, T> coercion)
+    {
+        Defense.notNull(source, "source");
+        Defense.notNull(coercion, "coercion");
+
+        int count = source.size();
+
+        List<T> result = new ArrayList<T>(count);
+
+        for (S s : source)
+        {
+            T t = coercion.coerce(s);
+
+            result.add(t);
+        }
+
+        return result;
+    }
+
+}

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

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java?rev=935575&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java Mon Apr 19 14:00:08 2010
@@ -0,0 +1,43 @@
+// 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.internal.util;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.tapestry5.ioc.services.Coercion;
+import org.apache.tapestry5.ioc.test.TestBase;
+import org.testng.annotations.Test;
+
+public class FunctionalCollectionsTest extends TestBase
+{
+    @Test
+    public void map()
+    {
+        List<String> source = Arrays.asList("Mary", "had", "a", "little", "lamb");
+
+        Coercion<String, Integer> stringToLength = new Coercion<String, Integer>()
+        {
+            public Integer coerce(String input)
+            {
+                return input.length();
+            }
+        };
+
+        List<Integer> lengths = FunctionalCollections.map(source, stringToLength);
+
+        assertListsEquals(lengths, 4, 3, 1, 6, 4);
+    }
+}

Propchange: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native