Add AutoCloseableUtil.java for java8+

This commit is contained in:
Travis Burtrum 2020-08-31 21:48:55 -04:00
parent c143004fc2
commit 83fdc67e4a
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package com.moparisthebest.jdbc.util;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
public interface AutoCloseableUtil {
static <E extends AutoCloseable, T, R> R apply(final Supplier<E> supplier, final Function<? super E, R> action) {
try (final E stream = supplier.get()) {
return action.apply(stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static <E extends AutoCloseable, T> void accept(final Supplier<E> supplier, final Consumer<? super E> action) {
try (final E stream = supplier.get()) {
action.accept(stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static <E extends Stream<T>, T> void forEach(final Supplier<E> supplier, final Consumer<? super T> action) {
accept(supplier, s -> s.forEach(action));
}
static <E extends AutoCloseable & Iterable<T>, T> void forEachIt(final Supplier<E> supplier, final Consumer<? super T> action) {
accept(supplier, s -> {
for (final T val : s) {
action.accept(val);
}
});
}
static <T> void forEachRemove(final Iterator<T> iterator, final Function<? super T, Boolean> action) {
while (iterator.hasNext()) {
if (action.apply(iterator.next())) {
iterator.remove();
}
}
}
}

View File

@ -246,6 +246,7 @@
<excludes>
<exclude>**/module-info.java</exclude>
<exclude>**/PrestoPersonDAO.java</exclude>
<exclude>**/AutoCloseableUtil.java</exclude>
<exclude>**/com/moparisthebest/jdbc/cache/*</exclude>
</excludes>
<compilerArgs>