view 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 source

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);
	}

}