docs(commons): annotate methods in Unchecked and Assume
ci/woodpecker/push/woodpecker Pipeline failed Details

This commit is contained in:
Johannes Frohnmeyer 2024-05-04 20:58:07 +02:00
parent 754f418ceb
commit bb38fe5868
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 19 additions and 7 deletions

View File

@ -1,5 +1,8 @@
package io.gitlab.jfronny.commons.throwable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
/**
@ -15,7 +18,8 @@ public class Assume {
* @param <TEx> the exception the runnable might throw
* @return a runnable not marked to throw the checked exception
*/
public static <TEx extends Throwable> Runnable isSafe(ThrowingRunnable<TEx> runnable) {
@Contract(pure = true)
public static <TEx extends Throwable> @NotNull Runnable isSafe(@NotNull ThrowingRunnable<TEx> runnable) {
var rn = (ThrowingRunnable<RuntimeException>) (ThrowingRunnable) runnable;
return rn::run;
}
@ -29,7 +33,8 @@ public class Assume {
* @param <TEx> the exception the supplier might throw
* @return a supplier not marked to throw the checked exception
*/
public static <T, TEx extends Throwable> Supplier<T> isSafe(ThrowingSupplier<T, TEx> supplier) {
@Contract(pure = 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;
}

View File

@ -1,5 +1,8 @@
package io.gitlab.jfronny.commons.throwable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
/**
@ -12,7 +15,8 @@ public class Unchecked {
*
* @param t the exception to throw
*/
public static <T> T sneakyThrow(Throwable t) {
@Contract("_ -> fail")
public static <T> T sneakyThrow(@NotNull Throwable t) {
return sneaky(() -> {
throw t;
});
@ -25,7 +29,7 @@ public class Unchecked {
* @param runnable the runnable to run
* @param <TEx> the exception the runnable might throw
*/
public static <TEx extends Throwable> void sneaky(ThrowingRunnable<TEx> runnable) {
public static <TEx extends Throwable> void sneaky(@NotNull ThrowingRunnable<TEx> runnable) {
Assume.isSafe(runnable).run();
}
@ -38,7 +42,7 @@ public class Unchecked {
* @param <TEx> the exception the supplier might throw
* @return the result of the supplier
*/
public static <T, TEx extends Throwable> T sneaky(ThrowingSupplier<T, TEx> supplier) {
public static <T, TEx extends Throwable> T sneaky(@NotNull ThrowingSupplier<T, TEx> supplier) {
return Assume.isSafe(supplier).get();
}
@ -49,6 +53,7 @@ public class Unchecked {
* @param <TEx> the exception to reintroduce into the set of possible exceptions
* @throws TEx the introduced exception
*/
@SuppressWarnings("RedundantThrows")
public static <TEx extends Throwable> void reintroduce() throws TEx {
}
@ -60,7 +65,8 @@ public class Unchecked {
* @param <TEx> the exception to reintroduce into the set of possible exceptions
* @throws TEx the exception that was thrown by the runnable
*/
public static <TEx extends Throwable> void reintroduce(Runnable runnable) throws TEx {
@SuppressWarnings("RedundantThrows")
public static <TEx extends Throwable> void reintroduce(@NotNull Runnable runnable) throws TEx {
runnable.run();
}
@ -74,7 +80,8 @@ public class Unchecked {
* @return the result of the supplier
* @throws TEx the exception that was thrown by the supplier
*/
public static <T, TEx extends Throwable> T reintroduce(Supplier<T> supplier) throws TEx {
@SuppressWarnings("RedundantThrows")
public static <T, TEx extends Throwable> T reintroduce(@NotNull Supplier<T> supplier) throws TEx {
return supplier.get();
}
}