Skip to content

Instantly share code, notes, and snippets.

@Maxopoly
Forked from Cryptite/CachedObject.java
Last active June 3, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maxopoly/f77fc7158b07a5d9cc0d4561a16047b8 to your computer and use it in GitHub Desktop.
Save Maxopoly/f77fc7158b07a5d9cc0d4561a16047b8 to your computer and use it in GitHub Desktop.
Cached Object
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