Class Preconditions

java.lang.Object
be.imgn.gaia.core.base.Preconditions

public final class Preconditions extends Object
Checks that a method is called correctly, and throws the matching exception if not.
Preconditions.checkArgument(count >= 0, "count must not be negative: %d", count);
Preconditions.checkState(!closed, "Already closed");

A message is only built if the check fails. A message format follows the syntax of String.format(Locale, String, Object...) and is formatted in Locale.ROOT, so that messages do not depend on the default locale. Use Objects.requireNonNull(Object, String) to check that a value is not null.

Since:
0.1.1
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    checkArgument(boolean expression)
    Checks an expression about the arguments of the calling method.
    static void
    checkArgument(boolean expression, @Nullable Object message)
    Checks an expression about the arguments of the calling method.
    static void
    checkArgument(boolean expression, String messageFormat, @Nullable Object... params)
    Checks an expression about the arguments of the calling method.
    static void
    checkState(boolean expression)
    Checks an expression about the state of the object the calling method is called on.
    static void
    checkState(boolean expression, @Nullable Object message)
    Checks an expression about the state of the object the calling method is called on.
    static void
    checkState(boolean expression, String messageFormat, @Nullable Object... params)
    Checks an expression about the state of the object the calling method is called on.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • checkArgument

      public static void checkArgument(boolean expression)
      Checks an expression about the arguments of the calling method.
      Parameters:
      expression - the expression to check
      Throws:
      IllegalArgumentException - if expression is false
    • checkArgument

      public static void checkArgument(boolean expression, @Nullable Object message)
      Checks an expression about the arguments of the calling method.
      Parameters:
      expression - the expression to check
      message - the message of the exception, converted with String.valueOf(Object)
      Throws:
      IllegalArgumentException - if expression is false
    • checkArgument

      @FormatMethod public static void checkArgument(boolean expression, @FormatString String messageFormat, @Nullable Object... params)
      Checks an expression about the arguments of the calling method.
      Parameters:
      expression - the expression to check
      messageFormat - the format of the message of the exception
      params - the values the format refers to
      Throws:
      IllegalArgumentException - if expression is false
      IllegalFormatException - if expression is false and messageFormat is not a valid format for params
    • checkState

      public static void checkState(boolean expression)
      Checks an expression about the state of the object the calling method is called on.
      Parameters:
      expression - the expression to check
      Throws:
      IllegalStateException - if expression is false
    • checkState

      public static void checkState(boolean expression, @Nullable Object message)
      Checks an expression about the state of the object the calling method is called on.
      Parameters:
      expression - the expression to check
      message - the message of the exception, converted with String.valueOf(Object)
      Throws:
      IllegalStateException - if expression is false
    • checkState

      @FormatMethod public static void checkState(boolean expression, @FormatString String messageFormat, @Nullable Object... params)
      Checks an expression about the state of the object the calling method is called on.
      Parameters:
      expression - the expression to check
      messageFormat - the format of the message of the exception
      params - the values the format refers to
      Throws:
      IllegalStateException - if expression is false
      IllegalFormatException - if expression is false and messageFormat is not a valid format for params