feat(commons): Extend Assume API to all Throwing* functional interfaces and merge it into them. Use Coerce to obtain one if needed.
This commit is contained in:
parent
bb38fe5868
commit
c50bdc95bd
|
@ -1,7 +1,6 @@
|
|||
package io.gitlab.jfronny.commons.concurrent;
|
||||
|
||||
import io.gitlab.jfronny.commons.throwable.Assume;
|
||||
import io.gitlab.jfronny.commons.throwable.Unchecked;
|
||||
import io.gitlab.jfronny.commons.throwable.*;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
public interface WithScopedValue<T> {
|
||||
|
@ -10,18 +9,23 @@ public interface WithScopedValue<T> {
|
|||
|
||||
default <TEx1 extends Throwable, TEx2 extends Throwable> void withContext(Action<TEx1, TEx2> action) throws TEx1, TEx2 {
|
||||
Unchecked.<TEx1>reintroduce();
|
||||
Unchecked.<TEx2>reintroduce(() -> ScopedValue.runWhere(getAttached(), self(), Assume.isSafe(action::run)));
|
||||
Unchecked.<TEx2>reintroduce(() -> ScopedValue.runWhere(getAttached(), self(), action.assumeSafe()));
|
||||
}
|
||||
default <T, TEx1 extends Throwable, TEx2 extends Throwable> T withContext(Returnable<T, TEx1, TEx2> action) throws TEx1, TEx2 {
|
||||
Unchecked.<TEx1>reintroduce();
|
||||
return Unchecked.<T, TEx2>reintroduce(() -> ScopedValue.getWhere(getAttached(), self(), Assume.isSafe(action::run)));
|
||||
return Unchecked.<T, TEx2>reintroduce(() -> ScopedValue.getWhere(getAttached(), self(), action.assumeSafe()));
|
||||
}
|
||||
|
||||
interface Returnable<T, TEx1 extends Throwable, TEx2 extends Throwable> {
|
||||
interface Returnable<T, TEx1 extends Throwable, TEx2 extends Throwable> extends ThrowingSupplier<T, Throwable> {
|
||||
T run() throws TEx1, TEx2;
|
||||
|
||||
@Override
|
||||
default T get() throws Throwable {
|
||||
return run();
|
||||
}
|
||||
}
|
||||
|
||||
interface Action<TEx1 extends Throwable, TEx2 extends Throwable> {
|
||||
interface Action<TEx1 extends Throwable, TEx2 extends Throwable> extends ThrowingRunnable<Throwable> {
|
||||
void run() throws TEx1, TEx2;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,42 +1,24 @@
|
|||
package io.gitlab.jfronny.commons.throwable;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A utility class to hide checked exceptions from the java compiler.
|
||||
* For immediate execution or to reintroduce exceptions, use {@link Unchecked}.
|
||||
* For conversion to unchecked exceptions, use {@code .assumeSafe()}.
|
||||
* @deprecated Please migrate to the alternatives mentioned as soon as possible.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public class Assume {
|
||||
/**
|
||||
* Hides an exception from the java compiler, effectively making it unchecked.
|
||||
* Wraps the runnable in a runnable not marked to throw the checked exception.
|
||||
*
|
||||
* @param runnable the runnable to wrap
|
||||
* @param <TEx> the exception the runnable might throw
|
||||
* @return a runnable not marked to throw the checked exception
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <TEx extends Throwable> @NotNull Runnable isSafe(@NotNull ThrowingRunnable<TEx> runnable) {
|
||||
var rn = (ThrowingRunnable<RuntimeException>) (ThrowingRunnable) runnable;
|
||||
return rn::run;
|
||||
return runnable.assumeSafe();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides an exception from the java compiler, effectively making it unchecked.
|
||||
* Wraps the supplier in a supplier not marked to throw the checked exception.
|
||||
*
|
||||
* @param supplier the supplier to wrap
|
||||
* @param <T> the return type of the supplier
|
||||
* @param <TEx> the exception the supplier might throw
|
||||
* @return a supplier not marked to throw the checked exception
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <T, TEx extends Throwable> @NotNull Supplier<T> isSafe(@NotNull ThrowingSupplier<T, TEx> supplier) {
|
||||
var rn = (ThrowingSupplier<T, RuntimeException>) (ThrowingSupplier) supplier;
|
||||
return rn::get;
|
||||
return supplier.assumeSafe();
|
||||
}
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
|
|
|
@ -98,7 +98,7 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull BiConsumer<T, U> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::accept;
|
||||
return orThrow(ExceptionWrapper::wrap)::accept;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -113,4 +113,17 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the consumer in a consumer not marked to throw the checked exception.
|
||||
*
|
||||
* @return a consumer that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default BiConsumer<T, U> assumeSafe() {
|
||||
ThrowingBiConsumer<T, U, RuntimeException> consumer = (ThrowingBiConsumer<T, U, java.lang.RuntimeException>) (ThrowingBiConsumer) this;
|
||||
return consumer::accept;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull BiFunction<T, U, R> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::apply;
|
||||
return orThrow(ExceptionWrapper::wrap)::apply;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -108,4 +108,17 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the function in a function not marked to throw the checked exception.
|
||||
*
|
||||
* @return a function that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default BiFunction<T, U, R> assumeSafe() {
|
||||
ThrowingBiFunction<T, U, R, RuntimeException> function = (ThrowingBiFunction<T, U, R, RuntimeException>) (ThrowingBiFunction) this;
|
||||
return function::apply;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ public interface ThrowingBooleanSupplier<TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull BooleanSupplier orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::get;
|
||||
return orThrow(ExceptionWrapper::wrap)::get;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -109,4 +109,17 @@ public interface ThrowingBooleanSupplier<TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the supplier in a supplier not marked to throw the checked exception.
|
||||
*
|
||||
* @return a supplier that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default BooleanSupplier assumeSafe() {
|
||||
ThrowingBooleanSupplier<RuntimeException> supplier = (ThrowingBooleanSupplier<RuntimeException>) (ThrowingBooleanSupplier) this;
|
||||
return supplier::get;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull Consumer<T> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::accept;
|
||||
return orThrow(ExceptionWrapper::wrap)::accept;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -101,4 +101,17 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the consumer in a consumer not marked to throw the checked exception.
|
||||
*
|
||||
* @return a consumer that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default Consumer<T> assumeSafe() {
|
||||
ThrowingConsumer<T, RuntimeException> consumer = (ThrowingConsumer<T, java.lang.RuntimeException>) (ThrowingConsumer) this;
|
||||
return consumer::accept;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull Function<T, R> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::apply;
|
||||
return orThrow(ExceptionWrapper::wrap)::apply;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -84,4 +84,17 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the function in a function not marked to throw the checked exception.
|
||||
*
|
||||
* @return a function that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default Function<T, R> assumeSafe() {
|
||||
ThrowingFunction<T, R, RuntimeException> function = (ThrowingFunction<T, R, RuntimeException>) (ThrowingFunction) this;
|
||||
return function::apply;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull Predicate<T> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::test;
|
||||
return orThrow(ExceptionWrapper::wrap)::test;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -129,4 +129,17 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the predicate in a predicate not marked to throw the checked exception.
|
||||
*
|
||||
* @return a predicate that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default Predicate<T> assumeSafe() {
|
||||
ThrowingPredicate<T, RuntimeException> predicate = (ThrowingPredicate<T, RuntimeException>) (ThrowingPredicate) this;
|
||||
return predicate::test;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ public interface ThrowingRunnable<TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull Runnable orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::run;
|
||||
return orThrow(ExceptionWrapper::wrap)::run;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -77,4 +77,17 @@ public interface ThrowingRunnable<TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making the runnable unchecked.
|
||||
* Wraps the runnable in a runnable not marked to throw the checked exception.
|
||||
*
|
||||
* @return a runnable not marked to throw the checked exception
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default Runnable assumeSafe() {
|
||||
ThrowingRunnable<RuntimeException> runnable = (ThrowingRunnable<RuntimeException>) (ThrowingRunnable) this;
|
||||
return runnable::run;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
|
|||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default @NotNull Supplier<T> orThrow() {
|
||||
return orThrow(t -> ExceptionWrapper.wrap(t))::get;
|
||||
return orThrow(ExceptionWrapper::wrap)::get;
|
||||
}
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -71,4 +71,17 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides the exception this could throw from the java compiler, effectively making it unchecked.
|
||||
* Wraps the supplier in a supplier not marked to throw the checked exception.
|
||||
*
|
||||
* @return a supplier that does not throw checked exceptions
|
||||
*/
|
||||
@Contract(value = "-> new", pure = true)
|
||||
@ApiStatus.NonExtendable
|
||||
default Supplier<T> assumeSafe() {
|
||||
ThrowingSupplier<T, RuntimeException> supplier = (ThrowingSupplier<T, RuntimeException>) (ThrowingSupplier) this;
|
||||
return supplier::get;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import java.util.function.Supplier;
|
|||
|
||||
/**
|
||||
* A utility class to hide checked exceptions from the java compiler.
|
||||
* For conversion of checked lambdas to unchecked lambdas, use {@link Assume}.
|
||||
*/
|
||||
public class Unchecked {
|
||||
/**
|
||||
|
@ -30,7 +29,7 @@ public class Unchecked {
|
|||
* @param <TEx> the exception the runnable might throw
|
||||
*/
|
||||
public static <TEx extends Throwable> void sneaky(@NotNull ThrowingRunnable<TEx> runnable) {
|
||||
Assume.isSafe(runnable).run();
|
||||
runnable.assumeSafe().run();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,7 +42,7 @@ public class Unchecked {
|
|||
* @return the result of the supplier
|
||||
*/
|
||||
public static <T, TEx extends Throwable> T sneaky(@NotNull ThrowingSupplier<T, TEx> supplier) {
|
||||
return Assume.isSafe(supplier).get();
|
||||
return supplier.assumeSafe().get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue