mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-12-03 22:22:16 -05:00
Add AutoCloseableUtil.java for java8+
This commit is contained in:
parent
c143004fc2
commit
83fdc67e4a
@ -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
1
pom.xml
@ -246,6 +246,7 @@
|
|||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/module-info.java</exclude>
|
<exclude>**/module-info.java</exclude>
|
||||||
<exclude>**/PrestoPersonDAO.java</exclude>
|
<exclude>**/PrestoPersonDAO.java</exclude>
|
||||||
|
<exclude>**/AutoCloseableUtil.java</exclude>
|
||||||
<exclude>**/com/moparisthebest/jdbc/cache/*</exclude>
|
<exclude>**/com/moparisthebest/jdbc/cache/*</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
<compilerArgs>
|
<compilerArgs>
|
||||||
|
Loading…
Reference in New Issue
Block a user