GP-1056 - Updated class to be static

Closes #3141
This commit is contained in:
dragonmacher 2021-06-17 15:44:29 -04:00
parent 82c8ba1a1c
commit 4fa9842f52
2 changed files with 24 additions and 24 deletions

View file

@ -21,8 +21,8 @@ import java.util.*;
/**
* <code>ObjectClass</code> provides a fixed-size long-key-based object cache.
* Both a hard and weak cache are maintained, where the weak cache is only
* limited by available memory. This cache mechanism is useful in ensuring that
* Both a hard and weak cache are maintained, where the weak cache is only
* limited by available memory. This cache mechanism is useful in ensuring that
* only a single object instance for a given key exists.
* <p>
* The weak cache is keyed, while the hard cache simply maintains the presence of
@ -42,8 +42,8 @@ public class ObjectCache {
public ObjectCache(int hardCacheSize) {
this.hardCacheSize = hardCacheSize;
hashTable = new HashMap<>();
refQueue = new ReferenceQueue<Object>();
hardCache = new LinkedList<Object>();
refQueue = new ReferenceQueue<>();
hardCache = new LinkedList<>();
}
/**
@ -100,7 +100,7 @@ public class ObjectCache {
*/
public synchronized void put(long key, Object obj) {
processQueue();
KeyedSoftReference<?> ref = new KeyedSoftReference<Object>(key, obj, refQueue);
KeyedSoftReference<?> ref = new KeyedSoftReference<>(key, obj, refQueue);
hashTable.put(key, ref);
addToHardCache(obj);
}
@ -143,7 +143,7 @@ public class ObjectCache {
/**
* Provides a weak wrapper for a keyed-object
*/
private class KeyedSoftReference<T> extends WeakReference<T> {
private static class KeyedSoftReference<T> extends WeakReference<T> {
private long key;
/**

View file

@ -35,7 +35,7 @@ import ghidra.program.model.address.KeyRange;
*/
public class DBObjectCache<T extends DatabaseObject> {
private Map<Long, KeyedSoftReference> map;
private Map<Long, KeyedSoftReference<T>> map;
private ReferenceQueue<T> refQueue;
private LinkedList<T> hardCache;
private int hardCacheSize;
@ -49,9 +49,9 @@ public class DBObjectCache<T extends DatabaseObject> {
*/
public DBObjectCache(int hardCacheSize) {
this.hardCacheSize = hardCacheSize;
map = new HashMap<Long, KeyedSoftReference>();
refQueue = new ReferenceQueue<T>();
hardCache = new LinkedList<T>();
map = new HashMap<>();
refQueue = new ReferenceQueue<>();
hardCache = new LinkedList<>();
}
/**
@ -60,7 +60,7 @@ public class DBObjectCache<T extends DatabaseObject> {
* @return the cached object or null if the object with that key is not currently cached.
*/
public synchronized T get(long key) {
KeyedSoftReference ref = map.get(key);
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj == null) {
@ -89,7 +89,7 @@ public class DBObjectCache<T extends DatabaseObject> {
*/
public synchronized T get(DBRecord objectRecord) {
long key = objectRecord.getKey();
KeyedSoftReference ref = map.get(key);
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj == null) {
@ -133,7 +133,7 @@ public class DBObjectCache<T extends DatabaseObject> {
processQueue();
long key = data.getKey();
addToHardCache(data);
KeyedSoftReference ref = new KeyedSoftReference(key, data, refQueue);
KeyedSoftReference<T> ref = new KeyedSoftReference<>(key, data, refQueue);
map.put(key, ref);
}
@ -142,9 +142,9 @@ public class DBObjectCache<T extends DatabaseObject> {
* @return an List of all the cached objects.
*/
public synchronized List<T> getCachedObjects() {
ArrayList<T> list = new ArrayList<T>();
ArrayList<T> list = new ArrayList<>();
processQueue();
for (KeyedSoftReference ref : map.values()) {
for (KeyedSoftReference<T> ref : map.values()) {
T obj = ref.get();
if (obj != null) {
list.add(obj);
@ -180,7 +180,7 @@ public class DBObjectCache<T extends DatabaseObject> {
private void deleteSmallKeyRanges(List<KeyRange> keyRanges) {
for (KeyRange range : keyRanges) {
for (long key = range.minKey; key <= range.maxKey; key++) {
KeyedSoftReference ref = map.remove(key);
KeyedSoftReference<T> ref = map.remove(key);
if (ref != null) {
DatabaseObject obj = ref.get();
if (obj != null) {
@ -202,7 +202,7 @@ public class DBObjectCache<T extends DatabaseObject> {
map.values().removeIf(ref -> checkRef(ref, keyRanges));
}
private boolean checkRef(KeyedSoftReference ref, List<KeyRange> keyRanges) {
private boolean checkRef(KeyedSoftReference<T> ref, List<KeyRange> keyRanges) {
long key = ref.getKey();
if (keyRangesContain(keyRanges, key)) {
DatabaseObject obj = ref.get();
@ -252,7 +252,7 @@ public class DBObjectCache<T extends DatabaseObject> {
processQueue();
if (++invalidateCount <= 0) {
invalidateCount = 1;
for (KeyedSoftReference ref : map.values()) {
for (KeyedSoftReference<T> ref : map.values()) {
DatabaseObject obj = ref.get();
if (obj != null) {
obj.setInvalid();
@ -276,7 +276,7 @@ public class DBObjectCache<T extends DatabaseObject> {
*/
public synchronized void delete(long key) {
processQueue();
KeyedSoftReference ref = map.get(key);
KeyedSoftReference<T> ref = map.get(key);
if (ref != null) {
T obj = ref.get();
if (obj != null) {
@ -297,10 +297,10 @@ public class DBObjectCache<T extends DatabaseObject> {
// we know the cast is safe--we put them in there
@SuppressWarnings("unchecked")
private void processQueue() {
KeyedSoftReference ref;
while ((ref = (KeyedSoftReference) refQueue.poll()) != null) {
KeyedSoftReference<T> ref;
while ((ref = (KeyedSoftReference<T>) refQueue.poll()) != null) {
long key = ref.getKey();
KeyedSoftReference oldValue = map.remove(key);
KeyedSoftReference<T> oldValue = map.remove(key);
if (oldValue != null && oldValue != ref) {
// we have put another item in the cache with the same key. Further, we
@ -312,7 +312,7 @@ public class DBObjectCache<T extends DatabaseObject> {
}
}
private class KeyedSoftReference extends WeakReference<T> {
private static class KeyedSoftReference<T> extends WeakReference<T> {
private long key;
KeyedSoftReference(long key, T obj, ReferenceQueue<T> queue) {
@ -328,7 +328,7 @@ public class DBObjectCache<T extends DatabaseObject> {
public synchronized void keyChanged(long oldKey, long newKey) {
processQueue();
KeyedSoftReference ref = map.remove(oldKey);
KeyedSoftReference<T> ref = map.remove(oldKey);
if (ref != null) {
map.put(newKey, ref);
T t = ref.get();