Last active
December 21, 2015 22:42
-
-
Save oxbowlakes/6376617 to your computer and use it in GitHub Desktop.
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.math.BigInteger | |
/** | |
* Clients can use this class instead, which means they only need fill in the | |
* V compute(K) method | |
*/ | |
abstract class ConcCache2<K, V> { | |
private final ConcCache<K, V> cache = new ConcCache<>(); | |
/** The computation */ | |
protected abstract V compute(K k); | |
public final V get(final K k) { | |
return cache.get(new Computation<K, V>(){ | |
public K key() { return k; } | |
public V compute() { return compute(key()); } | |
}) | |
} | |
} | |
/** | |
* Client cache implementation is now a bit simpler; they only need fill in a single method | |
*/ | |
class ProbablePrimeCache { | |
private final int ACCURACY = 8; | |
private static ConcCache2<BigInteger, Boolean> cache = new ConcCache2<BigInteger, Boolean>() { | |
protected Boolean compute(BigInteger i) { return i.isProbablePrime(ACCURACY); } | |
}; | |
public static isProbablePrime(BigInteger i) { return cache.get(i); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment