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.

Another use case is validating constructor parameters:

 public TokenManager(final TokenFactory factory) {
     Reject.ifNull(factory, "Cannot instantiate TokenManager with null TokenFactory");
 }
 
Sometimes, constructor parameters are passed to ancestor constructors which must be called first--thus, the checkNotNull syntax is available:
     import static org.forgerock.util.Reject.checkNotNull;

     public TokenManager(final TokenFactory factory) {
         super(checkNotNull(factory));
     }
 
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 <T> T
    checkNotNull(T object)
    Throws a NullPointerException if the object parameter is null, returns the object otherwise.
    static <T> T
    checkNotNull(T object, String message)
    Throws a NullPointerException if the object parameter is null, returns the object otherwise.
    static void
    ifFalse(boolean condition)
    Throws an IllegalArgumentException if the condition parameter is false.
    static void
    ifFalse(boolean condition, String message)
    Throws an IllegalArgumentException with a custom message if the condition parameter is false.
    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.

    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 object)
      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

      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
    • ifFalse

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

      public static void ifFalse(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