Class Checks

java.lang.Object
dev.prozilla.pine.common.util.checks.Checks

public final class Checks extends Object
Static utility methods for throwing exceptions if certain conditions are not met at runtime.
  • Method Details

    • isNotNull

      public static <O> O isNotNull(O object, String name) throws InvalidObjectException
      Throws:
      InvalidObjectException
    • areDisjunct

      public static <E> void areDisjunct(E[] arrayA, E[] arrayB) throws InvalidArrayException
      Checks that the given arrays are disjunct and throws an InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      arrayA - The first array
      arrayB - The second array
      Throws:
      InvalidArrayException - If arrayA and arrayB are not disjunct.
    • areDisjunct

      public static <E> void areDisjunct(E[] arrayA, E[] arrayB, String message) throws InvalidArrayException
      Checks that the given arrays are disjunct and throws a customized InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      arrayA - The first array
      arrayB - The second array
      message - The message to be used in the event that an InvalidArrayException is thrown
      Throws:
      InvalidArrayException - If arrayA and arrayB are not disjunct.
    • isNotEmpty

      public static <E> E[] isNotEmpty(E[] array) throws InvalidArrayException
      Checks that the given array is not empty and throws an InvalidArrayException if it is.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      Returns:
      array if it is not empty.
      Throws:
      InvalidArrayException - If array is empty.
    • isNotEmpty

      public static <E> E[] isNotEmpty(E[] array, String message) throws InvalidArrayException
      Checks that the given array is not empty and throws a customized InvalidArrayException if it is.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      message - The message to be used in the event that an InvalidArrayException is thrown
      Returns:
      array if it is not empty.
      Throws:
      InvalidArrayException - If array is empty.
    • hasMinLength

      public static <E> E[] hasMinLength(E[] array, int minLength) throws InvalidArrayException
      Checks that the given array's length is greater than or equal to the minimum length and throws an InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      minLength - The minimum length of the array (inclusive)
      Returns:
      array if its length is greater or equal.
      Throws:
      InvalidArrayException - If length of array is less than the minimum length.
    • hasMinLength

      public static <E> E[] hasMinLength(E[] array, int minLength, String message) throws InvalidArrayException
      Checks that the given array's length is greater than or equal to the minimum length and throws a customized InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      minLength - The minimum length of the array (inclusive)
      message - The message to be used in the event that an InvalidArrayException is thrown
      Returns:
      array if its length is greater or equal.
      Throws:
      InvalidArrayException - If length of array is less than the minimum length.
    • hasMaxLength

      public static <E> E[] hasMaxLength(E[] array, int maxLength) throws InvalidArrayException
      Checks that the given array's length is less than or equal to the maximum length and throws an InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      maxLength - The maximum length of the array (inclusive)
      Returns:
      array if its length is less or equal.
      Throws:
      InvalidArrayException - If length of array is greater than the maximum length.
    • hasMaxLength

      public static <E> E[] hasMaxLength(E[] array, int maxLength, String message) throws InvalidArrayException
      Checks that the given array's length is less than or equal to the maximum length and throws a customized InvalidArrayException if it is not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      maxLength - The maximum length of the array (inclusive)
      message - The message to be used in the event that an InvalidArrayException is thrown
      Returns:
      array if its length is less or equal.
      Throws:
      InvalidArrayException - If length of array is greater than the maximum length.
    • hasLength

      public static <E> E[] hasLength(E[] array, int length) throws InvalidArrayException
      Checks that the given array has a given length and throws an InvalidArrayException if it does not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      length - The required length of the array
      Returns:
      array if it matches the required length.
      Throws:
      InvalidArrayException - If array matches the required length.
    • hasLength

      public static <E> E[] hasLength(E[] array, int length, String message) throws InvalidArrayException
      Checks that the given array has a given length and throws a customized InvalidArrayException if it does not.
      Type Parameters:
      E - Type of the array elements
      Parameters:
      array - The array to check
      length - The required length of the array
      message - The message to be used in the event that an InvalidArrayException is thrown
      Returns:
      array if it matches the required length.
      Throws:
      InvalidArrayException - If array matches the required length.
    • isNotBlank

      public static String isNotBlank(@Nonnull String string) throws InvalidStringException
      Checks that the given string is not blank and throws an InvalidStringException if it is not.
      Parameters:
      string - The string to check
      Returns:
      string if it is not blank.
      Throws:
      InvalidStringException - If string is blank.
    • isNotBlank

      public static String isNotBlank(@Nonnull String string, String message) throws InvalidStringException
      Checks that the given string is not blank and throws a customized InvalidStringException if it is.
      Parameters:
      string - The string to check
      message - The message to be used in the event that an InvalidStringException is thrown
      Returns:
      string if it is not blank.
      Throws:
      InvalidStringException - If string is blank.
    • isNotEmpty

      public static String isNotEmpty(@Nonnull String string) throws InvalidStringException
      Checks that the given string is not empty and throws an InvalidStringException if it is not.
      Parameters:
      string - The string to check
      Returns:
      string if it is not empty.
      Throws:
      InvalidStringException - If string is empty.
    • isNotEmpty

      public static String isNotEmpty(@Nonnull String string, String message) throws InvalidStringException
      Checks that the given string is not empty and throws a customized InvalidStringException if it is.
      Parameters:
      string - The string to check
      message - The message to be used in the event that an InvalidStringException is thrown
      Returns:
      string if it is not empty.
      Throws:
      InvalidStringException - If string is empty.
    • hasPrefix

      public static String hasPrefix(@Nonnull String string, @Nonnull String prefix) throws InvalidStringException
      Checks that the given string starts with a prefix and throws an InvalidStringException if it doesn't.
      Parameters:
      string - The string to check
      prefix - The required prefix of the string
      Returns:
      string if it starts with a given prefix.
      Throws:
      InvalidStringException - If string does not start with a given prefix.
    • hasPrefix

      public static String hasPrefix(@Nonnull String string, @Nonnull String prefix, String message) throws InvalidStringException
      Checks that the given string starts with a prefix and throws a customized InvalidStringException if it doesn't.
      Parameters:
      string - The string to check
      prefix - The required prefix of the string
      message - The message to be used in the event that an InvalidStringException is thrown
      Returns:
      string if it starts with a given prefix.
      Throws:
      InvalidStringException - If string does not start with a given prefix.
    • hasSuffix

      public static String hasSuffix(@Nonnull String string, @Nonnull String suffix) throws InvalidStringException
      Checks that the given string ends with a suffix and throws an InvalidStringException if it doesn't.
      Parameters:
      string - The string to check
      suffix - The required suffix of the string
      Returns:
      string if it ends with a given suffix.
      Throws:
      InvalidStringException - If string does not end with a given suffix.
    • hasSuffix

      public static String hasSuffix(@Nonnull String string, @Nonnull String suffix, String message) throws InvalidStringException
      Checks that the given string ends with a suffix and throws a customized InvalidStringException if it doesn't.
      Parameters:
      string - The string to check
      suffix - The required suffix of the string
      message - The message to be used in the event that an InvalidStringException is thrown
      Returns:
      string if it ends with a given suffix.
      Throws:
      InvalidStringException - If string does not end with a given suffix.
    • isPositive

      public static float isPositive(float number, boolean strict) throws InvalidNumberException
      Checks that the given number is a positive number and throws an InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      strict - Whether to throw an exception if number is 0f
      Returns:
      number if it is positive.
      Throws:
      InvalidNumberException - If number is not positive.
    • isPositive

      public static float isPositive(float number, boolean strict, String message) throws InvalidNumberException
      Checks that the given number is a positive number and throws a customized InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      strict - Whether to throw an exception if number is 0f
      message - The message to be used in the event that an InvalidNumberException is thrown
      Returns:
      number if it is positive.
      Throws:
      InvalidNumberException - If number is not positive.
    • isPositive

      public static int isPositive(int number, boolean strict) throws InvalidNumberException
      Checks that the given number is a positive number and throws an InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      strict - Whether to throw an exception if number is 0
      Returns:
      number if it is positive.
      Throws:
      InvalidNumberException - If number is not positive.
    • isPositive

      public static int isPositive(int number, boolean strict, String message) throws InvalidNumberException
      Checks that the given number is a positive number and throws a customized InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      strict - Whether to throw an exception if number is 0
      message - The message to be used in the event that an InvalidNumberException is thrown
      Returns:
      number if it is positive.
      Throws:
      InvalidNumberException - If number is not positive.
    • isInRange

      public static int isInRange(int number, int min, int max) throws InvalidNumberException
      Checks that the given number is in the given range and throws an InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      min - Lower bound (inclusive)
      max - Upper bound (inclusive)
      Returns:
      number if it is in the range.
      Throws:
      InvalidNumberException - If number is not in the range.
    • isInRange

      public static int isInRange(int number, int min, int max, String message) throws InvalidNumberException
      Checks that the given number is in the given range and throws a customized InvalidNumberException if it is not.
      Parameters:
      number - The number to check
      min - Lower bound (inclusive)
      max - Upper bound (inclusive)
      message - The message to be used in the event that an InvalidNumberException is thrown
      Returns:
      number if it is in the range.
      Throws:
      InvalidNumberException - If number is not in the range.
    • array

      public static <E> ArrayChecks<E> array(E[] value, String name)
    • collection

      public static <E> CollectionChecks<E> collection(Collection<E> value, String name)
    • integer

      public static IntChecks integer(int value, String name)
    • object

      public static ObjectChecks object(Object value, String name)
    • string

      public static StringChecks string(String value, String name)