68
|
1 package fschmidt.util.java;
|
|
2
|
|
3 import java.util.Map;
|
|
4
|
|
5
|
|
6 public final class SimpleCache<A,V> implements Computable<A,V> {
|
|
7 private final Map<A,V> map;
|
|
8 private final Computable<A,V> comp;
|
|
9
|
|
10 public SimpleCache(Map<A,V> map,Computable<A,V> comp) {
|
|
11 this.map = map;
|
|
12 this.comp = comp;
|
|
13 }
|
|
14
|
|
15 public synchronized V get(A arg) throws ComputationException {
|
|
16 V val = map.get(arg);
|
|
17 if( val == null ) {
|
|
18 try {
|
|
19 val = comp.get(arg);
|
|
20 } catch(RuntimeException e) {
|
|
21 throw e;
|
|
22 } catch(Exception e) {
|
|
23 throw new ComputationException(e);
|
|
24 }
|
|
25 map.put(arg,val);
|
|
26 }
|
|
27 return val;
|
|
28 }
|
|
29
|
|
30 public synchronized void remove(A arg) {
|
|
31 map.remove(arg);
|
|
32 }
|
|
33
|
|
34 }
|