[infinispan-commits] Infinispan SVN: r1846 - trunk/core/src/main/java/org/infinispan/atomic.
infinispan-commits at lists.jboss.org
infinispan-commits at lists.jboss.org
Tue May 25 11:09:07 EDT 2010
Author: manik.surtani at jboss.com
Date: 2010-05-25 11:09:06 -0400 (Tue, 25 May 2010)
New Revision: 1846
Modified:
trunk/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java
Log:
[ISPN-450] (Add AtomicMapLookup method to use in Read Only operation without creating new map if none exists.)
Modified: trunk/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java
===================================================================
--- trunk/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java 2010-05-25 14:50:46 UTC (rev 1845)
+++ trunk/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java 2010-05-25 15:09:06 UTC (rev 1846)
@@ -1,7 +1,13 @@
package org.infinispan.atomic;
import org.infinispan.Cache;
+import org.infinispan.util.Immutables;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.infinispan.util.Immutables.immutableMapWrap;
+
/**
* A helper that locates atomic maps within a given cache. This should be the <b>only</b> way AtomicMaps are created/retrieved.
*
@@ -11,18 +17,59 @@
public class AtomicMapLookup {
/**
- * Retrieves an atomic map from a given cache, stored under a given key. If an AtomicMap did not exist, one is created
+ * Retrieves an atomic map from a given cache, stored under a given key. If an atomic map did not exist, one is created
* and registered in an atomic fashion.
+ *
* @param cache underlying cache
+ * @param key key under which the atomic map exists
+ * @param <MK> key param of the cache
* @param <K> key param of the AtomicMap
* @param <V> value param of the AtomicMap
* @return an AtomicMap
*/
@SuppressWarnings("unchecked")
- public static <MK, K, V> AtomicMap<K, V> getAtomicMap(Cache<?, ?> cache, MK key) {
+ public static <MK, K, V> AtomicMap<K, V> getAtomicMap(Cache<MK, ?> cache, MK key) {
+ return getAtomicMap(cache, key, true);
+ }
+
+ /**
+ * Retrieves an atomic map from a given cache, stored under a given key.
+ *
+ * @param cache underlying cache
+ * @param key key under which the atomic map exists
+ * @param createIfAbsent if true, a new atomic map is created if one did not exist.
+ * @param <MK> key param of the cache
+ * @param <K> key param of the AtomicMap
+ * @param <V> value param of the AtomicMap
+ * @return an AtomicMap, or null if one did not exist.
+ */
+ @SuppressWarnings("unchecked")
+ public static <MK, K, V> AtomicMap<K, V> getAtomicMap(Cache<MK, ?> cache, MK key, boolean createIfAbsent) {
Object value = cache.get(key);
- if (value == null) value = AtomicHashMap.newInstance(cache, key);
+ if (value == null) {
+ if (createIfAbsent)
+ value = AtomicHashMap.newInstance(cache, key);
+ else return null;
+ }
AtomicHashMap<K, V> castValue = (AtomicHashMap<K, V>) value;
return castValue.getProxy(cache, key, cache.getAdvancedCache().getBatchContainer(), cache.getAdvancedCache().getInvocationContextContainer());
}
+
+ /**
+ * Retrieves an atomic map from a given cache, stored under a given key, for reading only. The atomic map returned
+ * will not support updates, and if the map did not in fact exist, an empty map is returned.
+ * @param cache underlying cache
+ * @param key key under which the atomic map exists
+ * @param <MK> key param of the cache
+ * @param <K> key param of the AtomicMap
+ * @param <V> value param of the AtomicMap
+ * @return an immutable, read-only map
+ */
+ public static <MK, K, V> Map<K, V> getReadOnlyAtomicMap(Cache<MK, ?> cache, MK key) {
+ AtomicMap<K, V> am = getAtomicMap(cache, key, false);
+ if (am == null)
+ return Collections.emptyMap();
+ else
+ return immutableMapWrap(am);
+ }
}
More information about the infinispan-commits
mailing list