You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2010/04/01 06:42:21 UTC

svn commit: r929831 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java

Author: doogie
Date: Thu Apr  1 04:42:21 2010
New Revision: 929831

URL: http://svn.apache.org/viewvc?rev=929831&view=rev
Log:
Simplify logic in getNextDefaultIndex, so that it's possible to get full
coverage on all it's lines.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java?rev=929831&r1=929830&r2=929831&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java Thu Apr  1 04:42:21 2010
@@ -30,6 +30,7 @@ import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
 import javolution.util.FastList;
@@ -58,7 +59,7 @@ public class UtilCache<K, V> implements 
     private static final ConcurrentHashMap<String, UtilCache<?, ?>> utilCacheTable = new ConcurrentHashMap<String, UtilCache<?, ?>>();
 
     /** An index number appended to utilCacheTable names when there are conflicts. */
-    private final static ConcurrentHashMap<String, Integer> defaultIndices = new ConcurrentHashMap<String, Integer>();
+    private final static ConcurrentHashMap<String, AtomicInteger> defaultIndices = new ConcurrentHashMap<String, AtomicInteger>();
 
     /** The name of the UtilCache instance, is also the key for the instance in utilCacheTable. */
     private final String name;
@@ -124,21 +125,13 @@ public class UtilCache<K, V> implements 
     }
 
     private static String getNextDefaultIndex(String cacheName) {
-        Integer curInd;
-        do {
+        AtomicInteger curInd = defaultIndices.get(cacheName);
+        if (curInd == null) {
+            defaultIndices.putIfAbsent(cacheName, new AtomicInteger(0));
             curInd = defaultIndices.get(cacheName);
-            if (curInd == null) {
-                if (defaultIndices.putIfAbsent(cacheName, 1) == null) {
-                    // no one else was able to store a value before us
-                    break;
-                }
-            } else if (defaultIndices.replace(cacheName, curInd, curInd + 1)) {
-                // replaced the current value, so we know that this iteration
-                // has control over curInd
-                break;
-            }
-        } while (true);
-        return curInd == null ? "" : Integer.toString(curInd + 1);
+        }
+        int i = curInd.getAndIncrement();
+        return i == 0 ? "" : Integer.toString(i);
     }
 
     public static String getPropertyParam(ResourceBundle res, String[] propNames, String parameter) {