All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.rdbms.ConcurrentFixedCache Maven / Gradle / Ivy

package org.datanucleus.store.rdbms;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * When we know that there is a finite number of keys, so we get just "cache" everything.
 * 

* {@link #factory} may be called multiple times in case multiple threads try to init the same * key at once. */ public final class ConcurrentFixedCache { private final Function factory; private volatile Map immutable = Collections.emptyMap(); public ConcurrentFixedCache(Function factory) { this.factory = factory; } public V get(K key) { V value = immutable.get(key); if (value == null) { Map copy = new HashMap<>(immutable); value = factory.apply(key); copy.put(key, value); immutable = copy; } return value; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy