SimpleCache.java [src/csip/utils] Revision: beaf35d680e39fda49d78cf7e170b55dc5710c27 Date: Tue Apr 25 16:47:08 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
* @param <K>
* @param <V>
*/
public class SimpleCache<K, V> extends ConcurrentHashMap<K, V> {
private static final long serialVersionUID = 1L;
/**
* 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;
}
}