Author: mbrohl
Date: Sat Oct 7 12:26:35 2017 New Revision: 1811415 URL: http://svn.apache.org/viewvc?rev=1811415&view=rev Log: Improved: Fixing defects reported by FindBugs, package org.apache.ofbiz.base.util.cache. (OFBIZ-9575) Thanks Dennis Balkir for reporting and providing the patch. Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/CacheSoftReference.java ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/UtilCache.java Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/CacheSoftReference.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/CacheSoftReference.java?rev=1811415&r1=1811414&r2=1811415&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/CacheSoftReference.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/CacheSoftReference.java Sat Oct 7 12:26:35 2017 @@ -41,7 +41,7 @@ public abstract class CacheSoftReference } @Override - public void finalize() throws Throwable { + protected void finalize() throws Throwable { if (Debug.verboseOn()) { Debug.logVerbose(new Exception("UtilCache.CacheSoftRef.finalize()"), "Finalize UtilCache SoftReference - " + get(), module); } Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/UtilCache.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/UtilCache.java?rev=1811415&r1=1811414&r2=1811415&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/UtilCache.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/cache/UtilCache.java Sat Oct 7 12:26:35 2017 @@ -20,6 +20,7 @@ package org.apache.ofbiz.base.util.cache import java.io.NotSerializableException; import java.io.Serializable; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -66,10 +67,10 @@ public class UtilCache<K, V> implements public static final String module = UtilCache.class.getName(); /** A static Map to keep track of all of the UtilCache instances. */ - private static final ConcurrentHashMap<String, UtilCache<?, ?>> utilCacheTable = new ConcurrentHashMap<String, UtilCache<?, ?>>(); + private static final ConcurrentHashMap<String, UtilCache<?, ?>> utilCacheTable = new ConcurrentHashMap<>(); /** An index number appended to utilCacheTable names when there are conflicts. */ - private final static ConcurrentHashMap<String, AtomicInteger> defaultIndices = new ConcurrentHashMap<String, AtomicInteger>(); + private final static ConcurrentHashMap<String, AtomicInteger> defaultIndices = new ConcurrentHashMap<>(); /** The name of the UtilCache instance, is also the key for the instance in utilCacheTable. */ private final String name; @@ -104,7 +105,7 @@ public class UtilCache<K, V> implements protected boolean useSoftReference = false; /** The set of listeners to receive notifications when items are modified (either deliberately or because they were expired). */ - protected Set<CacheListener<K, V>> listeners = new CopyOnWriteArraySet<CacheListener<K, V>>(); + protected Set<CacheListener<K, V>> listeners = new CopyOnWriteArraySet<>(); protected ConcurrentMap<Object, CacheLine<V>> memoryTable = null; @@ -126,7 +127,7 @@ public class UtilCache<K, V> implements int maxMemSize = this.maxInMemory; if (maxMemSize == 0) maxMemSize = sizeLimit; if (maxMemSize == 0) { - memoryTable = new ConcurrentHashMap<Object, CacheLine<V>>(); + memoryTable = new ConcurrentHashMap<>(); } else { memoryTable = new Builder<Object, CacheLine<V>>() .maximumWeightedCapacity(maxMemSize) @@ -157,7 +158,7 @@ public class UtilCache<K, V> implements } } } catch (Exception e) { - Debug.logWarning(e, "Error getting " + parameter + " value from ResourceBundle for propNames: " + propNames, module); + Debug.logWarning(e, "Error getting " + parameter + " value from ResourceBundle for propNames: " + Arrays.toString(propNames), module); } return null; } @@ -234,12 +235,10 @@ public class UtilCache<K, V> implements if (useSoftReference) { if (differentExpireTime(expireTimeNanos)) { return this; - } else { - return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); } - } else { - return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); + return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); } + return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); } @Override @@ -255,13 +254,11 @@ public class UtilCache<K, V> implements CacheLine<V> changeLine(boolean useSoftReference, long expireTimeNanos) { if (useSoftReference) { return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); - } else { - if (differentExpireTime(expireTimeNanos)) { - return this; - } else { - return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); - } } + if (differentExpireTime(expireTimeNanos)) { + return this; + } + return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos); } @Override @@ -282,9 +279,8 @@ public class UtilCache<K, V> implements long loadTimeNanos = expireTimeNanos > 0 ? System.nanoTime() : 0; if (useSoftReference) { return createSoftRefCacheLine(key, value, loadTimeNanos, expireTimeNanos); - } else { - return createHardRefCacheLine(key, value, loadTimeNanos, expireTimeNanos); } + return createHardRefCacheLine(key, value, loadTimeNanos, expireTimeNanos); } private V cancel(CacheLine<V> line) { // FIXME: this is a race condition, the item could expire @@ -315,10 +311,9 @@ public class UtilCache<K, V> implements if (oldValue == null) { noteAddition(key, value); return null; - } else { - noteUpdate(key, value, oldValue); - return oldValue; } + noteUpdate(key, value, oldValue); + return oldValue; } V putIfAbsentInternal(K key, V value, long expireTimeNanos) { @@ -335,9 +330,8 @@ public class UtilCache<K, V> implements if (oldValue == null) { noteAddition(key, value); return null; - } else { - return oldValue; } + return oldValue; } /** Gets an element from the cache according to the specified key. @@ -357,7 +351,7 @@ public class UtilCache<K, V> implements } public Collection<V> values() { - List<V> valuesList = new LinkedList<V>(); + List<V> valuesList = new LinkedList<>(); for (CacheLine<V> line: memoryTable.values()) { valuesList.add(line.getValue()); } @@ -365,17 +359,16 @@ public class UtilCache<K, V> implements } private long findSizeInBytes(Object o) { + if (o == null) { + if (Debug.infoOn()) Debug.logInfo("Found null object in cache: " + getName(), module); + return 0; + } try { - if (o == null) { - if (Debug.infoOn()) Debug.logInfo("Found null object in cache: " + getName(), module); - return 0; - } if (o instanceof Serializable) { return UtilObject.getByteCount(o); - } else { - if (Debug.infoOn()) Debug.logInfo("Unable to compute memory size for non serializable object; returning 0 byte size for object of " + o.getClass(), module); - return 0; } + if (Debug.infoOn()) Debug.logInfo("Unable to compute memory size for non serializable object; returning 0 byte size for object of " + o.getClass(), module); + return 0; } catch (NotSerializableException e) { // this happens when we try to get the byte count for an object which itself is // serializable, but fails to be serialized, such as a map holding unserializable objects @@ -423,10 +416,9 @@ public class UtilCache<K, V> implements noteRemoval((K) key, oldValue); if (countRemove) removeHitCount.incrementAndGet(); return oldValue; - } else { - if (countRemove) removeMissCount.incrementAndGet(); - return null; } + if (countRemove) removeMissCount.incrementAndGet(); + return null; } protected synchronized void removeInternal(Object key, CacheLine<V> existingCacheLine) { @@ -463,7 +455,7 @@ public class UtilCache<K, V> implements } public static Set<String> getUtilCacheTableKeySet() { - Set<String> set = new HashSet<String>(utilCacheTable.size()); + Set<String> set = new HashSet<>(utilCacheTable.size()); set.addAll(utilCacheTable.keySet()); return set; } @@ -537,13 +529,12 @@ public class UtilCache<K, V> implements if (this.memoryTable instanceof ConcurrentLinkedHashMap<?, ?>) { ((ConcurrentLinkedHashMap<?, ?>) this.memoryTable).setCapacity(newInMemory); return; - } else { - this.memoryTable =new Builder<Object, CacheLine<V>>() + } + this.memoryTable =new Builder<Object, CacheLine<V>>() .maximumWeightedCapacity(newInMemory) .build(); - } } else { - this.memoryTable = new ConcurrentHashMap<Object, CacheLine<V>>(); + this.memoryTable = new ConcurrentHashMap<>(); } this.memoryTable.putAll(oldmap); @@ -627,7 +618,7 @@ public class UtilCache<K, V> implements Set<Object> keys; if (memoryTable.containsKey(ObjectType.NULL)) { - keys = new HashSet<Object>(memoryTable.keySet()); + keys = new HashSet<>(memoryTable.keySet()); keys.remove(ObjectType.NULL); keys.add(null); } else { @@ -642,7 +633,7 @@ public class UtilCache<K, V> implements } private Map<String, Object> createLineInfo(int keyNum, K key, CacheLine<V> line) { - Map<String, Object> lineInfo = new HashMap<String, Object>(); + Map<String, Object> lineInfo = new HashMap<>(); lineInfo.put("elementKey", key); if (line.getLoadTimeNanos() > 0) { @@ -654,7 +645,7 @@ public class UtilCache<K, V> implements } public Collection<? extends Map<String, Object>> getLineInfos() { - List<Map<String, Object>> lineInfos = new LinkedList<Map<String, Object>>(); + List<Map<String, Object>> lineInfos = new LinkedList<>(); int keyIndex = 0; for (K key: getCacheLineKeys()) { Object nulledKey = fromKey(key); @@ -729,49 +720,49 @@ public class UtilCache<K, V> implements UtilCache<K, V> existingCache = (UtilCache<K, V>) utilCacheTable.get(name); if (existingCache != null) return existingCache; String cacheName = name + getNextDefaultIndex(name); - UtilCache<K, V> newCache = new UtilCache<K, V>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name, names); + UtilCache<K, V> newCache = new UtilCache<>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name, names); utilCacheTable.putIfAbsent(name, newCache); return (UtilCache<K, V>) utilCacheTable.get(name); } public static <K, V> UtilCache<K, V> createUtilCache(String name, int sizeLimit, int maxInMemory, long expireTime, boolean useSoftReference, String... names) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name, names)); + return storeCache(new UtilCache<>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name, names)); } public static <K, V> UtilCache<K, V> createUtilCache(String name, int sizeLimit, int maxInMemory, long expireTime, boolean useSoftReference) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name)); + return storeCache(new UtilCache<>(cacheName, sizeLimit, maxInMemory, expireTime, useSoftReference, name)); } public static <K,V> UtilCache<K, V> createUtilCache(String name, int sizeLimit, long expireTime, boolean useSoftReference) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, sizeLimit, sizeLimit, expireTime, useSoftReference, name)); + return storeCache(new UtilCache<>(cacheName, sizeLimit, sizeLimit, expireTime, useSoftReference, name)); } public static <K,V> UtilCache<K, V> createUtilCache(String name, int sizeLimit, long expireTime) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, sizeLimit, sizeLimit, expireTime, false, name)); + return storeCache(new UtilCache<>(cacheName, sizeLimit, sizeLimit, expireTime, false, name)); } public static <K,V> UtilCache<K, V> createUtilCache(int sizeLimit, long expireTime) { String cacheName = "specified" + getNextDefaultIndex("specified"); - return storeCache(new UtilCache<K, V>(cacheName, sizeLimit, sizeLimit, expireTime, false, "specified")); + return storeCache(new UtilCache<>(cacheName, sizeLimit, sizeLimit, expireTime, false, "specified")); } public static <K,V> UtilCache<K, V> createUtilCache(String name, boolean useSoftReference) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, 0, 0, 0, useSoftReference, "default", name)); + return storeCache(new UtilCache<>(cacheName, 0, 0, 0, useSoftReference, "default", name)); } public static <K,V> UtilCache<K, V> createUtilCache(String name) { String cacheName = name + getNextDefaultIndex(name); - return storeCache(new UtilCache<K, V>(cacheName, 0, 0, 0, false, "default", name)); + return storeCache(new UtilCache<>(cacheName, 0, 0, 0, false, "default", name)); } public static <K,V> UtilCache<K, V> createUtilCache() { String cacheName = "default" + getNextDefaultIndex("default"); - return storeCache(new UtilCache<K, V>(cacheName, 0, 0, 0, false, "default")); + return storeCache(new UtilCache<>(cacheName, 0, 0, 0, false, "default")); } private static <K, V> UtilCache<K, V> storeCache(UtilCache<K, V> cache) { |
Free forum by Nabble | Edit this page |