Mercurial Hosting > nabble
comparison src/fschmidt/util/java/SimpleCache.java @ 68:00520880ad02
add fschmidt source
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 05 Oct 2025 17:24:15 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 67:9d0fefce6985 | 68:00520880ad02 |
|---|---|
| 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 } |
