Module de.carne
Package de.carne.util

Class AutoCloseables


  • public final class AutoCloseables
    extends java.lang.Object
    Utility class providing AutoCloseable related functions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(@Nullable java.lang.Object object)
      Closes a potential AutoCloseable.
      static void closeAll​(java.lang.AutoCloseable... closeables)
      Closes multiple AutoCloseables in a safe manner.
      static void closeAll​(java.lang.Iterable<java.lang.AutoCloseable> closeables)
      Closes multiple AutoCloseables in a safe manner.
      static void safeClose​(@Nullable java.lang.Object object)
      Close potential AutoCloseable.
      static void safeClose​(java.lang.Throwable exception, @Nullable java.lang.Object object)
      Close potential AutoCloseable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • close

        public static void close​(@Nullable java.lang.Object object)
                          throws java.lang.Exception
        Closes a potential AutoCloseable.

        An Exception caused by AutoCloseable.close() is forwarded to the caller.

        Parameters:
        object - the object to check and close.
        Throws:
        java.lang.Exception - if AutoCloseable.close() fails.
      • closeAll

        public static void closeAll​(java.lang.AutoCloseable... closeables)
                             throws java.lang.Exception
        Closes multiple AutoCloseables in a safe manner.

        This function ensures that for every submitted non-null AutoCloseable instance the corresponding AutoCloseable.close() method is called. The first encountered Exception will be forwarded to the caller. Any additional Exception will be added to the initial one as a suppressed exception.

        Parameters:
        closeables - the AutoCloseables to close (may contain null values).
        Throws:
        java.lang.Exception - if one or more AutoCloseable.close() calls fail.
      • closeAll

        public static void closeAll​(java.lang.Iterable<java.lang.AutoCloseable> closeables)
                             throws java.lang.Exception
        Closes multiple AutoCloseables in a safe manner.

        This function ensures that for every submitted AutoCloseable instance the corresponding AutoCloseable.close() method is called. The first encountered Exception will be forwarded to the caller. Any additional Exception will be added to the initial one as a suppressed exception.

        Parameters:
        closeables - the AutoCloseables to close.
        Throws:
        java.lang.Exception - if one or more AutoCloseable.close() calls fail.
      • safeClose

        public static void safeClose​(@Nullable java.lang.Object object)
        Close potential AutoCloseable.

        An Exception caused by AutoCloseable.close() is ignored.

        Parameters:
        object - the object to check and close.
      • safeClose

        public static void safeClose​(java.lang.Throwable exception,
                                     @Nullable java.lang.Object object)
        Close potential AutoCloseable.

        An Exception caused by AutoCloseable.close() is suppressed and added to the given outer exception.

        Parameters:
        exception - the currently handled outer exception.
        object - the object to check and close.