[utilities COMMIT] in /java-support/trunk/src: main/java/net/shibboleth/utilities/java/support/collection/IndexingObj...

noreply at shibboleth.net noreply at shibboleth.net
Wed Jun 11 18:52:00 EDT 2014


Author: putmanb
Date: Wed Jun 11 18:52:00 2014
New Revision: 597

URL: http://svn.shibboleth.net/view/utilities?rev=597&view=rev
Log:
Make sure all public methods access shared data under a read lock, to avoid race conditions.
Add containsInstance(T) method.
Rename contains(String) -> containsIndex(String), to more clearly differentiate from the other contains method.

Modified:
    java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStore.java
    java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStoreTest.java

Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStore.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStore.java?rev=597&r1=596&r2=597&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStore.java (original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStore.java Wed Jun 11 18:52:00 2014
@@ -94,7 +94,7 @@
      * 
      * @return true if an object is associated with the given index, false if not
      */
-    public boolean contains(String index) {
+    public boolean containsIndex(String index) {
         Lock readLock = rwLock.readLock();
         readLock.lock();
         try {
@@ -105,12 +105,40 @@
     }
 
     /**
+     * Checks whether the store contains an object instance equal to the specified one.
+     * 
+     * @param instance the object instance to check
+     * 
+     * @return true if an object instance equal to the specified one is stored, false if not
+     */
+    public boolean containsInstance(T instance) {
+        Lock readLock = rwLock.readLock();
+        readLock.lock();
+        try {
+            Integer index = indexStore.get(instance);
+            if (index == null) {
+                return false;
+            } else {
+                return objectStore.containsKey(index.toString());
+            }
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
      * Checks if the store is empty.
      * 
      * @return true if the store is empty, false if not
      */
     public boolean isEmpty() {
-        return objectStore.isEmpty();
+        Lock readLock = rwLock.readLock();
+        readLock.lock();
+        try {
+            return objectStore.isEmpty();
+        } finally {
+            readLock.unlock();
+        }
     }
 
     /**
@@ -205,7 +233,13 @@
      * @return number of items in the store
      */
     public int size() {
-        return objectStore.size();
+        Lock readLock = rwLock.readLock();
+        readLock.lock();
+        try {
+            return objectStore.size();
+        } finally {
+            readLock.unlock();
+        }
     }
     
     /**

Modified: java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStoreTest.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStoreTest.java?rev=597&r1=596&r2=597&view=diff
==============================================================================
--- java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStoreTest.java (original)
+++ java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/IndexingObjectStoreTest.java Wed Jun 11 18:52:00 2014
@@ -33,41 +33,47 @@
 
         Assert.assertTrue(store.isEmpty());
         Assert.assertEquals(store.size(), 0);
-        Assert.assertFalse(store.contains("foo"));
+        Assert.assertFalse(store.containsInstance("foo"));
 
         String nullIndex = store.put(null);
         Assert.assertNull(nullIndex);
         Assert.assertTrue(store.isEmpty());
         Assert.assertEquals(store.size(), 0);
+        Assert.assertFalse(store.containsInstance(null));
 
         String str1Index = store.put(str1);
-        Assert.assertTrue(store.contains(str1Index));
+        Assert.assertTrue(store.containsIndex(str1Index));
+        Assert.assertTrue(store.containsInstance("foo"));
         Assert.assertFalse(store.isEmpty());
         Assert.assertEquals(store.size(), 1);
         Assert.assertEquals(store.get(str1Index), str1);
 
         String index1 = store.put("foo");
-        Assert.assertTrue(store.contains(index1));
+        Assert.assertTrue(store.containsIndex(index1));
+        Assert.assertTrue(store.containsInstance("foo"));
         Assert.assertFalse(store.isEmpty());
         Assert.assertEquals(store.size(), 1);
         Assert.assertEquals(index1, str1Index);
         Assert.assertEquals(store.get(index1), str1);
 
         store.remove(str1Index);
-        Assert.assertTrue(store.contains(index1));
+        Assert.assertTrue(store.containsIndex(index1));

[... 42 lines stripped ...]


More information about the commits mailing list