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,39 +19,25 @@
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;
for (Releasable releasable : releasables) {
if (releasable != null) {
try { try {
releasable.close(); // this does the right thing with respect to add suppressed and not wrapping errors etc.
IOUtils.close(releasables);
} catch (Throwable t) { } catch (Throwable t) {
if (th == null) { if (ignoreException == false) {
th = t; IOUtils.reThrowUnchecked(t);
} }
} }
} }
}
if (th != null && !ignoreException) {
rethrow(th);
}
}
/** Release the provided {@link Releasable}s. */ /** Release the provided {@link Releasable}s. */
public static void close(Iterable<? extends Releasable> releasables) { public static void close(Iterable<? extends Releasable> releasables) {
@ -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);
}
};
} }
} }