You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/04/19 02:18:28 UTC

svn commit: r1094825 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/util/NamedSet.java test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy

Author: hlship
Date: Tue Apr 19 00:18:28 2011
New Revision: 1094825

URL: http://svn.apache.org/viewvc?rev=1094825&view=rev
Log:
TAP5-1508: Add methods for extracting the values from a set, and null-safe static convenience methods

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/NamedSet.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/NamedSet.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/NamedSet.java?rev=1094825&r1=1094824&r2=1094825&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/NamedSet.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/NamedSet.java Tue Apr 19 00:18:28 2011
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.internal.util;
 
+import java.util.Collections;
 import java.util.Set;
 
 import org.apache.commons.collections.map.CaseInsensitiveMap;
@@ -69,6 +70,24 @@ public class NamedSet<T>
     }
 
     /**
+     * Returns a set of all the values in the set.
+     */
+    public synchronized Set<T> getValues()
+    {
+        Set<T> result = CollectionFactory.newSet();
+
+        NamedRef<T> cursor = first;
+
+        while (cursor != null)
+        {
+            result.add(cursor.value);
+            cursor = cursor.next;
+        }
+
+        return result;
+    }
+
+    /**
      * Gets the value for the provided name.
      * 
      * @param name
@@ -171,4 +190,39 @@ public class NamedSet<T>
     {
         return new NamedSet<T>();
     }
+
+    /**
+     * Convienience method for getting a value from a set that may be null.
+     * 
+     * @param <T>
+     * @param set
+     *            set to search, may be null
+     * @param name
+     *            name to lookup
+     * @return value from set, or null if not found, or if set is null
+     */
+    public static <T> T get(NamedSet<T> set, String name)
+    {
+        return set == null ? null : set.get(name);
+    }
+
+    /**
+     * Gets the names in the set, returning an empty set if the NamedSet is null.
+     */
+    public static Set<String> getNames(NamedSet<?> set)
+    {
+        if (set == null)
+            return Collections.emptySet();
+
+        return set.getNames();
+    }
+
+    /** Returns the values in the set, returning an empty set if the NamedSet is null. */
+    public static <T> Set<T> getValues(NamedSet<T> set)
+    {
+        if (set == null)
+            return Collections.emptySet();
+
+        return set.getValues();
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy?rev=1094825&r1=1094824&r2=1094825&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/util/NamedSetTests.groovy Tue Apr 19 00:18:28 2011
@@ -38,6 +38,16 @@ class NamedSetTests extends Assert
     }
 
     @Test
+    void get_values_in_set() {
+        NamedSet ns = new NamedSet()
+
+        ns.put "Fred", 100
+        ns.put "Barney", 200
+
+        assert ns.values == [100, 200] as Set
+    }
+
+    @Test
     void replace_a_named_value() {
         NamedSet ns = new NamedSet()