package net.jcip.examples;
import java.math.BigInteger;
import java.util.*;
import net.jcip.annotations.*;
/**
* Memoizer1
*
* Initial cache attempt using HashMap and synchronization
*
* @author Brian Goetz and Tim Peierls
*/
public class Memoizer1 implements Computable {
@GuardedBy("this") private final Map cache = new HashMap();
private final Computable c;
public Memoizer1(Computable c) {
this.c = c;
}
public synchronized V compute(A arg) throws InterruptedException {
V result = cache.get(arg);
if (result == null) {
result = c.compute(arg);
cache.put(arg, result);
}
return result;
}
}
interface Computable {
V compute(A arg) throws InterruptedException;
}
class ExpensiveFunction
implements Computable {
public BigInteger compute(String arg) {
// after deep thought...
return new BigInteger(arg);
}
}