Class AbstractAutoCloseableRegistry<R extends AutoCloseable,​C extends R,​T,​E extends Exception>

  • Type Parameters:
    C - Type of the closeable this registers
    T - Type for potential meta data associated with the registering closeables
    All Implemented Interfaces:
    AutoCloseable
    Direct Known Subclasses:
    AutoCloseableRegistry, CloseableRegistry, SafetyNetCloseableRegistry

    @Internal
    public abstract class AbstractAutoCloseableRegistry<R extends AutoCloseable,​C extends R,​T,​E extends Exception>
    extends Object
    implements AutoCloseable
    This is the abstract base class for registries that allow to register instances of Closeable, which are all closed if this registry is closed.

    Registering to an already closed registry will throw an exception and close the provided Closeable

    All methods in this class are thread-safe.

    • Field Detail

      • closeableToRef

        protected final Map<R extends AutoCloseable,​T> closeableToRef
        Map from tracked Closeables to some associated meta data.
    • Constructor Detail

      • AbstractAutoCloseableRegistry

        public AbstractAutoCloseableRegistry​(@Nonnull
                                             Map<R,​T> closeableToRef)
    • Method Detail

      • registerCloseable

        public final void registerCloseable​(C closeable)
                                     throws IOException
        Registers a AutoCloseable with the registry. In case the registry is already closed, this method throws an IllegalStateException and closes the passed AutoCloseable.
        Parameters:
        closeable - Closeable to register.
        Throws:
        IOException - exception when the registry was closed before.
      • unregisterCloseable

        public final boolean unregisterCloseable​(C closeable)
        Removes a Closeable from the registry.
        Parameters:
        closeable - instance to remove from the registry.
        Returns:
        true if the closeable was previously registered and became unregistered through this call.
      • isClosed

        public boolean isClosed()
      • doRegister

        protected abstract void doRegister​(@Nonnull
                                           C closeable,
                                           @Nonnull
                                           Map<R,​T> closeableMap)
        Does the actual registration of the closeable with the registry map. This should not do any long running or potentially blocking operations as is is executed under the registry's lock.
      • doUnRegister

        protected abstract boolean doUnRegister​(@Nonnull
                                                C closeable,
                                                @Nonnull
                                                Map<R,​T> closeableMap)
        Does the actual un-registration of the closeable from the registry map. This should not do any long running or potentially blocking operations as is is executed under the registry's lock.
      • getSynchronizationLock

        protected final Object getSynchronizationLock()
        Returns the lock on which manipulations to members closeableToRef and closeable must be synchronized.
      • removeCloseableInternal

        protected final boolean removeCloseableInternal​(R closeable)
        Removes a mapping from the registry map, respecting locking.
      • getNumberOfRegisteredCloseables

        @VisibleForTesting
        public final int getNumberOfRegisteredCloseables()
      • isCloseableRegistered

        @VisibleForTesting
        public final boolean isCloseableRegistered​(R c)