Last active
August 29, 2015 14:01
-
-
Save kobachi/1ad5864f942c8df011bb to your computer and use it in GitHub Desktop.
CachedSupplier.java
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
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(); | |
} | |
}; | |
} | |
} |
Author
kobachi
commented
May 8, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment