Class RequirementUtil

java.lang.Object
de.tomatengames.util.RequirementUtil

public class RequirementUtil extends Object
Provides convenient methods to check requirements. For example, requireNotNull(Object, String) can be used to ensure that parameters are not null.
Since:
1.3
  • Method Details

    • requireNotNull

      public static void requireNotNull(@Nullable Object obj, @NotNull String msg) throws IllegalArgumentException
      Ensures that the specified object is not null.
      Parameters:
      obj - The object that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be null" is appended.
      Throws:
      IllegalArgumentException - If the object is null.
    • requireNotNull

      public static void requireNotNull(@Nullable Object obj) throws IllegalArgumentException
      Ensures that the specified object is not null.
      Parameters:
      obj - The object that should be checked.
      Throws:
      IllegalArgumentException - If the object is null.
      See Also:
    • requireNotBlank

      public static void requireNotBlank(@Nullable String str, @NotNull String msg)
      Ensures that the specified string is not null and does not only contain whitespace characters.
      Parameters:
      str - The string that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be null" or "must not be blank" is appended.
      Throws:
      IllegalArgumentException - If the string is null or blank.
    • requireNotBlank

      public static void requireNotBlank(@Nullable String str)
      Ensures that the specified string is not null and does not only contain whitespace characters.
      Parameters:
      str - The string that should be checked.
      Throws:
      IllegalArgumentException - If the string is null or blank.
      See Also:
    • requireNotNegative

      public static void requireNotNegative(int num, @NotNull String msg)
      Ensures that the specified number is not negative. 0 is allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be negative (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is negative.
      Since:
      1.8
    • requireNotNegative

      public static void requireNotNegative(long num, @NotNull String msg)
      Ensures that the specified number is not negative. 0 is allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be negative (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is negative.
      Since:
      1.8
    • requireNotNegative

      public static void requireNotNegative(double num, @NotNull String msg)
      Ensures that the specified number is not negative. 0 is allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be negative (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is negative.
      Since:
      1.8
    • requirePositive

      public static void requirePositive(int num, @NotNull String msg)
      Ensures that the specified number is positive. 0 is not allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be positive (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not positive.
      Since:
      1.8
    • requirePositive

      public static void requirePositive(long num, @NotNull String msg)
      Ensures that the specified number is positive. 0 is not allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be positive (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not positive.
      Since:
      1.8
    • requirePositive

      public static void requirePositive(double num, @NotNull String msg)
      Ensures that the specified number is positive. 0 is not allowed.
      Parameters:
      num - The number that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be positive (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not positive.
      Since:
      1.8
    • requireLessOrEqual

      public static void requireLessOrEqual(int num, int max, @NotNull String msg)
      Ensures that the specified number is less than or equal to the maximum value.
      Parameters:
      num - The number that should be checked.
      max - The maximum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be less than or equal to max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is greater than the maximum value.
      Since:
      1.8
    • requireLessOrEqual

      public static void requireLessOrEqual(long num, long max, @NotNull String msg)
      Ensures that the specified number is less than or equal to the maximum value.
      Parameters:
      num - The number that should be checked.
      max - The maximum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be less than or equal to max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is greater than the maximum value.
      Since:
      1.8
    • requireLessOrEqual

      public static void requireLessOrEqual(double num, double max, @NotNull String msg)
      Ensures that the specified number is less than or equal to the maximum value.
      Parameters:
      num - The number that should be checked.
      max - The maximum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be less than or equal to max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is greater than the maximum value.
      Since:
      1.8
    • requireGreaterOrEqual

      public static void requireGreaterOrEqual(int num, int min, @NotNull String msg)
      Ensures that the specified number is greater than or equal to the minimum value.
      Parameters:
      num - The number that should be checked.
      min - The minimum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be greater than or equal to min (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is less than the minimum value.
      Since:
      1.8
    • requireGreaterOrEqual

      public static void requireGreaterOrEqual(long num, long min, @NotNull String msg)
      Ensures that the specified number is greater than or equal to the minimum value.
      Parameters:
      num - The number that should be checked.
      min - The minimum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be greater than or equal to min (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is less than the minimum value.
      Since:
      1.8
    • requireGreaterOrEqual

      public static void requireGreaterOrEqual(double num, double min, @NotNull String msg)
      Ensures that the specified number is greater than or equal to the minimum value.
      Parameters:
      num - The number that should be checked.
      min - The minimum value.
      msg - The message that should be passed to the exception. If the message ends with "...", "must be greater than or equal to min (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is less than the minimum value.
      Since:
      1.8
    • requireInRange

      public static void requireInRange(int num, int min, int max, @NotNull String msg)
      Ensures that the specified number is in the range [min, max].
      Parameters:
      num - The number that should be checked.
      min - The minimum value (inclusive).
      max - The maximum value (inclusive).
      msg - The message that should be passed to the exception. If the message ends with "...", "must be between min and max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not within the range.
      Since:
      1.8
    • requireInRange

      public static void requireInRange(long num, long min, long max, @NotNull String msg)
      Ensures that the specified number is in the range [min, max].
      Parameters:
      num - The number that should be checked.
      min - The minimum value (inclusive).
      max - The maximum value (inclusive).
      msg - The message that should be passed to the exception. If the message ends with "...", "must be between min and max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not within the range.
      Since:
      1.8
    • requireInRange

      public static void requireInRange(double num, double min, double max, @NotNull String msg)
      Ensures that the specified number is in the range [min, max].
      Parameters:
      num - The number that should be checked.
      min - The minimum value (inclusive).
      max - The maximum value (inclusive).
      msg - The message that should be passed to the exception. If the message ends with "...", "must be between min and max (was num)" is appended.
      Throws:
      IllegalArgumentException - If the number is not within the range.
      Since:
      1.8
    • requireNotEmpty

      public static void requireNotEmpty(@Nullable Collection<?> collection, @NotNull String msg)
      Ensures that the specified Collection is not null and contains at least 1 element.
      Parameters:
      collection - The collection that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be null" or "must not be empty" is appended.
      Throws:
      IllegalArgumentException - If the collection is null or empty.
      Since:
      1.8
    • requireNotEmpty

      public static <T> void requireNotEmpty(T[] array, @NotNull String msg)
      Ensures that the specified array is not null and has a length of at least 1.
      Type Parameters:
      T - The type of the array elements.
      Parameters:
      array - The array that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", "must not be null" or "must not be empty" is appended.
      Throws:
      IllegalArgumentException - If the array is null or has a length of 0.
      Since:
      1.8
    • requireMinSize

      public static void requireMinSize(@Nullable Collection<?> collection, int minSize, @NotNull String msg)
      Ensures that the specified Collection is not null and has a size of at least the specified minimum size.
      Parameters:
      collection - The collection that should be checked.
      minSize - The minimum size that the collection should have.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the collection is null or has a size less than the specified minimum size.
      Since:
      1.8
    • requireMinLength

      public static <T> void requireMinLength(T[] array, int minLength, @NotNull String msg)
      Ensures that the specified array is not null and has a length of at least the specified minimum length.
      Type Parameters:
      T - The type of the array elements.
      Parameters:
      array - The array that should be checked.
      minLength - The minimum length that the array should have.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the array is null or has a length less than the specified minimum length.
      Since:
      1.8
    • requireExactSize

      public static void requireExactSize(@Nullable Collection<?> collection, int size, @NotNull String msg)
      Ensures that the specified collection is not null and has exactly the specified number of elements.
      Parameters:
      collection - The collection that should be checked.
      size - The exact number of elements that the collection should have.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the collection is null or has a different number of elements than the specified size.
      Since:
      1.8
    • requireExactLength

      public static <T> void requireExactLength(T[] array, int length, @NotNull String msg)
      Ensures that the specified array is not null and has exactly the specified length.
      Type Parameters:
      T - The type of the array elements.
      Parameters:
      array - The array that should be checked.
      length - The exact length that the array should have.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the array is null or has a different length than the specified length.
      Since:
      1.8
    • requireSizeInRange

      public static void requireSizeInRange(@Nullable Collection<?> collection, int minSize, int maxSize, @NotNull String msg)
      Ensures that the specified collection is not null and has a size within the specified range.
      Parameters:
      collection - The collection that should be checked.
      minSize - The minimum allowed size of the collection.
      maxSize - The maximum allowed size of the collection.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the collection is null or has a size outside the specified range.
      Since:
      1.8
    • requireLengthInRange

      public static <T> void requireLengthInRange(T[] array, int minLength, int maxLength, @NotNull String msg)
      Ensures that the specified array is not null and has a length within the specified range.
      Type Parameters:
      T - The type of elements in the array.
      Parameters:
      array - The array that should be checked.
      minLength - The minimum allowed length of the array.
      maxLength - The maximum allowed length of the array.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the array is null or has a length outside the specified range.
      Since:
      1.8
    • requireForAll

      public static <T> void requireForAll(@Nullable Collection<T> collection, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix)
      Ensures that the specified Collection is not null and all elements satisfy the specified predicate.
      Type Parameters:
      T - The type of elements in the collection.
      Parameters:
      collection - The collection that should be checked.
      predicate - The predicate that should be checked for each element in the collection. Not null.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      elementErrorMsgSuffix - The suffix to be appended to the error message if the predicate fails for an element. If null, a generic error message is used. Example: "must not contain a null value".
      Throws:
      IllegalArgumentException - If the collection is null or any of its elements do not satisfy the predicate.
      Since:
      1.8
    • requireForAll

      public static <T> void requireForAll(T[] array, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix)
      Ensures that the specified array is not null and all elements satisfy the specified predicate.
      Type Parameters:
      T - The type of elements in the array.
      Parameters:
      array - The array that should be checked.
      predicate - The predicate that should be checked for each element in the array. Not null.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      elementErrorMsgSuffix - The suffix to be appended to the error message if the predicate fails for an element. If null, a generic error message is used. Example: "must not contain a null value".
      Throws:
      IllegalArgumentException - If the array is null or any of its elements do not satisfy the predicate.
      Since:
      1.8
    • requireAllNotNull

      public static <T> void requireAllNotNull(@Nullable Collection<T> collection, @NotNull String msg)
      Ensures that the specified collection and all of its elements are not null.
      Type Parameters:
      T - The type of elements in the collection.
      Parameters:
      collection - The collection that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the collection is null or any of its elements are null.
      Since:
      1.8
    • requireAllNotNull

      public static <T> void requireAllNotNull(T[] array, @NotNull String msg)
      Ensures that the specified array and all of its elements are not null.
      Type Parameters:
      T - The type of elements in the array.
      Parameters:
      array - The array that should be checked.
      msg - The message that should be passed to the exception. If the message ends with "...", a suffix like "must not be null" with a meaningful error description is appended.
      Throws:
      IllegalArgumentException - If the array is null or any of its elements are null.
      Since:
      1.8