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 2007/01/07 20:23:59 UTC

svn commit: r493818 - in /tapestry/tapestry5/tapestry-ioc/trunk/src: main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java

Author: hlship
Date: Sun Jan  7 11:23:58 2007
New Revision: 493818

URL: http://svn.apache.org/viewvc?view=rev&rev=493818
Log:
Add CollectionFactory.newList() which takes a variable number of elements as its parameter.

Modified:
    tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java
    tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java?view=diff&rev=493818&r1=493817&r2=493818
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/main/java/org/apache/tapestry/ioc/internal/util/CollectionFactory.java Sun Jan  7 11:23:58 2007
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2007 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.
@@ -12,102 +12,107 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.apache.tapestry.ioc.internal.util;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-/**
- * Static factory methods to ease the creation of new collection types (when using generics). Most
- * of these method leverage the compiler's ability to match generic types by return value. Typical
- * usage (with a static import):
- * 
- * <pre>
- * Map&lt;Foo, Bar&gt; map = newMap();
- * </pre>
- * 
- * <p>
- * This is a replacement for:
- * 
- * <pre>
- * Map&lt;Foo, Bar&gt; map = new HashMap&lt;Foo, Bar&gt;();
- * </pre>
- */
-public final class CollectionFactory
-{
-    private CollectionFactory()
-    {
-    }
-
-    /** Constructs and returns a generic {@link HashMap} instance. */
-    public static <K, V> Map<K, V> newMap()
-    {
-        return new HashMap<K, V>();
-    }
-
-    /** Constructs and returns a generic {@link java.util.HashSet} instance. */
-    public static <T> Set<T> newSet()
-    {
-        return new HashSet<T>();
-    }
-
-    /** Contructs a new {@link HashSet} and initializes it using the provided collection. */
-    public static <T> Set<T> newSet(Collection<? extends T> values)
-    {
-        return new HashSet<T>(values);
-    }
-
-    public static <T> Set<T> newSet(T... values)
-    {
-        return newSet(Arrays.asList(values));
-    }
-
-    /**
-     * Constructs a new {@link java.util.HashMap} instance by copying an existing Map instance.
-     */
-    public static <K, V> Map<K, V> newMap(Map<? extends K, ? extends V> map)
-    {
-        return new HashMap<K, V>(map);
-    }
-
-    /**
-     * Constructs a new thread safe map.
-     */
-    public static <K, V> Map<K, V> newThreadSafeMap()
-    {
-        return new ConcurrentHashMap<K, V>();
-    }
-
-    /** Contructs and returns a new generic {@link java.util.ArrayList} instance. */
-    public static <T> List<T> newList()
-    {
-        return new ArrayList<T>();
-    }
-
-    /** Useful for queues. */
-    public static <T> LinkedList<T> newLinkedList()
-    {
-        return new LinkedList<T>();
-    }
-
-    /** Constructs and returns a new {@link ArrayList} as a copy of the provided collection. */
-    public static <T> List<T> newList(Collection<? extends T> list)
-    {
-        return new ArrayList<T>(list);
-    }
-
-    /** Constructs and returns a new {@link java.util.concurrent.CopyOnWriteArrayList}. */
-    public static <T> List<T> newThreadSafeList()
-    {
-        return new CopyOnWriteArrayList<T>();
-    }
-}
+package org.apache.tapestry.ioc.internal.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * Static factory methods to ease the creation of new collection types (when using generics). Most
+ * of these method leverage the compiler's ability to match generic types by return value. Typical
+ * usage (with a static import):
+ * 
+ * <pre>
+ * Map&lt;Foo, Bar&gt; map = newMap();
+ * </pre>
+ * 
+ * <p>
+ * This is a replacement for:
+ * 
+ * <pre>
+ * Map&lt;Foo, Bar&gt; map = new HashMap&lt;Foo, Bar&gt;();
+ * </pre>
+ */
+public final class CollectionFactory
+{
+    private CollectionFactory()
+    {
+    }
+
+    /** Constructs and returns a generic {@link HashMap} instance. */
+    public static <K, V> Map<K, V> newMap()
+    {
+        return new HashMap<K, V>();
+    }
+
+    /** Constructs and returns a generic {@link java.util.HashSet} instance. */
+    public static <T> Set<T> newSet()
+    {
+        return new HashSet<T>();
+    }
+
+    /** Contructs a new {@link HashSet} and initializes it using the provided collection. */
+    public static <T> Set<T> newSet(Collection<? extends T> values)
+    {
+        return new HashSet<T>(values);
+    }
+
+    public static <T> Set<T> newSet(T... values)
+    {
+        return newSet(Arrays.asList(values));
+    }
+
+    /**
+     * Constructs a new {@link java.util.HashMap} instance by copying an existing Map instance.
+     */
+    public static <K, V> Map<K, V> newMap(Map<? extends K, ? extends V> map)
+    {
+        return new HashMap<K, V>(map);
+    }
+
+    /**
+     * Constructs a new thread safe map.
+     */
+    public static <K, V> Map<K, V> newThreadSafeMap()
+    {
+        return new ConcurrentHashMap<K, V>();
+    }
+
+    /** Contructs and returns a new generic {@link java.util.ArrayList} instance. */
+    public static <T> List<T> newList()
+    {
+        return new ArrayList<T>();
+    }
+
+    public static <T> List<T> newList(T... elements)
+    {
+        return newList(Arrays.asList(elements));
+    }
+
+    /** Useful for queues. */
+    public static <T> LinkedList<T> newLinkedList()
+    {
+        return new LinkedList<T>();
+    }
+
+    /** Constructs and returns a new {@link ArrayList} as a copy of the provided collection. */
+    public static <T> List<T> newList(Collection<? extends T> list)
+    {
+        return new ArrayList<T>(list);
+    }
+
+    /** Constructs and returns a new {@link java.util.concurrent.CopyOnWriteArrayList}. */
+    public static <T> List<T> newThreadSafeList()
+    {
+        return new CopyOnWriteArrayList<T>();
+    }
+}

Modified: tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java?view=diff&rev=493818&r1=493817&r2=493818
==============================================================================
--- tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java (original)
+++ tapestry/tapestry5/tapestry-ioc/trunk/src/test/java/org/apache/tapestry/ioc/internal/util/CollectionFactoryTest.java Sun Jan  7 11:23:58 2007
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2007 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.
@@ -103,6 +103,16 @@
 
         assertNotSame(copy, start);
         assertEquals(copy, start);
+    }
+
+    @Test
+    public void new_list_from_elements()
+    {
+        List<String> list = newList("Fred", "Barney");
+
+        assertEquals(list.size(), 2);
+        assertEquals(list.get(0), "Fred");
+        assertEquals(list.get(1), "Barney");
     }
 
     private static final int THREAD_COUNT = 20;