Class Reject

java.lang.Object
org.forgerock.util.Reject

public final class Reject extends Object
A input parameter-validating utility class using fluent invocation:
 public int divide(int dividend, int divisor) {
     Reject.ifTrue(divisor == 0, "Division by zero not supported");
     return dividend / divisor;
 }
 
The example above will cause an IllegalArgumentException to be thrown with the message given.

Note that the methods herein throw generic RuntimeExceptions as opposed to custom, application-specific error Exceptions. This class is intended for wide use among multiple projects whose Exception frameworks may differ. The implementer is encouraged to catch the generic exceptions thrown by this class and rethrow exceptions appropriate to the target application.

  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
    static String
    checkNotBlank(String str, String message)
    Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
    static <T> T
    checkNotNull(T object)
    Deprecated.
    static <T> T
    checkNotNull(T object, String message)
    static void
    Alias for checkNotBlank(String) to be used in fluent Reject.ifBlank syntax.
    static void
    ifBlank(String str, String message)
    Alias for checkNotBlank(String, String) to be used in fluent Reject.ifBlank syntax.
    static void
    ifFalse(boolean condition)
    Deprecated.
    Experience has shown that Reject.ifFalse can be hard to read.
    static void
    ifFalse(boolean condition, String message)
    Deprecated.
    Experience has shown that Reject.ifFalse can be hard to read.
    static void
    ifNull(Object object)
    Alias for checkNotNull to be used in fluent Reject.ifNull syntax.
    static void
    ifNull(Object object, String message)
    Alias for checkNotNull to be used in fluent Reject.ifNull syntax.
    static <T> void
    ifNull(T... objects)
    Throws a NullPointerException if any of the provided arguments are null.
    static void
    ifTrue(boolean condition)
    Throws an IllegalArgumentException if the condition parameter is true.
    static void
    ifTrue(boolean condition, String message)
    Throws an IllegalArgumentException with a custom message if the condition parameter is true.
    static void
    rejectStateIfTrue(boolean condition, String message)
    Throws an IllegalStateException with a custom message if the condition parameter is true.
    static void
    unless(boolean condition)
    Throws an IllegalArgumentException if the condition parameter is false.
    static void
    unless(boolean condition, String message)
    Throws an IllegalArgumentException with a custom message if the condition parameter is false.

    Methods inherited from class java.lang.Object

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

    • checkNotNull

      @Deprecated(since="26.5.0") public static <T> T checkNotNull(T object)
      Deprecated.
      Throws a NullPointerException if the object parameter is null, returns the object otherwise.
      Type Parameters:
      T - The type of object to test.
      Parameters:
      object - the object to test
      Returns:
      the object
      Throws:
      NullPointerException - if object is null
    • checkNotNull

      @Deprecated(since="26.5.0") public static <T> T checkNotNull(T object, String message)
      Throws a NullPointerException if the object parameter is null, returns the object otherwise.
      Type Parameters:
      T - The type of object to test.
      Parameters:
      object - the object to test
      message - a custom exception message to use
      Returns:
      the object
      Throws:
      NullPointerException - if object is null
    • checkNotBlank

      public static String checkNotBlank(String str)
      Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
      Parameters:
      str - the string to check
      Returns:
      the string
      Throws:
      NullPointerException - if str is null
      IllegalArgumentException - if str is empty or only contains whitespace
    • checkNotBlank

      public static String checkNotBlank(String str, String message)
      Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
      Parameters:
      str - the string to check
      message - a custom exception message to use
      Returns:
      the string
      Throws:
      NullPointerException - if str is null
      IllegalArgumentException - if str is empty or only contains whitespace
    • ifBlank

      public static void ifBlank(String str)
      Alias for checkNotBlank(String) to be used in fluent Reject.ifBlank syntax. Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
      Parameters:
      str - the string to check
      Throws:
      NullPointerException - if str is null
      IllegalArgumentException - if str is empty or only contains whitespace
    • ifBlank

      public static void ifBlank(String str, String message)
      Alias for checkNotBlank(String, String) to be used in fluent Reject.ifBlank syntax. Throws a NullPointerException if the str parameter is null, throws IllegalArgumentException if empty or only contains whitespace, and returns the string otherwise.
      Parameters:
      str - the string to check
      message - a custom exception message to use
      Throws:
      NullPointerException - if str is null
      IllegalArgumentException - if str is empty or only contains whitespace
    • ifFalse

      @Deprecated public static void ifFalse(boolean condition)
      Deprecated.
      Experience has shown that Reject.ifFalse can be hard to read. Prefer to use unless(boolean) (which works identically) or rewrite to use ifTrue(boolean) instead.
      Throws an IllegalArgumentException if the condition parameter is false.
      Parameters:
      condition - the condition to test
      Throws:
      IllegalArgumentException - if condition is false
    • ifFalse

      @Deprecated public static void ifFalse(boolean condition, String message)
      Deprecated.
      Experience has shown that Reject.ifFalse can be hard to read. Prefer to use unless(boolean, String) (which works identically) or rewrite to use ifTrue(boolean, String) instead.
      Throws an IllegalArgumentException with a custom message if the condition parameter is false.
      Parameters:
      condition - the condition to test
      message - a custom exception message to use
      Throws:
      IllegalArgumentException - if condition is false
    • unless

      public static void unless(boolean condition)
      Throws an IllegalArgumentException if the condition parameter is false.
      Parameters:
      condition - the condition to test
      Throws:
      IllegalArgumentException - if condition is false
    • unless

      public static void unless(boolean condition, String message)
      Throws an IllegalArgumentException with a custom message if the condition parameter is false.
      Parameters:
      condition - the condition to test
      message - a custom exception message to use
      Throws:
      IllegalArgumentException - if condition is false
    • ifNull

      public static void ifNull(Object object)
      Alias for checkNotNull to be used in fluent Reject.ifNull syntax. Throws a NullPointerException if the object parameter is null.
      Parameters:
      object - the object to test
      Throws:
      NullPointerException - if object is null
    • ifNull

      @SafeVarargs public static <T> void ifNull(T... objects)
      Throws a NullPointerException if any of the provided arguments are null.
      Type Parameters:
      T - The type of object to test.
      Parameters:
      objects - The objects to test.
      Throws:
      NullPointerException - If any of the provided arguments are null.
    • ifNull

      public static void ifNull(Object object, String message)
      Alias for checkNotNull to be used in fluent Reject.ifNull syntax. Throws a NullPointerException if the object parameter is null.
      Parameters:
      object - the object to test
      message - a custom exception message to use
      Throws:
      NullPointerException - if object is null
    • ifTrue

      public static void ifTrue(boolean condition)
      Throws an IllegalArgumentException if the condition parameter is true.
      Parameters:
      condition - the condition to test
      Throws:
      IllegalArgumentException - if condition is true
    • ifTrue

      public static void ifTrue(boolean condition, String message)
      Throws an IllegalArgumentException with a custom message if the condition parameter is true.
      Parameters:
      condition - the condition to test
      message - a custom exception message to use
      Throws:
      IllegalArgumentException - if condition is true
    • rejectStateIfTrue

      public static void rejectStateIfTrue(boolean condition, String message)
      Throws an IllegalStateException with a custom message if the condition parameter is true.
      Parameters:
      condition - the condition to test
      message - a custom exception message to use
      Throws:
      IllegalStateException - if condition is true