From f15d2f41d8848c5c0049fc6bf2a7d75e5a4e73ca Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Wed, 17 Oct 2007 20:33:02 +0000 Subject: [PATCH] Add java 1.5 generics and enhanced-for to all UtilCache classes. Closes https://issues.apache.org/jira/browse/OFBIZ-1306 git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@585673 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/ofbiz/base/util/cache/CacheLine.java | 4 +- .../ofbiz/base/util/cache/CacheLineTable.java | 53 +++---- .../ofbiz/base/util/cache/CacheListener.java | 8 +- .../base/util/cache/CacheSoftReference.java | 6 +- .../base/util/cache/HardRefCacheLine.java | 10 +- .../base/util/cache/SoftRefCacheLine.java | 14 +- .../org/ofbiz/base/util/cache/UtilCache.java | 145 ++++++++---------- 7 files changed, 107 insertions(+), 133 deletions(-) diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java b/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java index b7bb409cd6..8c87324c1f 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java @@ -22,7 +22,7 @@ import java.io.Serializable; import org.ofbiz.base.util.UtilObject; -public abstract class CacheLine implements Serializable { +public abstract class CacheLine implements Serializable { public long loadTime; public final long expireTime; @@ -35,7 +35,7 @@ public abstract class CacheLine implements Serializable { this.loadTime = loadTime; } - public abstract Object getValue(); + public abstract V getValue(); public abstract boolean isInvalid(); public long getExpireTime() { diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/CacheLineTable.java b/framework/base/src/base/org/ofbiz/base/util/cache/CacheLineTable.java index fbd3f32759..d83d4fd4b4 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/CacheLineTable.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/CacheLineTable.java @@ -36,18 +36,18 @@ import org.ofbiz.base.util.ObjectType; import org.ofbiz.base.util.collections.LRUMap; -public class CacheLineTable implements Serializable { +public class CacheLineTable implements Serializable { public static final String module = CacheLineTable.class.getName(); protected static transient jdbm.RecordManager jdbmMgr = null; protected transient jdbm.htree.HTree fileTable = null; - protected Map memoryTable = null; + protected Map> memoryTable = null; protected String fileStore = null; protected String cacheName = null; protected int maxInMemory = 0; protected boolean isNullSet = false; - protected CacheLine nullValue = null; + protected CacheLine nullValue = null; public CacheLineTable(String fileStore, String cacheName, boolean useFileSystemStore, int maxInMemory) { this.fileStore = fileStore; @@ -85,23 +85,23 @@ public class CacheLineTable implements Serializable { this.setLru(maxInMemory); } - public synchronized CacheLine put(Object key, CacheLine value) { - CacheLine oldValue; + public synchronized CacheLine put(K key, CacheLine value) { + CacheLine oldValue; if (key == null) { if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to put with null key, using NullObject" + this.cacheName, module); if (memoryTable instanceof LRUMap) { - oldValue = (CacheLine) memoryTable.put(key, value); + oldValue = memoryTable.put(key, value); } else { oldValue = isNullSet ? nullValue : null; isNullSet = true; nullValue = value; } } else { - oldValue = (CacheLine) memoryTable.put(key, value); + oldValue = memoryTable.put(key, value); } if (fileTable != null) { try { - if (oldValue == null) oldValue = (CacheLine) fileTable.get(key); + if (oldValue == null) oldValue = (CacheLine) fileTable.get(key); fileTable.put(key != null ? key : ObjectType.NULL, value); CacheLineTable.jdbmMgr.commit(); } catch (IOException e) { @@ -111,17 +111,17 @@ public class CacheLineTable implements Serializable { return oldValue; } - public CacheLine get(Object key) { + public CacheLine get(Object key) { if (key == null) { if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to get with null key, using NullObject" + this.cacheName, module); } return getNoCheck(key); } - protected CacheLine getNoCheck(Object key) { - CacheLine value; + protected CacheLine getNoCheck(Object key) { + CacheLine value; if (memoryTable instanceof LRUMap) { - value = (CacheLine) memoryTable.get(key); + value = memoryTable.get(key); } else { if (key == null) { value = isNullSet ? nullValue : null; @@ -132,7 +132,7 @@ public class CacheLineTable implements Serializable { if (value == null) { if (fileTable != null) { try { - value = (CacheLine) fileTable.get(key != null ? key : ObjectType.NULL); + value = (CacheLine) fileTable.get(key != null ? key : ObjectType.NULL); } catch (IOException e) { Debug.logError(e, module); } @@ -141,11 +141,11 @@ public class CacheLineTable implements Serializable { return value; } - public synchronized CacheLine remove(Object key) { + public synchronized CacheLine remove(Object key) { if (key == null) { if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to remove with null key, using NullObject" + this.cacheName, module); } - CacheLine value = this.getNoCheck(key); + CacheLine value = this.getNoCheck(key); if (fileTable != null) { try { fileTable.remove(key != null ? key : ObjectType.NULL); @@ -166,15 +166,15 @@ public class CacheLineTable implements Serializable { return value; } - public synchronized Collection values() { - List values = FastList.newInstance(); + public synchronized Collection> values() { + List> values = FastList.newInstance(); if (fileTable != null) { try { jdbm.helper.FastIterator iter = fileTable.values(); Object value = iter.next(); while (value != null) { - values.add(value); + values.add((CacheLine) value); value = iter.next(); } } catch (IOException e) { @@ -192,9 +192,9 @@ public class CacheLineTable implements Serializable { * * @return An unmodifiable Set for the keys for this cache; to remove while iterating call the remove method on this class. */ - public synchronized Set keySet() { + public synchronized Set keySet() { // note that this must be a HashSet and not a FastSet in order to have a null value - Set keys = new HashSet(); + Set keys = new HashSet(); if (fileTable != null) { try { @@ -204,7 +204,7 @@ public class CacheLineTable implements Serializable { if (key instanceof ObjectType.NullObject) { keys.add(null); } else { - keys.add(key); + keys.add((K) key); } } } catch (IOException e) { @@ -255,7 +255,7 @@ public class CacheLineTable implements Serializable { public synchronized void setLru(int newSize) { this.maxInMemory = newSize; - Map oldmap = null; + Map> oldmap = null; if (this.memoryTable != null) { // using linked map to preserve the order when using LRU (FastMap is a linked map) oldmap = FastMap.newInstance(); @@ -263,7 +263,7 @@ public class CacheLineTable implements Serializable { } if (newSize > 0) { - this.memoryTable = new LRUMap(newSize); + this.memoryTable = new LRUMap>(newSize); if (isNullSet) { this.memoryTable.put(null, nullValue); isNullSet = false; @@ -278,9 +278,7 @@ public class CacheLineTable implements Serializable { } } - public synchronized Object getKeyFromMemory(int index) { - Iterator i = memoryTable.keySet().iterator(); - + public synchronized K getKeyFromMemory(int index) { int currentIdx = 0; if (isNullSet) { if (currentIdx == index) { @@ -288,8 +286,7 @@ public class CacheLineTable implements Serializable { } currentIdx++; } - while (i.hasNext()) { - Object key = i.next(); + for (K key: memoryTable.keySet()) { if (currentIdx == index) { return key; } diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/CacheListener.java b/framework/base/src/base/org/ofbiz/base/util/cache/CacheListener.java index 9e8639a5ba..5c684c73d6 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/CacheListener.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/CacheListener.java @@ -18,11 +18,11 @@ *******************************************************************************/ package org.ofbiz.base.util.cache; -public interface CacheListener { +public interface CacheListener { - public void noteKeyRemoval(UtilCache cache, Object key, Object oldValue); + public void noteKeyRemoval(UtilCache cache, K key, V oldValue); - public void noteKeyAddition(UtilCache cache, Object key, Object newValue); + public void noteKeyAddition(UtilCache cache, K key, V newValue); - public void noteKeyUpdate(UtilCache cache, Object key, Object newValue, Object oldValue); + public void noteKeyUpdate(UtilCache cache, K key, V newValue, V oldValue); } diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/CacheSoftReference.java b/framework/base/src/base/org/ofbiz/base/util/cache/CacheSoftReference.java index 23a28b6fe5..8f2af8e388 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/CacheSoftReference.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/CacheSoftReference.java @@ -24,15 +24,15 @@ import java.io.Serializable; import org.ofbiz.base.util.Debug; -public class CacheSoftReference extends SoftReference implements Serializable { +public class CacheSoftReference extends SoftReference implements Serializable { public static final String module = CacheSoftReference.class.getName(); - public CacheSoftReference(Object o) { + public CacheSoftReference(V o) { super(o); } - public CacheSoftReference(Object o, ReferenceQueue referenceQueue) { + public CacheSoftReference(V o, ReferenceQueue referenceQueue) { super(o, referenceQueue); } diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/HardRefCacheLine.java b/framework/base/src/base/org/ofbiz/base/util/cache/HardRefCacheLine.java index 4781bf24c2..06ee7644c1 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/HardRefCacheLine.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/HardRefCacheLine.java @@ -18,20 +18,20 @@ *******************************************************************************/ package org.ofbiz.base.util.cache; -public final class HardRefCacheLine extends CacheLine { - public final Object value; +public final class HardRefCacheLine extends CacheLine { + public final V value; - public HardRefCacheLine(Object value, long expireTime) { + public HardRefCacheLine(V value, long expireTime) { super(expireTime); this.value = value; } - public HardRefCacheLine(Object value, long loadTime, long expireTime) { + public HardRefCacheLine(V value, long loadTime, long expireTime) { super(loadTime, expireTime); this.value = value; } - public Object getValue() { + public V getValue() { return value; } diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/SoftRefCacheLine.java b/framework/base/src/base/org/ofbiz/base/util/cache/SoftRefCacheLine.java index 1860d3c1b1..6d6da5d417 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/SoftRefCacheLine.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/SoftRefCacheLine.java @@ -18,20 +18,20 @@ *******************************************************************************/ package org.ofbiz.base.util.cache; -public final class SoftRefCacheLine extends CacheLine { - public final CacheSoftReference ref; +public final class SoftRefCacheLine extends CacheLine { + public final CacheSoftReference ref; - public SoftRefCacheLine(Object value, long expireTime) { + public SoftRefCacheLine(V value, long expireTime) { super(expireTime); - this.ref = new CacheSoftReference(value); + this.ref = new CacheSoftReference(value); } - public SoftRefCacheLine(Object value, long loadTime, long expireTime) { + public SoftRefCacheLine(V value, long loadTime, long expireTime) { super(loadTime, expireTime); - this.ref = new CacheSoftReference(value); + this.ref = new CacheSoftReference(value); } - public Object getValue() { + public V getValue() { return ref.get(); } diff --git a/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java b/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java index e55a376382..93e6510d10 100644 --- a/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java +++ b/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java @@ -46,21 +46,21 @@ import org.ofbiz.base.util.UtilValidate; * * */ -public class UtilCache implements Serializable { +public class UtilCache implements Serializable { public static final String module = UtilCache.class.getName(); /** A static Map to keep track of all of the UtilCache instances. */ - public static Map utilCacheTable = new WeakHashMap(); + public static Map> utilCacheTable = new WeakHashMap>(); /** An index number appended to utilCacheTable names when there are conflicts. */ - protected static Map defaultIndices = FastMap.newInstance(); + protected static Map defaultIndices = FastMap.newInstance(); /** The name of the UtilCache instance, is also the key for the instance in utilCacheTable. */ protected String name = null; /** A hashtable containing a CacheLine object with a value and a loadTime for each element. */ - public CacheLineTable cacheLineTable = null; + public CacheLineTable cacheLineTable = null; /** A count of the number of cache hits */ protected long hitCount = 0; @@ -96,7 +96,7 @@ public class UtilCache implements Serializable { private String fileStore = "runtime/data/utilcache"; /** The set of listeners to receive notifcations when items are modidfied(either delibrately or because they were expired). */ - protected Set listeners = FastSet.newInstance(); + protected Set> listeners = FastSet.newInstance(); /** Constructor which specifies the cacheName as well as the maxSize, expireTime and useSoftReference. * The passed maxSize, expireTime and useSoftReference will be overridden by values from cache.properties if found. @@ -183,7 +183,7 @@ public class UtilCache implements Serializable { } protected String getNextDefaultIndex(String cacheName) { - Integer curInd = (Integer) UtilCache.defaultIndices.get(cacheName); + Integer curInd = UtilCache.defaultIndices.get(cacheName); if (curInd == null) { UtilCache.defaultIndices.put(cacheName, 1); @@ -196,9 +196,9 @@ public class UtilCache implements Serializable { public static String getPropertyParam(ResourceBundle res, String[] propNames, String parameter) { String value = null; - for (int i = 0; i < propNames.length && value == null; i++ ) { + for (String propName: propNames) { try { - value = res.getString(propNames[i] + '.' + parameter); + value = res.getString(propName + '.' + parameter); } catch (MissingResourceException e) {} } // don't need this, just return null @@ -268,14 +268,14 @@ public class UtilCache implements Serializable { int maxMemSize = this.maxInMemory; if (maxMemSize == 0) maxMemSize = maxSize; - this.cacheLineTable = new CacheLineTable(this.fileStore, this.name, this.useFileSystemStore, maxMemSize); + this.cacheLineTable = new CacheLineTable(this.fileStore, this.name, this.useFileSystemStore, maxMemSize); } /** Puts or loads the passed element into the cache * @param key The key for the element, used to reference it in the hastables and LRU linked list * @param value The value of the element */ - public synchronized Object put(Object key, Object value) { + public V put(K key, V value) { return put(key, value, expireTime); } @@ -284,13 +284,13 @@ public class UtilCache implements Serializable { * @param value The value of the element * @param expireTime how long to keep this key in the cache */ - public synchronized Object put(Object key, Object value, long expireTime) { - CacheLine oldCacheLine; - CacheLine newCacheLine; + public V put(K key, V value, long expireTime) { + CacheLine oldCacheLine; + CacheLine newCacheLine; if (expireTime > 0) { - newCacheLine = useSoftReference ? new SoftRefCacheLine(value, System.currentTimeMillis(), expireTime) : new HardRefCacheLine(value, System.currentTimeMillis(), expireTime); + newCacheLine = useSoftReference ? new SoftRefCacheLine(value, System.currentTimeMillis(), expireTime) : new HardRefCacheLine(value, System.currentTimeMillis(), expireTime); } else { - newCacheLine = useSoftReference ? new SoftRefCacheLine(value, expireTime) : new HardRefCacheLine(value, expireTime); + newCacheLine = useSoftReference ? new SoftRefCacheLine(value, expireTime) : new HardRefCacheLine(value, expireTime); } oldCacheLine = cacheLineTable.put(key, newCacheLine); @@ -309,8 +309,8 @@ public class UtilCache implements Serializable { * @param key The key for the element, used to reference it in the hastables and LRU linked list * @return The value of the element specified by the key */ - public Object get(Object key) { - CacheLine line = getInternal(key, true); + public V get(Object key) { + CacheLine line = getInternal(key, true); if (line == null) { return null; } else { @@ -318,13 +318,13 @@ public class UtilCache implements Serializable { } } - protected CacheLine getInternalNoCheck(Object key) { - CacheLine line = cacheLineTable.get(key); + protected CacheLine getInternalNoCheck(Object key) { + CacheLine line = cacheLineTable.get(key); return line; } - protected CacheLine getInternal(Object key, boolean countGet) { - CacheLine line = getInternalNoCheck(key); + protected CacheLine getInternal(Object key, boolean countGet) { + CacheLine line = getInternalNoCheck(key); if (line == null) { if (countGet) missCountNotFound++; } else if (line.isInvalid()) { @@ -343,15 +343,13 @@ public class UtilCache implements Serializable { return line; } - public List values() { + public List values() { if (cacheLineTable == null) { return null; } - List valuesList = FastList.newInstance(); - Iterator i = cacheLineTable.keySet().iterator(); - while (i.hasNext()) { - Object key = i.next(); + List valuesList = FastList.newInstance(); + for (K key: cacheLineTable.keySet()) { valuesList.add(this.get(key)); } @@ -360,9 +358,8 @@ public class UtilCache implements Serializable { public long getSizeInBytes() { long totalSize = 0; - Iterator i = cacheLineTable.values().iterator(); - while (i.hasNext()) { - totalSize += ((CacheLine) i.next()).getSizeInBytes(); + for (CacheLine line: cacheLineTable.values()) { + totalSize += line.getSizeInBytes(); } return totalSize; } @@ -371,15 +368,15 @@ public class UtilCache implements Serializable { * @param key The key for the element, used to reference it in the hastables and LRU linked list * @return The value of the removed element specified by the key */ - public synchronized Object remove(Object key) { + public V remove(Object key) { return this.removeInternal(key, true); } /** This is used for internal remove calls because we only want to count external calls */ - protected synchronized Object removeInternal(Object key, boolean countRemove) { - CacheLine line = cacheLineTable.remove(key); + protected synchronized V removeInternal(Object key, boolean countRemove) { + CacheLine line = cacheLineTable.remove(key); if (line != null) { - noteRemoval(key, line.getValue()); + noteRemoval((K) key, line.getValue()); if (countRemove) this.removeHitCount++; return line.getValue(); } else { @@ -390,10 +387,8 @@ public class UtilCache implements Serializable { /** Removes all elements from this cache */ public synchronized void clear() { - Iterator it = cacheLineTable.keySet().iterator(); - while (it.hasNext()) { - Object key = it.next(); - CacheLine line = getInternalNoCheck(key); + for (K key: cacheLineTable.keySet()) { + CacheLine line = getInternalNoCheck(key); noteRemoval(key, line == null ? null : line.getValue()); } cacheLineTable.clear(); @@ -402,10 +397,8 @@ public class UtilCache implements Serializable { /** Removes all elements from this cache */ public static void clearAllCaches() { - Iterator entries = utilCacheTable.entrySet().iterator(); - while (entries.hasNext()) { - Map.Entry entry = (Map.Entry) entries.next(); - UtilCache utilCache = (UtilCache) entry.getValue(); + for (Map.Entry> entry: utilCacheTable.entrySet()) { + UtilCache utilCache = entry.getValue(); utilCache.clear(); } } @@ -495,9 +488,7 @@ public class UtilCache implements Serializable { // if expire time was <= 0 and is now greater, fill expire table now if (this.expireTime <= 0 && expireTime > 0) { long currentTime = System.currentTimeMillis(); - Iterator values = cacheLineTable.values().iterator(); - while (values.hasNext()) { - CacheLine line = (CacheLine) values.next(); + for (CacheLine line: cacheLineTable.values()) { line.loadTime = currentTime; } } else if (this.expireTime <= 0 && expireTime > 0) { @@ -518,16 +509,14 @@ public class UtilCache implements Serializable { public void setUseSoftReference(boolean useSoftReference) { if (this.useSoftReference != useSoftReference) { this.useSoftReference = useSoftReference; - Iterator keys = cacheLineTable.keySet().iterator(); - while (keys.hasNext()) { - Object key = keys.next(); - CacheLine line = cacheLineTable.get(key); + for (K key: cacheLineTable.keySet()) { + CacheLine line = cacheLineTable.get(key); if (useSoftReference) { if (line instanceof SoftRefCacheLine) continue; - cacheLineTable.put(key, new SoftRefCacheLine(line.getValue(), line.loadTime, line.expireTime)); + cacheLineTable.put(key, new SoftRefCacheLine(line.getValue(), line.loadTime, line.expireTime)); } else { if (line instanceof HardRefCacheLine) continue; - cacheLineTable.put(key, new HardRefCacheLine(line.getValue(), line.loadTime, line.expireTime)); + cacheLineTable.put(key, new HardRefCacheLine(line.getValue(), line.loadTime, line.expireTime)); } } } @@ -555,7 +544,7 @@ public class UtilCache implements Serializable { * @return True is the cache contains an element corresponding to the specified key, otherwise false */ public boolean containsKey(Object key) { - CacheLine line = getInternal(key, false); + CacheLine line = getInternal(key, false); if (line != null) { return true; } else { @@ -568,11 +557,11 @@ public class UtilCache implements Serializable { * and calling a remove while iterating through the set will not cause a concurrent modification exception. * This behavior is necessary for now for the persisted cache feature. */ - public Set getCacheLineKeys() { + public Set getCacheLineKeys() { return cacheLineTable.keySet(); } - public Collection getCacheLineValues() { + public Collection> getCacheLineValues() { return cacheLineTable.values(); } @@ -585,16 +574,14 @@ public class UtilCache implements Serializable { * @return True is the element corresponding to the specified key has expired, otherwise false */ public boolean hasExpired(Object key) { - CacheLine line = getInternalNoCheck(key); + CacheLine line = getInternalNoCheck(key); if (line == null) return false; return line.hasExpired(); } /** Clears all expired cache entries; also clear any cache entries where the SoftReference in the CacheLine object has been cleared by the gc */ public void clearExpired() { - Iterator keys = cacheLineTable.keySet().iterator(); - while (keys.hasNext()) { - Object key = keys.next(); + for (K key: cacheLineTable.keySet()) { if (hasExpired(key)) { removeInternal(key, false); } @@ -602,47 +589,41 @@ public class UtilCache implements Serializable { } /** Send a key addition event to all registered listeners */ - protected void noteAddition(Object key, Object newValue) { + protected void noteAddition(K key, V newValue) { synchronized (listeners) { - Iterator it = listeners.iterator(); - while (it.hasNext()) { - CacheListener listener = (CacheListener) it.next(); + for (CacheListener listener: listeners) { listener.noteKeyAddition(this, key, newValue); } } } /** Send a key removal event to all registered listeners */ - protected void noteRemoval(Object key, Object oldValue) { + protected void noteRemoval(K key, V oldValue) { synchronized (listeners) { - Iterator it = listeners.iterator(); - while (it.hasNext()) { - CacheListener listener = (CacheListener) it.next(); + for (CacheListener listener: listeners) { listener.noteKeyRemoval(this, key, oldValue); } } } /** Send a key update event to all registered listeners */ - protected void noteUpdate(Object key, Object newValue, Object oldValue) { + protected void noteUpdate(K key, V newValue, V oldValue) { synchronized (listeners) { - Iterator it = listeners.iterator(); - while (it.hasNext()) { - CacheListener listener = (CacheListener) it.next(); + for (CacheListener listener: listeners) { listener.noteKeyUpdate(this, key, newValue, oldValue); } } } /** Adds an event listener for key removals */ - public void addListener(CacheListener listener) { + public void addListener(CacheListener listener) { synchronized (listeners) { listeners.add(listener); } } /** Removes an event listener for key removals */ - public void removeListener(CacheListener listener) { + public void removeListener(CacheListener listener) { synchronized (listeners) { listeners.remove(listener); } @@ -650,17 +631,15 @@ public class UtilCache implements Serializable { /** Clears all expired cache entries from all caches */ public static void clearExpiredFromAllCaches() { - Iterator entries = utilCacheTable.entrySet().iterator(); - while (entries.hasNext()) { - Map.Entry entry = (Map.Entry) entries.next(); - UtilCache utilCache = (UtilCache) entry.getValue(); + for (Map.Entry> entry: utilCacheTable.entrySet()) { + UtilCache utilCache = entry.getValue(); utilCache.clearExpired(); } } /** Checks for a non-expired key in a specific cache */ public static boolean validKey(String cacheName, Object key) { - UtilCache cache = findCache(cacheName); + UtilCache cache = findCache(cacheName); if (cache != null) { if (cache.containsKey(key)) return true; @@ -670,12 +649,10 @@ public class UtilCache implements Serializable { public static void clearCachesThatStartWith(String startsWith) { synchronized (utilCacheTable) { - Iterator it = utilCacheTable.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = (Map.Entry) it.next(); - String name = (String) entry.getKey(); + for (Map.Entry> entry: utilCacheTable.entrySet()) { + String name = entry.getKey(); if (name.startsWith(startsWith)) { - UtilCache cache = (UtilCache) entry.getValue(); + UtilCache cache = entry.getValue(); cache.clear(); } } @@ -683,14 +660,14 @@ public class UtilCache implements Serializable { } public static void clearCache(String cacheName) { - UtilCache cache = findCache(cacheName); + UtilCache cache = findCache(cacheName); if (cache == null) return; cache.clear(); } - public static UtilCache findCache(String cacheName) { + public static UtilCache findCache(String cacheName) { synchronized (UtilCache.utilCacheTable) { - return (UtilCache) UtilCache.utilCacheTable.get(cacheName); + return (UtilCache) UtilCache.utilCacheTable.get(cacheName); } } }