Skip to content

Instantly share code, notes, and snippets.

@AngryCarrot789
Last active December 15, 2021 09:26
Show Gist options
  • Save AngryCarrot789/10b64014ef40ef7baec7ebd012e40a62 to your computer and use it in GitHub Desktop.
Save AngryCarrot789/10b64014ef40ef7baec7ebd012e40a62 to your computer and use it in GitHub Desktop.
Used for caching a hashmap's last accessed value, by caching the last accessed key and checking for equality, then returning that last accessed value if it matches
package reghzy.api.utils;
public abstract class KVObjectCache<K, V> {
private K lastAccessedKey;
private V lastAccessedValue;
public V get(K key) {
if (key == null) {
this.lastAccessedKey = null;
this.lastAccessedValue = getValue(null);
}
else if (this.lastAccessedKey == null) {
this.lastAccessedKey = key;
this.lastAccessedValue = getValue(key);
}
else if (this.lastAccessedKey == key) {
if (this.lastAccessedValue == null) {
this.lastAccessedValue = getValue(key);
}
}
else if (this.lastAccessedKey.equals(key)) {
if (this.lastAccessedValue == null) {
this.lastAccessedValue = getValue(key);
}
this.lastAccessedKey = key;
}
else {
this.lastAccessedKey = key;
this.lastAccessedValue = getValue(key);
}
return this.lastAccessedValue;
}
public abstract V getValue(K key);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment