Compare commits
3 Commits
bb38fe5868
...
e1d6f578a8
Author | SHA1 | Date |
---|---|---|
Johannes Frohnmeyer | e1d6f578a8 | |
Johannes Frohnmeyer | 0e15fe3abd | |
Johannes Frohnmeyer | 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)
|
||||
|
|
|
@ -8,6 +8,10 @@ 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) {
|
||||
return Objects.requireNonNull(consumer)::accept;
|
||||
}
|
||||
|
||||
void accept(T var1, U var2) throws TEx;
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
|
@ -98,7 +102,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 +117,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,10 @@ 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) {
|
||||
return Objects.requireNonNull(function)::apply;
|
||||
}
|
||||
|
||||
R apply(T var1, U var2) throws TEx;
|
||||
|
||||
@Contract(value = "-> new", pure = true)
|
||||
|
@ -93,7 +97,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 +112,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,10 @@ import java.util.function.*;
|
|||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
|
||||
static <TEx extends Throwable> @NotNull ThrowingBooleanSupplier<TEx> of(@NotNull BooleanSupplier supplier) {
|
||||
return Objects.requireNonNull(supplier)::getAsBoolean;
|
||||
}
|
||||
|
||||
boolean get() throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -94,7 +98,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 +113,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ import java.util.function.Function;
|
|||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingConsumer<T, TEx extends Throwable> {
|
||||
static <T> @NotNull ThrowingConsumer<T, RuntimeException> of(@NotNull Consumer<T> consumer) {
|
||||
return Objects.requireNonNull(consumer)::accept;
|
||||
}
|
||||
|
||||
void accept(T var1) throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -86,7 +90,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 +105,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,10 @@ 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) {
|
||||
return Objects.requireNonNull(function)::apply;
|
||||
}
|
||||
|
||||
R apply(T var1) throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -69,7 +73,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 +88,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ import java.util.function.Predicate;
|
|||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingPredicate<T, TEx extends Throwable> {
|
||||
static <T> @NotNull ThrowingPredicate<T, RuntimeException> of(@NotNull Predicate<T> predicate) {
|
||||
return Objects.requireNonNull(predicate)::test;
|
||||
}
|
||||
|
||||
boolean test(T var1) throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -114,7 +118,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 +133,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ import java.util.function.Function;
|
|||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingRunnable<TEx extends Throwable> {
|
||||
static @NotNull ThrowingRunnable<RuntimeException> of(@NotNull Runnable runnable) {
|
||||
return Objects.requireNonNull(runnable)::run;
|
||||
}
|
||||
|
||||
void run() throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -62,7 +66,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 +81,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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,10 @@ import java.util.function.Supplier;
|
|||
|
||||
@FunctionalInterface
|
||||
public interface ThrowingSupplier<T, TEx extends Throwable> {
|
||||
static <T> @NotNull ThrowingSupplier<T, RuntimeException> of(@NotNull Supplier<T> supplier) {
|
||||
return Objects.requireNonNull(supplier)::get;
|
||||
}
|
||||
|
||||
T get() throws TEx;
|
||||
|
||||
@Contract(value = "_ -> new", pure = true)
|
||||
|
@ -56,7 +60,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 +75,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