Skip to content

Commit

Permalink
api: Improved conditional API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed May 13, 2023
1 parent 0048eb1 commit bc6d51e
Showing 1 changed file with 46 additions and 9 deletions.
@@ -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<String> 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<String> 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;
}

Expand Down

0 comments on commit bc6d51e

Please sign in to comment.