diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/concurrent/WithScopedValue.java b/commons/src/main/java/io/gitlab/jfronny/commons/concurrent/WithScopedValue.java index 1c429ba..1b29baa 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/concurrent/WithScopedValue.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/concurrent/WithScopedValue.java @@ -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 { @@ -10,18 +9,23 @@ public interface WithScopedValue { default void withContext(Action action) throws TEx1, TEx2 { Unchecked.reintroduce(); - Unchecked.reintroduce(() -> ScopedValue.runWhere(getAttached(), self(), Assume.isSafe(action::run))); + Unchecked.reintroduce(() -> ScopedValue.runWhere(getAttached(), self(), action.assumeSafe())); } default T withContext(Returnable action) throws TEx1, TEx2 { Unchecked.reintroduce(); - return Unchecked.reintroduce(() -> ScopedValue.getWhere(getAttached(), self(), Assume.isSafe(action::run))); + return Unchecked.reintroduce(() -> ScopedValue.getWhere(getAttached(), self(), action.assumeSafe())); } - interface Returnable { + interface Returnable extends ThrowingSupplier { T run() throws TEx1, TEx2; + + @Override + default T get() throws Throwable { + return run(); + } } - interface Action { + interface Action extends ThrowingRunnable { void run() throws TEx1, TEx2; } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Assume.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Assume.java index 465f8be..e8d6584 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Assume.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Assume.java @@ -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 the exception the runnable might throw - * @return a runnable not marked to throw the checked exception - */ - @Contract(pure = true) + @Deprecated(forRemoval = true) public static @NotNull Runnable isSafe(@NotNull ThrowingRunnable runnable) { - var rn = (ThrowingRunnable) (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 the return type of the supplier - * @param the exception the supplier might throw - * @return a supplier not marked to throw the checked exception - */ - @Contract(pure = true) + @Deprecated(forRemoval = true) public static @NotNull Supplier isSafe(@NotNull ThrowingSupplier supplier) { - var rn = (ThrowingSupplier) (ThrowingSupplier) supplier; - return rn::get; + return supplier.assumeSafe(); } @Deprecated(forRemoval = true) diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiConsumer.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiConsumer.java index d28aed5..22a2b08 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiConsumer.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiConsumer.java @@ -98,7 +98,7 @@ public interface ThrowingBiConsumer { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull BiConsumer 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingBiConsumer consumer = (ThrowingBiConsumer) (ThrowingBiConsumer) this; + return consumer::accept; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiFunction.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiFunction.java index 29e0cf6..0324be8 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiFunction.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBiFunction.java @@ -93,7 +93,7 @@ public interface ThrowingBiFunction { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull BiFunction 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingBiFunction function = (ThrowingBiFunction) (ThrowingBiFunction) this; + return function::apply; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBooleanSupplier.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBooleanSupplier.java index f0e6fd6..d4f6e0c 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBooleanSupplier.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingBooleanSupplier.java @@ -94,7 +94,7 @@ public interface ThrowingBooleanSupplier { @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 { } }; } + + /** + * 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 supplier = (ThrowingBooleanSupplier) (ThrowingBooleanSupplier) this; + return supplier::get; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingConsumer.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingConsumer.java index e03b123..a50d81f 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingConsumer.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingConsumer.java @@ -86,7 +86,7 @@ public interface ThrowingConsumer { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull Consumer 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingConsumer consumer = (ThrowingConsumer) (ThrowingConsumer) this; + return consumer::accept; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingFunction.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingFunction.java index eacc2f1..329c998 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingFunction.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingFunction.java @@ -69,7 +69,7 @@ public interface ThrowingFunction { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull Function 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingFunction function = (ThrowingFunction) (ThrowingFunction) this; + return function::apply; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingPredicate.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingPredicate.java index a03cc88..9b4adc2 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingPredicate.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingPredicate.java @@ -114,7 +114,7 @@ public interface ThrowingPredicate { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull Predicate 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingPredicate predicate = (ThrowingPredicate) (ThrowingPredicate) this; + return predicate::test; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingRunnable.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingRunnable.java index 350556e..d14dae7 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingRunnable.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingRunnable.java @@ -62,7 +62,7 @@ public interface ThrowingRunnable { @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 { } }; } + + /** + * 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 runnable = (ThrowingRunnable) (ThrowingRunnable) this; + return runnable::run; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingSupplier.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingSupplier.java index 4ad8b31..e1eeeae 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingSupplier.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/ThrowingSupplier.java @@ -56,7 +56,7 @@ public interface ThrowingSupplier { @Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable default @NotNull Supplier 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 { } }; } + + /** + * 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 assumeSafe() { + ThrowingSupplier supplier = (ThrowingSupplier) (ThrowingSupplier) this; + return supplier::get; + } } diff --git a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Unchecked.java b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Unchecked.java index fbf7bd9..57ce2d8 100644 --- a/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Unchecked.java +++ b/commons/src/main/java/io/gitlab/jfronny/commons/throwable/Unchecked.java @@ -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 the exception the runnable might throw */ public static void sneaky(@NotNull ThrowingRunnable runnable) { - Assume.isSafe(runnable).run(); + runnable.assumeSafe().run(); } /** @@ -43,7 +42,7 @@ public class Unchecked { * @return the result of the supplier */ public static T sneaky(@NotNull ThrowingSupplier supplier) { - return Assume.isSafe(supplier).get(); + return supplier.assumeSafe().get(); } /**