Class Requires

java.lang.Object
io.github.torand.javacommons.contract.Requires

public final class Requires extends Object
Provides factory methods to test the state of variables.
  • Method Details

    • requireNonEmpty

      public static int[] requireNonEmpty(int[] array, String msg, Object... args)
      Checks that the specified 'int' array is not null and contains at least one element. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Parameters:
      array - the array to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the array if not null or empty.
    • requireNonEmpty

      public static long[] requireNonEmpty(long[] array, String msg, Object... args)
      Checks that the specified 'long' array is not null and contains at least one element. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Parameters:
      array - the array to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the array if not null or empty.
    • requireNonEmpty

      public static <T> T[] requireNonEmpty(T[] array, String msg, Object... args)
      Checks that the specified object array is not null and contains at least one element. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Type Parameters:
      T - the object type.
      Parameters:
      array - the array to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the array if not null or empty.
    • requireNonEmpty

      public static <T extends Collection<?>> T requireNonEmpty(T collection, String msg, Object... args)
      Checks that the specified object collection is not null and contains at least one element. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Type Parameters:
      T - the object type.
      Parameters:
      collection - the collection to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the collection if not null or empty.
    • requireNonBlank

      public static String requireNonBlank(String string, String msg, Object... args)
      Checks that the specified string is not null and contains at least one character. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Parameters:
      string - the string to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the string if not null or blank.
    • requireNonEmpty

      public static <T> Optional<T> requireNonEmpty(Optional<T> optional, String msg, Object... args)
      Checks that the specified optional object is not null and is not empty. Throws a customized NullPointerException or IllegalArgumentException if it is.
      Type Parameters:
      T - the object type.
      Parameters:
      optional - the optional to check.
      msg - the exception message.
      args - the exception message parameters.
      Returns:
      the optional if not null or empty.
    • require

      public static void require(Requirement requirement, String msg, Object... args)
      Checks that the specified requirement is fulfilled. Throws a customized IllegalArgumentException if it is.
      Parameters:
      requirement - the requirement.
      msg - the exception message.
      args - the exception message parameters.
    • precondition

      public static void precondition(Requirement requirement, String msg, Object... args)
      Checks that the specified requirement is fulfilled. Throws a customized IllegalStateException if it is.
      Parameters:
      requirement - the requirement.
      msg - the exception message.
      args - the exception message parameters.