/** * Create a {@code ReferenceCountedProxyCache} 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 iface The interface class of values in this cache. * @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 cache. * @param <V> The value type (must implement {@link java.io.Closeable}) of cache. */ public static <K, V extends Closeable> ReferenceCountedProxyCache<K, V> create( final Class<V> iface, final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedProxyCache<>(iface, cacheLoader, false, false); }
final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedProxyCache<>(iface, cacheLoader, true, false);
/** * Create a {@code ReferenceCountedCache} using the supplied function to create cached values * on demand with resource tracking of proxies. * * <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 returned proxy objects will be registered with the {@link ResourceTracker}, and * automatically unregistered when they are closed. This is useful for debugging resource * leaks, but comes at the cost of more actively tracked resources. Prefer * {@link #createWithResourceTracking} for performance-sensitive caches that need tracking. * </p> * * @param iface The interface class of values in this cache. * @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 java.io.Closeable}) of the cache. */ public static <K, V extends Closeable> ReferenceCountedProxyCache<K, V> createWithProxyTracking( final Class<V> iface, final Function<? super K, ? extends V> cacheLoader ) { return new ReferenceCountedProxyCache<>(iface, cacheLoader, false, true); }