Class KiwiPreconditions

java.lang.Object
org.kiwiproject.base.KiwiPreconditions

public final class KiwiPreconditions extends Object
Static utility methods similar to those found in Preconditions, but with a lovely Kiwi flavor to them. That class has good documentation, so go read it if you need more information on the intent and general usage.

If you're looking for preconditions related to validating arguments using Jakarta Beans Validation, they are in KiwiValidations.

Implementation Note:
Several methods in this class use Lombok SneakyThrows so that they do not need to declare that they throw Exceptions of type T, for the case that T is a checked exception. Read more details about how this works in SneakyThrows. Most notably, this should give you more insight into how the JVM (versus Java the language) actually work: "The JVM does not check for the consistency of the checked exception system; javac does, and this annotation lets you opt out of its mechanism."
  • Method Details

    • checkArgument

      public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType)
      Ensures the truth of an expression involving one or more parameters to the calling method.

      Throws an Exception of type T if expression is false.

      Type Parameters:
      T - the type of exception
      Parameters:
      expression - a boolean expression
      exceptionType - the type of exception to be thrown if expression is false
      Implementation Note:
      This uses Lombok SneakyThrows to throw any checked exceptions without declaring them.
    • checkArgument

      public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType, String errorMessage)
      Ensures the truth of an expression involving one or more parameters to the calling method.

      Throws an Exception of type T if expression is false.

      Type Parameters:
      T - the type of exception
      Parameters:
      expression - a boolean expression
      exceptionType - the type of exception to be thrown if expression is false
      errorMessage - the exception message to use if the check fails
      Implementation Note:
      This uses Lombok SneakyThrows to throw any checked exceptions without declaring them.
    • checkArgument

      public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures the truth of an expression involving one or more parameters to the calling method.

      Throws an Exception of type T if expression is false.

      Type Parameters:
      T - the type of exception
      Parameters:
      expression - a boolean expression
      exceptionType - the type of exception to be thrown if expression is false
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
      Throws:
      NullPointerException - if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)
      Implementation Note:
      This uses Lombok SneakyThrows to throw any checked exceptions without declaring them.
    • requireNotBlank

      public static String requireNotBlank(String value)
      Ensures that a String passed as a parameter to the calling method is not blank, throwing an IllegalArgumentException if blank or returning the String otherwise.
      Parameters:
      value - the String value to check
      Returns:
      the given String
      See Also:
    • requireNotBlank

      public static String requireNotBlank(String value, String errorMessage)
      Ensures that a String passed as a parameter to the calling method is not blank, throwing an IllegalArgumentException if blank or returning the String otherwise.
      Parameters:
      value - the String value to check
      errorMessage - the error message for the exception
      Returns:
      the given String
      See Also:
    • requireNotBlank

      public static String requireNotBlank(String value, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that a String passed as a parameter to the calling method is not blank, throwing an IllegalArgumentException if blank or returning the String otherwise.
      Parameters:
      value - the String value to check
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
      Returns:
      the given String
      See Also:
    • requireNotNull

      public static <T> T requireNotNull(T reference)
      Ensures that an object reference passed as a parameter to the calling method is not null, throwing an IllegalArgumentException if null or returning the (non-null) reference otherwise.
      Type Parameters:
      T - the type of object
      Parameters:
      reference - an object reference
      Returns:
      the object type
    • requireNotNull

      public static <T> T requireNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that an object reference passed as a parameter to the calling method is not null, throwing an IllegalArgumentException if null or returning the (non-null) reference otherwise.
      Type Parameters:
      T - the type of object
      Parameters:
      reference - an object reference
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
      Returns:
      the object type
    • checkArgumentNotNull

      public static <T> void checkArgumentNotNull(T reference)
      Ensures that an object reference passed as a parameter to the calling method is not null, throwing an IllegalArgumentException if null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
    • checkArgumentNotNull

      public static <T> void checkArgumentNotNull(T reference, String errorMessage)
      Ensures that an object reference passed as a parameter to the calling method is not null, throwing an IllegalArgumentException if null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
      errorMessage - the error message for the exception
    • checkArgumentNotNull

      public static <T> void checkArgumentNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that an object reference passed as a parameter to the calling method is not null, throwing an IllegalArgumentException if null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
    • checkOnlyOneArgumentIsNull

      public static <T> void checkOnlyOneArgumentIsNull(T first, T second)
      Ensures that only one of two given arguments is null. Throws IllegalArgumentException if both are null or both are non-null.
      Type Parameters:
      T - the object type
      Parameters:
      first - the first argument
      second - the second argument
    • checkOnlyOneArgumentIsNull

      public static <T> void checkOnlyOneArgumentIsNull(T first, T second, String message)
      Ensures that only one of two given arguments is null. Throws IllegalArgumentException if both are null or both are non-null.
      Type Parameters:
      T - the object type
      Parameters:
      first - the first argument
      second - the second argument
      message - the error message to use if the check fails
    • checkOnlyOneArgumentIsNull

      public static <T> void checkOnlyOneArgumentIsNull(T first, T second, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that only one of two given arguments is null. Throws IllegalArgumentException if both are null or both are non-null.
      Type Parameters:
      T - the object type
      Parameters:
      first - the first argument
      second - the second argument
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
    • checkArgumentIsNull

      public static <T> void checkArgumentIsNull(T reference)
      Ensures that an object reference passed as a parameter to the calling method is null, throwing an IllegalArgumentException if not null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
    • checkArgumentIsNull

      public static <T> void checkArgumentIsNull(T reference, String errorMessage)
      Ensures that an object reference passed as a parameter to the calling method is null, throwing an IllegalArgumentException if not null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
      errorMessage - the error message for the exception
    • checkArgumentIsNull

      public static <T> void checkArgumentIsNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that an object reference passed as a parameter to the calling method is null, throwing an IllegalArgumentException if not null.
      Type Parameters:
      T - the object type
      Parameters:
      reference - an object reference
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentNotBlank

      public static void checkArgumentNotBlank(String string)
      Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing an IllegalArgumentException if it is null, empty, or blank.
      Parameters:
      string - a string
    • checkArgumentNotBlank

      public static void checkArgumentNotBlank(String string, String errorMessage)
      Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing an IllegalArgumentException if it is null, empty, or blank.
      Parameters:
      string - a string
      errorMessage - the error message for the exception
    • checkArgumentNotBlank

      public static void checkArgumentNotBlank(String string, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing an IllegalArgumentException if it is null, empty, or blank.
      Parameters:
      string - a string
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
    • checkArgumentIsBlank

      public static void checkArgumentIsBlank(String string)
      Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing an IllegalArgumentException if it is not null, empty, or blank.
      Parameters:
      string - a string
    • checkArgumentIsBlank

      public static void checkArgumentIsBlank(String string, String errorMessage)
      Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing an IllegalArgumentException if it is not null, empty, or blank.
      Parameters:
      string - a string
      errorMessage - the error message for the exception
    • checkArgumentIsBlank

      public static void checkArgumentIsBlank(String string, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing an IllegalArgumentException if it is not null, empty, or blank.
      Parameters:
      string - a string
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentNotEmpty

      public static <T> void checkArgumentNotEmpty(Collection<T> collection)
      Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the collection is null or empty.
      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
    • checkArgumentNotEmpty

      public static <T> void checkArgumentNotEmpty(Collection<T> collection, String errorMessage)
      Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the collection is null or empty.
      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
      errorMessage - the error message for the exception
    • checkArgumentNotEmpty

      public static <T> void checkArgumentNotEmpty(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the collection is null or empty.
      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentContainsOnlyNotNull

      public static <T> void checkArgumentContainsOnlyNotNull(Collection<T> collection)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are null.

      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
    • checkArgumentContainsOnlyNotNull

      public static <T> void checkArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessage)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are null.

      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
      errorMessage - the error message for the exception
    • checkArgumentContainsOnlyNotNull

      public static <T> void checkArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are null.

      Type Parameters:
      T - the type of object in the collection
      Parameters:
      collection - a collection, possibly null
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentContainsOnlyNotBlank

      public static void checkArgumentContainsOnlyNotBlank(Collection<String> collection)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are blank strings.

      Parameters:
      collection - a collection, possibly null
      Implementation Note:
      uses StringUtils.isBlank(CharSequence) to check for blank elements
    • checkArgumentContainsOnlyNotBlank

      public static void checkArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessage)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are blank strings.

      Parameters:
      collection - a collection, possibly null
      errorMessage - the error message for the exception
      Implementation Note:
      uses StringUtils.isBlank(CharSequence) to check for blank elements
    • checkArgumentContainsOnlyNotBlank

      public static void checkArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.

      Throws an IllegalArgumentException if the collection is null or empty, or if any elements are blank strings.

      Parameters:
      collection - a collection, possibly null
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
      Implementation Note:
      uses StringUtils.isBlank(CharSequence) to check for blank elements
    • checkArgumentNotEmpty

      public static <K, V> void checkArgumentNotEmpty(Map<K,V> map)
      Ensures that the map passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the map is null or empty.
      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      map - a map, possibly null
    • checkArgumentNotEmpty

      public static <K, V> void checkArgumentNotEmpty(Map<K,V> map, String errorMessage)
      Ensures that the map passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the map is null or empty.
      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      map - a map, possibly null
      errorMessage - the error message for the exception
    • checkArgumentNotEmpty

      public static <K, V> void checkArgumentNotEmpty(Map<K,V> map, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the map passed as a parameter to the calling method is not null or empty. Throws an IllegalArgumentException if the map is null or empty.
      Type Parameters:
      K - the type of keys in the map
      V - the type of values in the map
      Parameters:
      map - a map, possibly null
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkEvenItemCount

      @SafeVarargs public static <T> void checkEvenItemCount(T... items)
      Ensures that a collection of items has an even count, throwing an IllegalArgumentException if items is null or there is an odd number of items.
      Type Parameters:
      T - the object type
      Parameters:
      items - items to count
    • checkEvenItemCount

      public static <T> void checkEvenItemCount(Collection<T> items)
      Ensures that a collection of items has an even count, throwing an IllegalArgumentException if items is null or there is an odd number of items.
      Type Parameters:
      T - the object type
      Parameters:
      items - items to count
    • checkEvenItemCount

      public static void checkEvenItemCount(IntSupplier countSupplier)
      Ensures that a collection of items has an even count, throwing an IllegalArgumentException if countSupplier is null or returns an odd number.
      Parameters:
      countSupplier - an IntSupplier that returns the count to evaluate
    • requireNotNullElse

      public static <T> T requireNotNullElse(T obj, T defaultObj)
      Returns the first argument if it is not null, otherwise the second argument (which must not be null).

      The main reason for this method instead of the Objects.requireNonNullElse(Object, Object) is that this method throws an IllegalArgumentException instead of a NullPointerException, which was an unfortunate choice by the JDK.

      Type Parameters:
      T - the type of the reference
      Parameters:
      obj - an object, possibly null
      defaultObj - a non-null object
      Returns:
      the first non-null argument
    • requireNotNullElseGet

      public static <T> T requireNotNullElseGet(T obj, Supplier<? extends T> supplier)
      Returns the first argument if it is not null, otherwise the value supplied by the Supplier, which must not be null.

      The main reason for this method instead of the Objects.requireNonNullElse(Object, Object) is that this method throws an IllegalArgumentException instead of a NullPointerException, which was an unfortunate choice by the JDK.

      Type Parameters:
      T - the type of the reference
      Parameters:
      obj - an object, possibly null
      supplier - creates a non-null object
      Returns:
      the first argument, or the value from supplier
    • checkPositive

      public static void checkPositive(int value)
      Ensures int value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositive

      public static void checkPositive(int value, String errorMessage)
      Ensures int value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositive

      public static void checkPositive(int value, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures int value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositive

      public static void checkPositive(long value)
      Ensures long value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositive

      public static void checkPositive(long value, String errorMessage)
      Ensures long value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositive

      public static void checkPositive(long value, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures long value is a positive number (greater than zero).
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(int value)
      Ensures int value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      Throws:
      IllegalStateException - if the value is not positive or zero
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(int value, String errorMessage)
      Ensures int value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures int value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not zero or positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(long value)
      Ensures long value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      Throws:
      IllegalStateException - if the value is not positive or zero
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(long value, String errorMessage)
      Ensures long value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkPositiveOrZero

      public static void checkPositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures long value is a positive number (greater than zero) or zero.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not zero or positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositive

      public static int requirePositive(int value)
      Returns the int value if it is positive, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive
    • requirePositive

      public static int requirePositive(int value, String errorMessage)
      Returns the int value if it is a positive number (greater than zero), throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositive

      public static int requirePositive(int value, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the int value if it is a positive number (greater than zero), throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositive

      public static long requirePositive(long value)
      Returns the long value if it is positive, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive
    • requirePositive

      public static long requirePositive(long value, String errorMessage)
      Returns the long value if it is a positive number (greater than zero), throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositive

      public static long requirePositive(long value, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the long value if it is a positive number (greater than zero), throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given value if positive
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositiveOrZero

      public static int requirePositiveOrZero(int value)
      Returns the int value if it is positive or zero, throwing an IllegalStateException if not positive or zero.
      Parameters:
      value - the value to check for positivity or zero
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive zero
    • requirePositiveOrZero

      public static int requirePositiveOrZero(int value, String errorMessage)
      Returns the int value if it is a positive number (greater than zero) or zero, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositiveOrZero

      public static int requirePositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the int value if it is a positive number (greater than zero) or zero, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not zero or positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositiveOrZero

      public static long requirePositiveOrZero(long value)
      Returns the long value if it is positive or zero, throwing an IllegalStateException if not positive or zero.
      Parameters:
      value - the value to check for positivity or zero
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive zero
    • requirePositiveOrZero

      public static long requirePositiveOrZero(long value, String errorMessage)
      Returns the long value if it is a positive number (greater than zero) or zero, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessage - the error message to put in the exception if not positive
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • requirePositiveOrZero

      public static long requirePositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the long value if it is a positive number (greater than zero) or zero, throwing an IllegalStateException if not positive.
      Parameters:
      value - the value to check for positivity
      errorMessageTemplate - a template for the exception message if value is not zero or positive, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given value if positive or zero
      Throws:
      IllegalStateException - if the value is not positive (e.g. greater than zero)
      See Also:
      • Preconditions.checkState(boolean, Object)
    • checkValidPort

      public static void checkValidPort(int port)
      Ensures given port is valid, between 0 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      Throws:
      IllegalStateException - if port is not valid
    • checkValidPort

      public static void checkValidPort(int port, String errorMessage)
      Ensures given port is valid, between 0 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      errorMessage - the error message to put in the exception if the port is not valid
      Throws:
      IllegalStateException - if port is not valid
    • checkValidPort

      public static void checkValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures given port is valid, between 0 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      errorMessageTemplate - a template for the exception message if port is not valid, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Throws:
      IllegalStateException - if port is not valid
    • requireValidPort

      public static int requireValidPort(int port)
      Returns the given port if it is valid
      Parameters:
      port - the port to check for validity
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • requireValidPort

      public static int requireValidPort(int port, String errorMessage)
      Returns the given port if it is valid
      Parameters:
      port - the port to check for validity
      errorMessage - the error message to put in the exception if the port is not valid
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • requireValidPort

      public static int requireValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the given port if it is valid
      Parameters:
      port - the port to check for validity
      errorMessageTemplate - a template for the exception message if port is not valid, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • checkValidNonZeroPort

      public static void checkValidNonZeroPort(int port)
      Ensures given port is valid (excluding zero), between 1 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      Throws:
      IllegalStateException - if port is not valid
    • checkValidNonZeroPort

      public static void checkValidNonZeroPort(int port, String errorMessage)
      Ensures given port is valid (excluding zero), between 1 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      errorMessage - the error message to put in the exception if the port is not valid
      Throws:
      IllegalStateException - if port is not valid
    • checkValidNonZeroPort

      public static void checkValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures given port is valid (excluding zero), between 1 and MAX_PORT_NUMBER.
      Parameters:
      port - the port to check for validity
      errorMessageTemplate - a template for the exception message if port is not valid, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Throws:
      IllegalStateException - if port is not valid
    • requireValidNonZeroPort

      public static int requireValidNonZeroPort(int port)
      Returns the given port if it is valid (excluding zero)
      Parameters:
      port - the port to check for validity
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • requireValidNonZeroPort

      public static int requireValidNonZeroPort(int port, String errorMessage)
      Returns the given port if it is valid (excluding zero)
      Parameters:
      port - the port to check for validity
      errorMessage - the error message to put in the exception if the port is not valid
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • requireValidNonZeroPort

      public static int requireValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs)
      Returns the given port if it is valid (excluding zero)
      Parameters:
      port - the port to check for validity
      errorMessageTemplate - a template for the exception message if port is not valid, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
      Returns:
      the given port if valid
      Throws:
      IllegalStateException - if port is not valid
    • checkArgumentInstanceOf

      public static <T> void checkArgumentInstanceOf(T argument, Class<?> requiredType)
      Ensures the argument has the expected type.
      Type Parameters:
      T - the class of the required type
      Parameters:
      argument - the argument to check
      requiredType - the type that the argument is required to be
    • checkArgumentInstanceOf

      public static <T> void checkArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessage)
      Ensures the argument has the expected type.
      Type Parameters:
      T - the class of the required type
      Parameters:
      argument - the argument to check
      requiredType - the type that the argument is required to be
      errorMessage - the error message to put in the exception if the argument is not the required type
    • checkArgumentInstanceOf

      public static <T> void checkArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures the argument has the expected type.
      Type Parameters:
      T - the class of the required type
      Parameters:
      argument - the argument to check
      requiredType - the type that the argument is required to be
      errorMessageTemplate - the error message template to use in the exception if the argument is not the required type, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template
    • checkArgumentNotInstanceOf

      public static <T> void checkArgumentNotInstanceOf(T argument, Class<?> restrictedType)
      Ensures the argument type is not the restricted type.
      Type Parameters:
      T - the class of the restricted type
      Parameters:
      argument - the argument to check
      restrictedType - the type that the argument must not be
    • checkArgumentNotInstanceOf

      public static <T> void checkArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessage)
      Ensures the argument type is not the restricted type.
      Type Parameters:
      T - the class of the restricted type
      Parameters:
      argument - the argument to check
      restrictedType - the type that the argument must not be
      errorMessage - the error message to put in the exception if the argument is of the restricted type
    • checkArgumentNotInstanceOf

      public static <T> void checkArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures the argument type is not the restricted type.
      Type Parameters:
      T - the class of the restricted type
      Parameters:
      argument - the argument to check
      restrictedType - the type that the argument must not be
      errorMessageTemplate - the error message to use in the exception if the argument is of the restricted type, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to populate into the error message template