[main] Expand generics package to include most common functions
This commit is contained in:
parent
4f3ee6df65
commit
472258c62b
|
@ -0,0 +1,28 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
|
||||
void accept(T var1, U var2) throws TEx;
|
||||
|
||||
default ThrowingBiConsumer<T, U, TEx> andThen(ThrowingBiConsumer<? super T, ? super U, ? extends TEx> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (l, r) -> {
|
||||
this.accept(l, r);
|
||||
after.accept(l, r);
|
||||
};
|
||||
}
|
||||
|
||||
default BiConsumer<T, U> addHandler(Consumer<Throwable> handler) {
|
||||
return (l, r) -> {
|
||||
try {
|
||||
this.accept(l, r);
|
||||
} catch (Throwable e) {
|
||||
handler.accept(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
|
||||
R apply(T var1, U var2) throws TEx;
|
||||
|
||||
default <V> ThrowingBiFunction<T, U, V, TEx> andThen(ThrowingFunction<? super R, ? extends V, TEx> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (t, u) -> after.apply(this.apply(t, u));
|
||||
}
|
||||
|
||||
default BiFunction<T, U, R> addHandler(Function<Throwable, R> handler) {
|
||||
return (t, u) -> {
|
||||
try {
|
||||
return this.apply(t, u);
|
||||
} catch (Throwable e) {
|
||||
return handler.apply(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingConsumer<T, TEx extends Throwable> {
|
||||
void accept(T var1) throws TEx;
|
||||
|
||||
default ThrowingConsumer<T, TEx> andThen(ThrowingConsumer<? super T, ? extends TEx> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (t) -> {
|
||||
this.accept(t);
|
||||
after.accept(t);
|
||||
};
|
||||
}
|
||||
|
||||
default Consumer<T> addHandler(Consumer<Throwable> handler) {
|
||||
return (t) -> {
|
||||
try {
|
||||
this.accept(t);
|
||||
} catch (Throwable e) {
|
||||
handler.accept(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingFunction<T, R, TEx extends Throwable> {
|
||||
R apply(T var1) throws TEx;
|
||||
|
||||
default <V> ThrowingFunction<V, R, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
|
||||
Objects.requireNonNull(before);
|
||||
return (v) -> this.apply(before.apply(v));
|
||||
}
|
||||
|
||||
default <V> ThrowingFunction<T, V, TEx> andThen(ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (t) -> after.apply(this.apply(t));
|
||||
}
|
||||
|
||||
default Function<T, R> addHandler(Function<Throwable, R> handler) {
|
||||
return (t) -> {
|
||||
try {
|
||||
return this.apply(t);
|
||||
} catch (Throwable e) {
|
||||
return handler.apply(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,5 +1,18 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingRunnable<TEx extends Throwable> {
|
||||
void run() throws TEx;
|
||||
|
||||
default Runnable addHandler(Consumer<Throwable> handler) {
|
||||
return () -> {
|
||||
try {
|
||||
this.run();
|
||||
} catch (Throwable e) {
|
||||
handler.accept(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,19 @@
|
|||
package io.gitlab.jfronny.libjf.generic;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingSupplier<T, TEx extends Throwable> {
|
||||
T get() throws TEx;
|
||||
|
||||
default Supplier<T> addHandler(Function<Throwable, T> handler) {
|
||||
return () -> {
|
||||
try {
|
||||
return this.get();
|
||||
} catch (Throwable e) {
|
||||
return handler.apply(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,18 +5,10 @@ import java.util.function.Function;
|
|||
|
||||
public class Try {
|
||||
public static void orElse(ThrowingRunnable<?> tr, Consumer<Throwable> alternative) {
|
||||
try {
|
||||
tr.run();
|
||||
} catch (Throwable e) {
|
||||
alternative.accept(e);
|
||||
}
|
||||
tr.addHandler(alternative).run();
|
||||
}
|
||||
|
||||
public static <T> T orElse(ThrowingSupplier<T, ?> tr, Function<Throwable, T> alternative) {
|
||||
try {
|
||||
return tr.get();
|
||||
} catch (Throwable e) {
|
||||
return alternative.apply(e);
|
||||
}
|
||||
return tr.addHandler(alternative).get();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue