Author: manik.surtani(a)jboss.com
Date: 2009-03-18 04:43:29 -0400 (Wed, 18 Mar 2009)
New Revision: 7907
Modified:
core/trunk/src/main/java/org/jboss/cache/Cache.java
core/trunk/src/main/java/org/jboss/cache/Node.java
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java
core/trunk/src/test/java/org/jboss/cache/api/CacheAPITest.java
core/trunk/src/test/java/org/jboss/cache/api/NodeAPITest.java
core/trunk/src/test/java/org/jboss/cache/mock/NodeSpiMock.java
Log:
JBCACHE-1492 - Add a hasChildren() (or equivalent) method to the Node interface
Modified: core/trunk/src/main/java/org/jboss/cache/Cache.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/Cache.java 2009-03-16 20:38:11 UTC (rev
7906)
+++ core/trunk/src/main/java/org/jboss/cache/Cache.java 2009-03-18 08:43:29 UTC (rev
7907)
@@ -281,6 +281,20 @@
Set<String> getChildrenNames(String fqn);
/**
+ * Tests if a node is a leaf, i.e., doesn't have any children
+ * @param fqn fqn to test
+ * @return true if it is a leaf, false otherwise
+ */
+ boolean isLeaf(Fqn fqn);
+
+ /**
+ * Tests if a node is a leaf, i.e., doesn't have any children
+ * @param fqn fqn to test
+ * @return true if it is a leaf, false otherwise
+ */
+ boolean isLeaf(String fqn);
+
+ /**
* Convenience method that allows for direct access to the data in a {@link Node}.
*
* @param fqn <b><i>absolute</i></b> {@link Fqn} to the {@link
Node} to be accessed.
Modified: core/trunk/src/main/java/org/jboss/cache/Node.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/Node.java 2009-03-16 20:38:11 UTC (rev 7906)
+++ core/trunk/src/main/java/org/jboss/cache/Node.java 2009-03-18 08:43:29 UTC (rev 7907)
@@ -341,4 +341,9 @@
* @param recursive if true, child nodes will have their object references released as
well.
*/
void releaseObjectReferences(boolean recursive);
+
+ /**
+ * @return true if the current node is a leaf node (i.e., has no children), or false
otherwise.
+ */
+ boolean isLeaf();
}
Modified:
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2009-03-16
20:38:11 UTC (rev 7906)
+++
core/trunk/src/main/java/org/jboss/cache/invocation/CacheInvocationDelegate.java 2009-03-18
08:43:29 UTC (rev 7907)
@@ -621,9 +621,33 @@
batchContainer.endBatch(successful);
}
- @SuppressWarnings("unchecked")
+ public boolean isLeaf(String fqn) throws NodeNotExistsException
+ {
+ return isLeaf(Fqn.fromString(fqn));
+ }
+
+ public boolean isLeaf(Fqn fqn) throws NodeNotExistsException
+ {
+ Set<Object> names = getChildrenNamesInternal(fqn);
+ if (names == null) throw new NodeNotExistsException("Node " + fqn +
" does not exist!");
+ return names.isEmpty();
+ }
+
+
public Set<Object> getChildrenNames(Fqn fqn)
{
+ Set<Object> names = getChildrenNamesInternal(fqn);
+ return names == null ? Collections.emptySet() : names;
+ }
+
+ /**
+ * Will return a null if the node doesnt exist!
+ * @param fqn to check
+ * @return set or null
+ */
+ @SuppressWarnings("unchecked")
+ private Set<Object> getChildrenNamesInternal(Fqn fqn)
+ {
InvocationContext ctx = invocationContextContainer.get();
cacheStatusCheck(ctx);
GetChildrenNamesCommand command =
commandsFactory.buildGetChildrenNamesCommand(fqn);
@@ -643,11 +667,7 @@
{
retval = Immutables.immutableSetWrap(retval); // this is already copied in the
command
}
- else
- {
- retval = Collections.emptySet();
- }
-
+
return retval;
}
Modified: core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java 2009-03-16
20:38:11 UTC (rev 7906)
+++
core/trunk/src/main/java/org/jboss/cache/invocation/NodeInvocationDelegate.java 2009-03-18
08:43:29 UTC (rev 7907)
@@ -293,6 +293,12 @@
return spi.getChildrenNames(getFqn());
}
+ public boolean isLeaf()
+ {
+ assertValid();
+ return getChildrenNames().isEmpty();
+ }
+
public Map<K, V> getData()
{
assertValid();
Modified: core/trunk/src/test/java/org/jboss/cache/api/CacheAPITest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/CacheAPITest.java 2009-03-16 20:38:11 UTC
(rev 7906)
+++ core/trunk/src/test/java/org/jboss/cache/api/CacheAPITest.java 2009-03-18 08:43:29 UTC
(rev 7907)
@@ -1,6 +1,14 @@
package org.jboss.cache.api;
-import org.jboss.cache.*;
+import org.jboss.cache.AbstractSingleCacheTest;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.Region;
+import org.jboss.cache.UnitTestCacheFactory;
+import org.jboss.cache.NodeNotExistsException;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Configuration.NodeLockingScheme;
import org.jboss.cache.config.ConfigurationException;
@@ -8,11 +16,8 @@
import org.jboss.cache.notifications.annotation.NodeCreated;
import org.jboss.cache.notifications.event.Event;
import org.jboss.cache.transaction.GenericTransactionManagerLookup;
-import org.jboss.cache.util.CachePrinter;
-import org.jboss.cache.util.TestingUtil;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.transaction.TransactionManager;
@@ -355,6 +360,32 @@
assert cache.peek(Fqn.fromString("/a"), true, true) == null;
}
+ public void testIsLeaf()
+ {
+ cache.put("/a/b/c", "k", "v");
+ cache.put("/a/d", "k", "v");
+
+ assert !cache.isLeaf(Fqn.ROOT);
+ assert !cache.isLeaf("/a");
+ assert !cache.isLeaf("/a/b");
+ assert cache.isLeaf("/a/d");
+ assert cache.isLeaf("/a/b/c");
+
+ cache.removeNode("/a/b");
+ cache.removeNode("/a/d");
+
+ assert cache.isLeaf("/a");
+ try
+ {
+ assert cache.isLeaf("/a/b");
+ assert false;
+ }
+ catch (NodeNotExistsException expected)
+ {
+ assert true;
+ }
+ }
+
public void testRpcManagerElements()
{
assertEquals("CacheMode.LOCAL cache has no address", null,
cache.getLocalAddress());
Modified: core/trunk/src/test/java/org/jboss/cache/api/NodeAPITest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/NodeAPITest.java 2009-03-16 20:38:11 UTC
(rev 7906)
+++ core/trunk/src/test/java/org/jboss/cache/api/NodeAPITest.java 2009-03-18 08:43:29 UTC
(rev 7907)
@@ -461,4 +461,27 @@
assertNull(cache.get("/foo/1/2", "item"));
assertNull(cache.get("/foo/1", "item"));
}
+
+ public void testIsLeaf()
+ {
+ cache.put("/a/b/c", "k", "v");
+ cache.put("/a/d", "k", "v");
+
+ Node A = cache.getNode("/a");
+ Node B = cache.getNode("/a/b");
+ Node C = cache.getNode("/a/b/c");
+ Node D = cache.getNode("/a/d");
+ Node root = cache.getRoot();
+
+ assert !root.isLeaf();
+ assert !A.isLeaf();
+ assert !B.isLeaf();
+ assert C.isLeaf();
+ assert D.isLeaf();
+
+ cache.removeNode("/a/b");
+ cache.removeNode("/a/d");
+
+ assert A.isLeaf();
+ }
}
Modified: core/trunk/src/test/java/org/jboss/cache/mock/NodeSpiMock.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/mock/NodeSpiMock.java 2009-03-16 20:38:11 UTC
(rev 7906)
+++ core/trunk/src/test/java/org/jboss/cache/mock/NodeSpiMock.java 2009-03-18 08:43:29 UTC
(rev 7907)
@@ -485,4 +485,9 @@
public void releaseObjectReferences(boolean recursive)
{
}
+
+ public boolean isLeaf()
+ {
+ return false;
+ }
}