Skip to content

Instantly share code, notes, and snippets.

@oxbowlakes
Last active December 21, 2015 22:42
Show Gist options
  • Save oxbowlakes/6376617 to your computer and use it in GitHub Desktop.
Save oxbowlakes/6376617 to your computer and use it in GitHub Desktop.
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