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:24 UTC
svn commit: r1094824 - 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:24 2011
New Revision: 1094824
URL: http://svn.apache.org/viewvc?rev=1094824&view=rev
Log:
TAP5-1508: Add NamedSet.putIfNew() and NamedSet.create() 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=1094824&r1=1094823&r2=1094824&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:24 2011
@@ -130,4 +130,45 @@ public class NamedSet<T>
else
prev.next = newRef;
}
+
+ /**
+ * Puts a new value, but only if it does not already exist.
+ *
+ * @param name
+ * name to store (comparisons are case insensitive) may not be blank
+ * @param newValue
+ * non-null value to store
+ * @return true if value stored, false if name already exists
+ */
+ public synchronized boolean putIfNew(String name, T newValue)
+ {
+ assert InternalUtils.isNonBlank(name);
+ assert newValue != null;
+
+ NamedRef<T> prev = null;
+ NamedRef<T> cursor = first;
+
+ while (cursor != null)
+ {
+ if (cursor.name.equalsIgnoreCase(name)) { return false; }
+
+ prev = cursor;
+ cursor = cursor.next;
+ }
+
+ NamedRef<T> newRef = new NamedRef<T>(name, newValue);
+
+ if (prev == null)
+ first = newRef;
+ else
+ prev.next = newRef;
+
+ return true;
+ }
+
+ /** Convienience method for creating a new, empty set. */
+ public static <T> NamedSet<T> create()
+ {
+ return new NamedSet<T>();
+ }
}
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=1094824&r1=1094823&r2=1094824&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:24 2011
@@ -57,6 +57,26 @@ class NamedSetTests extends Assert
}
@Test
+ void put_if_new_does_not_overrwrite() {
+ NamedSet ns = new NamedSet()
+
+ ns.put "Fred", 100
+ ns.put "Barney", 200
+
+ assert ns.get("fred") == 100
+ assert ns.get("barney") == 200
+
+ assert ns.putIfNew("FRED", 110) == false
+ assert ns.putIfNew("Wilma", 300) == true
+
+ assert ns.get("fred") == 100
+ assert ns.get("barney") == 200
+ assert ns.get("wilma") == 300
+
+ assert ns.names.sort() == ["Barney", "Fred", "Wilma"]
+ }
+
+ @Test
void missing_key_returns_null() {
NamedSet ns = new NamedSet()