From bc6d51e07c4d4fc2e8ba64d759cf234cc45e2d76 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sat, 13 May 2023 10:43:56 -0500 Subject: [PATCH] api: Improved conditional API methods --- .../api/utils/Conditions.java | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/io/github/miniplaceholders/api/utils/Conditions.java b/api/src/main/java/io/github/miniplaceholders/api/utils/Conditions.java index ddb7384..c15f121 100644 --- a/api/src/main/java/io/github/miniplaceholders/api/utils/Conditions.java +++ b/api/src/main/java/io/github/miniplaceholders/api/utils/Conditions.java @@ -1,36 +1,73 @@ package io.github.miniplaceholders.api.utils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.util.function.Supplier; /** Checks required for the correct operation of the expansions*/ public final class Conditions { - private Conditions(){} + private Conditions() {} /** - * Checks if a string is null or empty or consists of whitespace characters only + * Checks if a string is null or empty or consists of whitespace characters only. + * * @param string the string * @param name the name supplier * @return the string if it is not null or empty * @since 1.0.0 + * @throws IllegalStateException if the provided string is empty + */ + public static String nonNullOrEmptyString(@Nullable String string, @NotNull Supplier<@NotNull String> name){ + if (string == null) + throw new NullPointerException(name.get() + " cannot be null"); + if (string.isBlank()) + throw new IllegalStateException(name.get() + "cannot be an empty string"); + return string; + } + + /** + * Checks if a string is null or empty or consists of whitespace characters only. + * + * @param string the string + * @param name the name + * @return the string if it is not null or empty + * @since 2.2.0 + * @throws IllegalStateException if the provided string is empty */ - public static String nonNullOrEmptyString(String string, Supplier name){ + public static String nonNullOrEmptyString(@Nullable String string, @NotNull String name){ if (string == null) - throw new NullPointerException("the " + name.get() + " cannot be null"); + throw new NullPointerException(name + " cannot be null"); if (string.isBlank()) - throw new IllegalStateException("the " + name.get() + "cannot be an empty string"); + throw new IllegalStateException(name + "cannot be an empty string"); return string; } /** - * Check if a string is empty or consists of whitespace characters only + * Check if a string is empty or consists of whitespace characters only. + * * @param string the string * @param reason the reason supplier * @return the string if it not empty * @since 1.0.0 + * @throws IllegalStateException if the provided string is empty */ - public static String nonEmptyString(String string, Supplier reason){ - if (string.isBlank()) - throw new IllegalStateException(reason.get()); + public static String nonEmptyString(@NotNull String string, @NotNull Supplier<@NotNull String> reason){ + if (string.isBlank()) throw new IllegalStateException(reason.get()); + return string; + } + + /** + * Check if a string is empty or consists of whitespace characters only. + * + * @param string the string + * @param reason the reason + * @return the string if it not empty + * @since 2.2.0 + * @throws IllegalStateException if the provided string is empty + */ + public static String nonEmptyString(@NotNull String string, @NotNull String reason){ + if (string.isBlank()) throw new IllegalStateException(reason); return string; }