-
-
Save Maxopoly/f77fc7158b07a5d9cc0d4561a16047b8 to your computer and use it in GitHub Desktop.
Cached Object
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
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