Cleanup Relesables now that we can delegate to IOUtils

This commit is contained in:
Simon Willnauer 2016-01-29 17:02:40 +01:00
parent 15841081f6
commit 6814f24009
1 changed files with 11 additions and 39 deletions

View File

@ -19,38 +19,24 @@
package org.elasticsearch.common.lease; package org.elasticsearch.common.lease;
import org.apache.lucene.util.IOUtils;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
/** Utility methods to work with {@link Releasable}s. */ /** Utility methods to work with {@link Releasable}s. */
public enum Releasables { public enum Releasables {
; ;
private static void rethrow(Throwable t) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
if (t instanceof Error) {
throw (Error) t;
}
throw new RuntimeException(t);
}
private static void close(Iterable<? extends Releasable> releasables, boolean ignoreException) { private static void close(Iterable<? extends Releasable> releasables, boolean ignoreException) {
Throwable th = null; try {
for (Releasable releasable : releasables) { // this does the right thing with respect to add suppressed and not wrapping errors etc.
if (releasable != null) { IOUtils.close(releasables);
try { } catch (Throwable t) {
releasable.close(); if (ignoreException == false) {
} catch (Throwable t) { IOUtils.reThrowUnchecked(t);
if (th == null) {
th = t;
}
}
} }
} }
if (th != null && !ignoreException) {
rethrow(th);
}
} }
/** Release the provided {@link Releasable}s. */ /** Release the provided {@link Releasable}s. */
@ -99,25 +85,11 @@ public enum Releasables {
* </pre> * </pre>
*/ */
public static Releasable wrap(final Iterable<Releasable> releasables) { public static Releasable wrap(final Iterable<Releasable> releasables) {
return new Releasable() { return () -> close(releasables);
@Override
public void close() {
Releasables.close(releasables);
}
};
} }
/** @see #wrap(Iterable) */ /** @see #wrap(Iterable) */
public static Releasable wrap(final Releasable... releasables) { public static Releasable wrap(final Releasable... releasables) {
return new Releasable() { return () -> close(releasables);
@Override
public void close() {
Releasables.close(releasables);
}
};
} }
} }