SimpleCache.java [src/csip/utils] Revision: default Date:
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2022, 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> generic key
* @param <V> generic value
*/
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;
}
}