diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/util/java/SimpleCache.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,34 @@
+package fschmidt.util.java;
+
+import java.util.Map;
+
+
+public final class SimpleCache<A,V> implements Computable<A,V> {
+	private final Map<A,V> map;
+	private final Computable<A,V> comp;
+
+	public SimpleCache(Map<A,V> map,Computable<A,V> comp) {
+		this.map = map;
+		this.comp = comp;
+	}
+
+	public synchronized V get(A arg) throws ComputationException {
+		V val = map.get(arg);
+		if( val == null ) {
+			try {
+				val = comp.get(arg);
+			} catch(RuntimeException e) {
+				throw e;
+			} catch(Exception e) {
+				throw new ComputationException(e);
+			}
+			map.put(arg,val);
+		}
+		return val;
+	}
+
+	public synchronized void remove(A arg) {
+		map.remove(arg);
+	}
+
+}