Last active
December 15, 2021 09:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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