Class Preconditions

java.lang.Object
org.apache.hadoop.util.Preconditions

@Private @Unstable public final class Preconditions extends Object

This class replaces guava.Preconditions which provides helpers to validate the following conditions:

  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    checkArgument(boolean expression)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static void
    checkArgument(boolean expression, Object errorMessage)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static void
    checkArgument(boolean expression, String errorMsg, Object... errorMsgArgs)
    Ensures the truth of an expression involving one or more parameters to the calling method.
    static void
    checkArgument(boolean expression, Supplier<String> msgSupplier)
    Preconditions that the expression involving one or more parameters to the calling method.
    static <T> T
    checkNotNull(T obj)
    Preconditions that the specified argument is not null, throwing a NPE exception otherwise.
    static <T> T
    checkNotNull(T obj, Object errorMessage)
    Preconditions that the specified argument is not null, throwing a NPE exception otherwise.
    static <T> T
    checkNotNull(T obj, String message, Object... values)
    Preconditions that the specified argument is not null, throwing a NPE exception otherwise.
    static <T> T
    checkNotNull(T obj, Supplier<String> msgSupplier)
    Preconditions that the specified argument is not null, throwing a NPE exception otherwise.
    static void
    checkState(boolean expression)
    Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.
    static void
    checkState(boolean expression, Object errorMessage)
    Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.
    static void
    checkState(boolean expression, String errorMsg, Object... errorMsgArgs)
    Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.
    static void
    checkState(boolean expression, Supplier<String> msgSupplier)
    Preconditions that the expression involving one or more parameters to the calling method.

    Methods inherited from class java.lang.Object

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

    • checkNotNull

      public static <T> T checkNotNull(T obj)

      Preconditions that the specified argument is not null, throwing a NPE exception otherwise.

      The message of the exception is "The validated object is null".

      Type Parameters:
      T - the object type
      Parameters:
      obj - the object to check
      Returns:
      the validated object
      Throws:
      NullPointerException - if the object is null
      See Also:
    • checkNotNull

      public static <T> T checkNotNull(T obj, Object errorMessage)

      Preconditions that the specified argument is not null, throwing a NPE exception otherwise.

      The message of the exception is errorMessage.

      Type Parameters:
      T - the object type
      Parameters:
      obj - the object to check
      errorMessage - the message associated with the NPE
      Returns:
      the validated object
      Throws:
      NullPointerException - if the object is null
      See Also:
    • checkNotNull

      public static <T> T checkNotNull(T obj, String message, Object... values)

      Preconditions that the specified argument is not null, throwing a NPE exception otherwise.

      The message of the exception is String.format(f, m).

      Type Parameters:
      T - the object type
      Parameters:
      obj - the object to check
      message - the String.format(String, Object...) exception message if valid. Otherwise, the message is VALIDATE_IS_NOT_NULL_EX_MESSAGE
      values - the optional values for the formatted exception message
      Returns:
      the validated object
      Throws:
      NullPointerException - if the object is null
      See Also:
    • checkNotNull

      public static <T> T checkNotNull(T obj, Supplier<String> msgSupplier)
      Preconditions that the specified argument is not null, throwing a NPE exception otherwise.

      The message of the exception is msgSupplier.get().

      Type Parameters:
      T - the object type
      Parameters:
      obj - the object to check
      msgSupplier - the Supplier.get() set the exception message if valid. Otherwise, the message is VALIDATE_IS_NOT_NULL_EX_MESSAGE
      Returns:
      the validated object (never null for method chaining)
      Throws:
      NullPointerException - if the object is null
    • checkArgument

      public static void checkArgument(boolean expression)
      Ensures the truth of an expression involving one or more parameters to the calling method.
      Parameters:
      expression - a boolean expression
      Throws:
      IllegalArgumentException - if expression is false
    • checkArgument

      public static void checkArgument(boolean expression, Object errorMessage)
      Ensures the truth of an expression involving one or more parameters to the calling method.
      Parameters:
      expression - a boolean expression
      errorMessage - the exception message to use if the check fails; will be converted to a string using String.valueOf(Object)
      Throws:
      IllegalArgumentException - if expression is false
    • checkArgument

      public static void checkArgument(boolean expression, String errorMsg, Object... errorMsgArgs)
      Ensures the truth of an expression involving one or more parameters to the calling method.

      The message of the exception is String.format(f, m).

      Parameters:
      expression - a boolean expression
      errorMsg - the String.format(String, Object...) exception message if valid. Otherwise, the message is CHECK_ARGUMENT_EX_MESSAGE
      errorMsgArgs - the optional values for the formatted exception message.
      Throws:
      IllegalArgumentException - if expression is false
    • checkArgument

      public static void checkArgument(boolean expression, Supplier<String> msgSupplier)
      Preconditions that the expression involving one or more parameters to the calling method.

      The message of the exception is msgSupplier.get().

      Parameters:
      expression - a boolean expression
      msgSupplier - the Supplier.get() set the exception message if valid. Otherwise, the message is CHECK_ARGUMENT_EX_MESSAGE
      Throws:
      IllegalArgumentException - if expression is false
    • checkState

      public static void checkState(boolean expression)
      Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.
      Parameters:
      expression - a boolean expression
      Throws:
      IllegalStateException - if expression is false
    • checkState

      public static void checkState(boolean expression, Object errorMessage)
      Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.
      Parameters:
      expression - a boolean expression
      errorMessage - the exception message to use if the check fails; will be converted to a string using String.valueOf(Object)
      Throws:
      IllegalStateException - if expression is false
    • checkState

      public static void checkState(boolean expression, String errorMsg, Object... errorMsgArgs)
      Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.

      The message of the exception is String.format(f, m).

      Parameters:
      expression - a boolean expression
      errorMsg - the String.format(String, Object...) exception message if valid. Otherwise, the message is CHECK_STATE_EX_MESSAGE
      errorMsgArgs - the optional values for the formatted exception message.
      Throws:
      IllegalStateException - if expression is false
    • checkState

      public static void checkState(boolean expression, Supplier<String> msgSupplier)
      Preconditions that the expression involving one or more parameters to the calling method.

      The message of the exception is msgSupplier.get().

      Parameters:
      expression - a boolean expression
      msgSupplier - the Supplier.get() set the exception message if valid. Otherwise, the message is CHECK_STATE_EX_MESSAGE
      Throws:
      IllegalStateException - if expression is false