Class ReferenceCountingMap<K,​V extends Closeable>

  • Type Parameters:
    K - Key type for the map
    V - Value type for the map - it should be a Closeable

    @ThreadSafe
    public final class ReferenceCountingMap<K,​V extends Closeable>
    extends java.lang.Object
    A map for managing reference-counted shared resources - typically providers.

    A key (of generic type K) identifies a provider (of generic type V). The map also depends on a factory ReferenceCountingMap.ValueFactory to create a new instance of V as needed. Values are ref-counted and closed by calling Closeable.close() when ref-count reaches zero.

    • Method Detail

      • get

        @CheckReturnValue
        public V get​(K key)
        Gets an existing instance of a provider. If it doesn't exist, creates a new one using the provided <K, V>
      • release

        public V release​(K key,
                         V value)
        Releases an instance of the given value.

        The instance must have been obtained from get(Object). Otherwise will throw IllegalArgumentException.

        Caller must not release a reference more than once. It's advised that you clear the reference to the instance with the null returned by this method.

        Parameters:
        key - for the instance to be released
        value - the instance to be released
        Returns:
        a null which the caller can use to clear the reference to that instance.
      • getInternal

        private V getInternal​(K key)
      • releaseInternal

        private V releaseInternal​(K key,
                                  V value)