1 package org.apidesign.javamonitorflaws;
3 import java.util.HashMap;
6 /** Classical caching support class that makes sure there is
7 * always one "To" value for each "From" one returned from the {@link #get}
8 * method. However it does not prevent multiple threads to call
9 * {@link #createItem} multiple times for the same "From" value.
13 * @author Jaroslav Tulach <jtulach@netbeans.org>
15 public abstract class Cache<From,To> {
16 private Map<From,To> cache;
18 protected abstract To createItem(From f);
20 public final To get(From f) {
21 To t = inspectValue(f);
25 To newT = createItem(f);
26 return registerValue(f, newT);
30 private synchronized To inspectValue(From f) {
32 cache = new HashMap<From, To>();
37 private synchronized To registerValue(From f, To newT) {