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. * <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 * 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 * limited by available memory. This cache mechanism is useful in ensuring that
* only a single object instance for a given key exists. * only a single object instance for a given key exists.
* <p> * <p>
* The weak cache is keyed, while the hard cache simply maintains the presence of * 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) { public ObjectCache(int hardCacheSize) {
this.hardCacheSize = hardCacheSize; this.hardCacheSize = hardCacheSize;
hashTable = new HashMap<>(); hashTable = new HashMap<>();
refQueue = new ReferenceQueue<Object>(); refQueue = new ReferenceQueue<>();
hardCache = new LinkedList<Object>(); hardCache = new LinkedList<>();
} }
/** /**
@ -100,7 +100,7 @@ public class ObjectCache {
*/ */
public synchronized void put(long key, Object obj) { public synchronized void put(long key, Object obj) {
processQueue(); processQueue();
KeyedSoftReference<?> ref = new KeyedSoftReference<Object>(key, obj, refQueue); KeyedSoftReference<?> ref = new KeyedSoftReference<>(key, obj, refQueue);
hashTable.put(key, ref); hashTable.put(key, ref);
addToHardCache(obj); addToHardCache(obj);
} }
@ -143,7 +143,7 @@ public class ObjectCache {
/** /**
* Provides a weak wrapper for a keyed-object * 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; private long key;
/** /**

View file

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