/** * Create a {@code ReferenceCountedCache} using the supplied function to create cached values * on demand. * * <p> * The function may be evaluated with the same key multiple times in the case that the key * becomes invalidated due to the reference count falling to 0. A function need not handle the * case of a null key, but it should never return a null value for any key. * </p> * * @param cacheLoader A function to compute cached values on demand. Should never return null. * @return A new {@link ReferenceCountedCache}. * @param <K> The key type of the cache. * @param <V> The value type (must implement {@link Closeable}) of the cache. */ public static <K, V extends Closeable> ReferenceCountedCache<K, V> create( final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedCache<>(cacheLoader, false); }
/** * Create a {@code ReferenceCountedCache} using the supplied function to create cached values * on demand with resource tracking of cached values. * * <p> * The function may be evaluated with the same key multiple times in the case that the key * becomes invalidated due to the reference count falling to 0. A function need not handle the * case of a null key, but it should never return a null value for any key. * </p> * * <p> * The cached values will be registered with the {@link ResourceTracker}, and automatically * unregistered when their reference count falls to 0 and they are removed from the cache. The * values will be registered once upon creation, not every time the value is borrowed. If the * value objects already perform resource tracking, then prefer using {@link #create} to avoid * double registration. * </p> * * @param cacheLoader A function to compute cached values on demand. Should never return null. * @return A new {@link ReferenceCountedCache}. * @param <K> The key type of the cache. * @param <V> The value type (must implement {@link Closeable}) of the cache. */ public static <K, V extends Closeable> ReferenceCountedCache<K, V> createWithResourceTracking( final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedCache<>(cacheLoader, true); }