Skip to content

Instantly share code, notes, and snippets.

@kobachi
Last active August 29, 2015 14:01
Show Gist options
  • Save kobachi/1ad5864f942c8df011bb to your computer and use it in GitHub Desktop.
Save kobachi/1ad5864f942c8df011bb to your computer and use it in GitHub Desktop.
CachedSupplier.java
public abstract class CachedSupplier<T> implements Supplier<T>{
private T cache;
private boolean cached;
public CachedSupplier(){
cache = null;
cached = false;
}
@Override
public final T get(){
if(!cached){
synchronized{
cache = refresh();
cacheed = true;
}
}
return cache;
}
protected synchronized T refresh();
public static CachedSupplier<T> of(Supplier<T> supplier){
return new CachedSupplier<T>(){
@Override
public synchronized T refresh(){
return supplier.get();
}
};
}
}
@kobachi
Copy link
Author

kobachi commented May 8, 2014

CachedSupplier<Boolean> checked = new CachedSupplier<Boolean>(){
    @Override
    public Boolean onSupply(){
        return true;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment