Package me.sbasalaev

Class Require

java.lang.Object
me.sbasalaev.Require

public final class Require extends Object
Precondition checks. This class contains convenience static methods that help to check whether the preconditions of a method or constructor are met. In contrast to Assert these should only be used when the exceptional situation is the caller's fault. The method names are chosen so that Require class itself is imported rather than individual methods. For instance, the start of the method repeating the string given number of times could be
String repeat(String string, int times) {
    Require.nonNull(string, "string");
    Require.nonNegative(times, "times");
    ...
}
See Also:
  • Method Details

    • nonNull

      public static <T> T nonNull(@Nullable T value, String name)
      Checks that the value is not null.
      Type Parameters:
      T - the type of the value.
      Parameters:
      value - the value to be checked.
      name - the name of the parameter to report when there is null.
      Returns:
      the value if it is not null.
      Throws:
      NullPointerException - if the value is null.
      Since:
      4.1
      See Also:
    • noNulls

      public static <T> T[] noNulls(@Nullable T @Nullable [] array, String name)
      Checks that the array is not null and contains no null elements.
      Type Parameters:
      T - the type of the elements of the array.
      Parameters:
      array - the array to check for null references.
      name - the name of the parameter to report when there is null.
      Returns:
      the array if it is not null and contains no null elements.
      Throws:
      NullPointerException - if either array or any of its elements is null.
      Since:
      4.1
    • noNulls

      public static <T> T[] noNulls(@Nullable T @Nullable [] array)
      Checks that the array is not null and contains no null elements.
      Type Parameters:
      T - the type of the elements of the array.
      Parameters:
      array - the array to check for null references.
      Returns:
      the array if it is not null and contains no null elements.
      Throws:
      NullPointerException - if either array or any of its elements is null.
    • argument

      public static void argument(boolean condition, Supplier<String> message)
      Ensures condition is true.
      Parameters:
      condition - a boolean expression to be checked.
      message - the message to be constructed and reported when condition is false.
      Throws:
      IllegalArgumentException - if condition is false.
    • argument

      public static void argument(boolean condition, String message)
      Ensures condition is true.
      Parameters:
      condition - a boolean expression to be checked.
      message - the message to be reported when condition is false.
      Throws:
      IllegalArgumentException - if condition is false.
    • state

      public static void state(boolean condition, Supplier<String> message)
      Ensures condition is true.
      Parameters:
      condition - a boolean expression to be checked.
      message - the message to be constructed and reported when condition is false.
      Throws:
      IllegalStateException - if condition is false.
    • state

      public static void state(boolean condition, String message)
      Ensures condition is true.
      Parameters:
      condition - a boolean expression to be checked.
      message - the message to be reported when condition is false.
      Throws:
      IllegalStateException - if condition is false.
    • nonNegative

      public static int nonNegative(int value, String name)
      Checks that the value is non-negative and returns it.
      Parameters:
      value - the value to be checked.
      name - the name of the parameter to report when the value is negative.
      Returns:
      the value if it is non-negative.
      Throws:
      IllegalArgumentException - if the value is negative.
    • nonNegative

      public static long nonNegative(long value, String name)
      Checks that the value is non-negative and returns it.
      Parameters:
      value - the value to be checked.
      name - the name of the parameter to report when the value is negative.
      Returns:
      the value if it is non-negative.
      Throws:
      IllegalArgumentException - if the value is negative.
      Since:
      4.1
    • positive

      public static int positive(int value, String name)
      Checks that the value is positive and returns it.
      Parameters:
      value - the value to be checked.
      name - the name of the parameter to report when the value is not positive.
      Returns:
      the value if it is positive.
      Throws:
      IllegalArgumentException - if the value is zero or negative.
      Since:
      4.1
    • positive

      public static long positive(long value, String name)
      Checks that the value is positive and returns it.
      Parameters:
      value - the value to be checked.
      name - the name of the parameter to report when the value is not positive.
      Returns:
      the value if it is positive.
      Throws:
      IllegalArgumentException - if the value is zero or negative.
      Since:
      4.1