Skip to content

Commit

Permalink
[Java] Add close methods to CloseHelper which provide an ErrorHandler…
Browse files Browse the repository at this point in the history
… to delegate to.
  • Loading branch information
mjpt777 committed Feb 3, 2020
1 parent ec94f1a commit fce823f
Showing 1 changed file with 86 additions and 5 deletions.
91 changes: 86 additions & 5 deletions agrona/src/main/java/org/agrona/CloseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import java.util.List;

/**
* Utility functions to help with using {@link java.lang.AutoCloseable} resources.
* Utility functions to help with using {@link java.lang.AutoCloseable} resources. If a null exception is passed
* then it is ignored.
*/
public final class CloseHelper
{
Expand Down Expand Up @@ -116,14 +117,14 @@ public static void close(final AutoCloseable closeable)
closeable.close();
}
}
catch (final Exception e)
catch (final Exception ex)
{
LangUtil.rethrowUnchecked(e);
LangUtil.rethrowUnchecked(ex);
}
}

/**
* Close all closeables in closeables. If any of them throw then throw that exception.
* Close all provided closeables. If any of them throw then throw that exception.
* If multiple closeables throw an exception when being closed, then throw an exception that contains
* all of them as suppressed exceptions.
*
Expand Down Expand Up @@ -166,7 +167,7 @@ public static void closeAll(final List<? extends AutoCloseable> closeables)
}

/**
* Close all closeables in closeables. If any of them throw then throw that exception.
* Close all provided closeables. If any of them throw then throw that exception.
* If multiple closeables throw an exception when being closed, then throw an exception that contains
* all of them as suppressed exceptions.
*
Expand Down Expand Up @@ -206,4 +207,84 @@ public static void closeAll(final AutoCloseable... closeables)
LangUtil.rethrowUnchecked(exception);
}
}

/**
* Close a {@link java.lang.AutoCloseable} delegating exceptions to the {@link ErrorHandler}.
*
* @param errorHandler to delegate exceptions to.
* @param closeable to be closed.
*/
public static void close(final ErrorHandler errorHandler, final AutoCloseable closeable)
{
try
{
if (null != closeable)
{
closeable.close();
}
}
catch (final Exception ex)
{
errorHandler.onError(ex);
}
}

/**
* Close all closeables and delegate exceptions to the {@link ErrorHandler}.
*
* @param errorHandler to delegate exceptions to.
* @param closeables to be closed.
*/
public static void closeAll(final ErrorHandler errorHandler, final List<? extends AutoCloseable> closeables)
{
if (closeables == null)
{
return;
}

for (int i = 0, size = closeables.size(); i < size; i++)
{
final AutoCloseable closeable = closeables.get(i);
if (closeable != null)
{
try
{
closeable.close();
}
catch (final Exception ex)
{
errorHandler.onError(ex);
}
}
}
}

/**
* Close all closeables and delegate exceptions to the {@link ErrorHandler}.
*
* @param errorHandler to delegate exceptions to.
* @param closeables to be closed.
*/
public static void closeAll(final ErrorHandler errorHandler, final AutoCloseable... closeables)
{
if (closeables == null)
{
return;
}

for (final AutoCloseable closeable : closeables)
{
if (closeable != null)
{
try
{
closeable.close();
}
catch (final Exception ex)
{
errorHandler.onError(ex);
}
}
}
}
}

0 comments on commit fce823f

Please sign in to comment.