Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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.
11 * In contrast to {@link Cache}, this is correctly synchronized.
13 * @author Jaroslav Tulach <jtulach@netbeans.org>
15 // BEGIN: monitor.pitfalls.CacheOK
16 public abstract class CacheOK<From,To> {
17 private final Object LOCK = new Object();
19 private Map<From,To> cache;
21 protected abstract To createItem(From f);
23 public final To get(From f) {
24 To t = inspectValue(f);
28 To newT = createItem(f);
29 return registerValue(f, newT);
33 private To inspectValue(From f) {
36 cache = new HashMap<From, To>();
42 private To registerValue(From f, To newT) {
54 // END: monitor.pitfalls.CacheOK