From bb38fe586898837dc62b9db11ed076d5396fa6e4 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 4 May 2024 20:58:07 +0200 Subject: [PATCH] docs(commons): annotate methods in Unchecked and Assume --- .../jfronny/commons/throwable/Assume.java | 9 +++++++-- .../jfronny/commons/throwable/Unchecked.java | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) 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 525662a..465f8be 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,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 the exception the runnable might throw * @return a runnable not marked to throw the checked exception */ - public static Runnable isSafe(ThrowingRunnable runnable) { + @Contract(pure = true) + public static @NotNull Runnable isSafe(@NotNull ThrowingRunnable runnable) { var rn = (ThrowingRunnable) (ThrowingRunnable) runnable; return rn::run; } @@ -29,7 +33,8 @@ public class Assume { * @param the exception the supplier might throw * @return a supplier not marked to throw the checked exception */ - public static Supplier isSafe(ThrowingSupplier supplier) { + @Contract(pure = true) + public static @NotNull Supplier isSafe(@NotNull ThrowingSupplier supplier) { var rn = (ThrowingSupplier) (ThrowingSupplier) supplier; return rn::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 055ed3c..fbf7bd9 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 @@ -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 sneakyThrow(Throwable t) { + @Contract("_ -> fail") + public static T sneakyThrow(@NotNull Throwable t) { return sneaky(() -> { throw t; }); @@ -25,7 +29,7 @@ public class Unchecked { * @param runnable the runnable to run * @param the exception the runnable might throw */ - public static void sneaky(ThrowingRunnable runnable) { + public static void sneaky(@NotNull ThrowingRunnable runnable) { Assume.isSafe(runnable).run(); } @@ -38,7 +42,7 @@ public class Unchecked { * @param the exception the supplier might throw * @return the result of the supplier */ - public static T sneaky(ThrowingSupplier supplier) { + public static T sneaky(@NotNull ThrowingSupplier supplier) { return Assume.isSafe(supplier).get(); } @@ -49,6 +53,7 @@ public class Unchecked { * @param the exception to reintroduce into the set of possible exceptions * @throws TEx the introduced exception */ + @SuppressWarnings("RedundantThrows") public static void reintroduce() throws TEx { } @@ -60,7 +65,8 @@ public class Unchecked { * @param the exception to reintroduce into the set of possible exceptions * @throws TEx the exception that was thrown by the runnable */ - public static void reintroduce(Runnable runnable) throws TEx { + @SuppressWarnings("RedundantThrows") + public static 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 reintroduce(Supplier supplier) throws TEx { + @SuppressWarnings("RedundantThrows") + public static T reintroduce(@NotNull Supplier supplier) throws TEx { return supplier.get(); } }