Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active June 3, 2018 21:48
Show Gist options
  • Save Cryptite/b3861001698d10f2c616b6c66e15382c to your computer and use it in GitHub Desktop.
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.
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