tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r935576 - in /tapestry/tapestry5/trunk/tapestry-ioc/src: main/java/org/apache/tapestry5/ioc/internal/util/ test/java/org/apache/tapestry5/ioc/internal/util/
Date Mon, 19 Apr 2010 14:01:00 GMT
Author: hlship
Date: Mon Apr 19 14:01:00 2010
New Revision: 935576

URL: http://svn.apache.org/viewvc?rev=935576&view=rev
Log:
Add an functional each() that works on a Collection and an Operation

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

Modified: 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=935576&r1=935575&r2=935576&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollections.java
Mon Apr 19 14:01:00 2010
@@ -15,6 +15,7 @@
 package org.apache.tapestry5.ioc.internal.util;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 import org.apache.tapestry5.ioc.services.Coercion;
@@ -50,4 +51,14 @@ public class FunctionalCollections
         return result;
     }
 
+    /**
+     * Performs an operation on each element of the source collection.
+     */
+    public static <T> void each(Collection<T> source, Operation<T> operation)
+    {
+        for (T t : source)
+        {
+            operation.op(t);
+        }
+    }
 }

Added: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/Operation.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/Operation.java?rev=935576&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/Operation.java
(added)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/Operation.java
Mon Apr 19 14:01:00 2010
@@ -0,0 +1,28 @@
+// 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;
+
+/**
+ * An operational function used with a collection.
+ * 
+ * @since 5.2.0
+ */
+public interface Operation<T>
+{
+    /**
+     * Perform the operation on some object of type T.
+     */
+    void op(T value);
+}

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

Modified: 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=935576&r1=935575&r2=935576&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/util/FunctionalCollectionsTest.java
Mon Apr 19 14:01:00 2010
@@ -40,4 +40,27 @@ public class FunctionalCollectionsTest e
 
         assertListsEquals(lengths, 4, 3, 1, 6, 4);
     }
+
+    @Test
+    public void each()
+    {
+        List<String> source = Arrays.asList("Mary", "had", "a", "little", "lamb");
+
+        final StringBuffer buffer = new StringBuffer();
+
+        Operation<String> op = new Operation<String>()
+        {
+            public void op(String value)
+            {
+                if (buffer.length() > 0)
+                    buffer.append(" ");
+
+                buffer.append(value);
+            }
+        };
+
+        FunctionalCollections.each(source, op);
+
+        assertEquals(buffer.toString(), "Mary had a little lamb");
+    }
 }



Mime
View raw message