SimpleCache.java [src/csip/utils] Revision: d5970d7c0e30454d24a80dc6a55dd59e3bcbc398 Date: Sat Apr 15 07:53:13 MDT 2017
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2017, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip.utils;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* Simple, non-evicting cache. Good for Resource caching.
*
* @author od
*/
public class SimpleCache<K, V> extends ConcurrentHashMap<K, V> {
/**
* Get the cache entry. If not present, the Function f will be invoked to
* create one.
*
* @param k the key
* @param f the Function to create the value
* @return the cache value
*/
public synchronized V get(K k, Function<K, V> f) {
V v = get(k);
if (v == null && f != null) {
put(k, v = f.apply(k));
}
return v;
}
}