Browse Source

Add AutoCloseableUtil.java for java8+

dependabot/maven/junit-junit-4.13.1
Travis Burtrum 2 years ago
parent
commit
83fdc67e4a
  1. 47
      common/src/main/java/com/moparisthebest/jdbc/util/AutoCloseableUtil.java
  2. 1
      pom.xml

47
common/src/main/java/com/moparisthebest/jdbc/util/AutoCloseableUtil.java

@ -0,0 +1,47 @@ @@ -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();
}
}
}
}

1
pom.xml

@ -246,6 +246,7 @@ @@ -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>

Loading…
Cancel
Save