feat(commons): Add Throwable*.of to create instances from common types

This commit is contained in:
Johannes Frohnmeyer 2024-05-04 21:58:57 +02:00
parent c50bdc95bd
commit 0e15fe3abd
Signed by: Johannes
GPG Key ID: E76429612C2929F4
8 changed files with 40 additions and 0 deletions

View File

@ -8,6 +8,11 @@ import java.util.function.*;
@FunctionalInterface
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
static <T, U, TEx extends Throwable> @NotNull ThrowingBiConsumer<T, U, TEx> of(@NotNull BiConsumer<T, U> consumer) {
Objects.requireNonNull(consumer);
return consumer::accept;
}
void accept(T var1, U var2) throws TEx;
@Contract(value = "-> new", pure = true)

View File

@ -9,6 +9,11 @@ import java.util.function.Function;
@FunctionalInterface
public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
static <T, U, R, TEx extends Throwable> @NotNull ThrowingBiFunction<T, U, R, TEx> of(@NotNull BiFunction<T, U, R> function) {
Objects.requireNonNull(function);
return function::apply;
}
R apply(T var1, U var2) throws TEx;
@Contract(value = "-> new", pure = true)

View File

@ -7,6 +7,11 @@ import java.util.function.*;
@FunctionalInterface
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
static <TEx extends Throwable> @NotNull ThrowingBooleanSupplier<TEx> of(@NotNull BooleanSupplier supplier) {
Objects.requireNonNull(supplier);
return supplier::getAsBoolean;
}
boolean get() throws TEx;
@Contract(value = "_ -> new", pure = true)

View File

@ -8,6 +8,11 @@ import java.util.function.Function;
@FunctionalInterface
public interface ThrowingConsumer<T, TEx extends Throwable> {
static <T> @NotNull ThrowingConsumer<T, RuntimeException> of(@NotNull Consumer<T> consumer) {
Objects.requireNonNull(consumer);
return consumer::accept;
}
void accept(T var1) throws TEx;
@Contract(value = "_ -> new", pure = true)

View File

@ -7,6 +7,11 @@ import java.util.function.Function;
@FunctionalInterface
public interface ThrowingFunction<T, R, TEx extends Throwable> {
static <T, R> @NotNull ThrowingFunction<T, R, RuntimeException> of(@NotNull Function<T, R> function) {
Objects.requireNonNull(function);
return function::apply;
}
R apply(T var1) throws TEx;
@Contract(value = "_ -> new", pure = true)

View File

@ -8,6 +8,11 @@ import java.util.function.Predicate;
@FunctionalInterface
public interface ThrowingPredicate<T, TEx extends Throwable> {
static <T> @NotNull ThrowingPredicate<T, RuntimeException> of(@NotNull Predicate<T> predicate) {
Objects.requireNonNull(predicate);
return predicate::test;
}
boolean test(T var1) throws TEx;
@Contract(value = "_ -> new", pure = true)

View File

@ -8,6 +8,11 @@ import java.util.function.Function;
@FunctionalInterface
public interface ThrowingRunnable<TEx extends Throwable> {
static @NotNull ThrowingRunnable<RuntimeException> of(@NotNull Runnable runnable) {
Objects.requireNonNull(runnable);
return runnable::run;
}
void run() throws TEx;
@Contract(value = "_ -> new", pure = true)

View File

@ -8,6 +8,11 @@ import java.util.function.Supplier;
@FunctionalInterface
public interface ThrowingSupplier<T, TEx extends Throwable> {
static <T> @NotNull ThrowingSupplier<T, RuntimeException> of(@NotNull Supplier<T> supplier) {
Objects.requireNonNull(supplier);
return supplier::get;
}
T get() throws TEx;
@Contract(value = "_ -> new", pure = true)