Last active
June 3, 2018 21:48
-
-
Save Cryptite/b3861001698d10f2c616b6c66e15382c to your computer and use it in GitHub Desktop.
Cached Object - A very simple caching class that will use the supplied update function whenever dirty to refresh its value.
This file contains 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
import java.util.function.Supplier; | |
public class CachedObject<T> { | |
private final Supplier<T> updateFunction; | |
private T cachedValue; | |
private boolean dirty = true; | |
public CachedObject(Supplier<T> updateFunction) { | |
this.updateFunction = updateFunction; | |
} | |
public T get() { | |
if (dirty) { | |
cachedValue = updateFunction.get(); | |
dirty = false; | |
} | |
return cachedValue; | |
} | |
public void setDirty() { | |
dirty = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment