Class Assertions

java.lang.Object
org.bson.assertions.Assertions

public final class Assertions extends Object

Design by contract assertions.

This class is not part of the public API and may be removed or changed at any time.

  • Method Details

    • notNull

      public static <T> T notNull(String name, T value)
      Throw IllegalArgumentException if the value is null.
      Type Parameters:
      T - the value type
      Parameters:
      name - the parameter name
      value - the value that should not be null
      Returns:
      the value
      Throws:
      IllegalArgumentException - if value is null
    • isTrue

      public static void isTrue(String name, boolean condition)
      Throw IllegalStateException if the condition if false.
      Parameters:
      name - the name of the state that is being checked
      condition - the condition about the parameter to check
      Throws:
      IllegalStateException - if the condition is false
    • isTrueArgument

      public static void isTrueArgument(String name, boolean condition)
      Throw IllegalArgumentException if the condition if false.
      Parameters:
      name - the name of the state that is being checked
      condition - the condition about the parameter to check
      Throws:
      IllegalArgumentException - if the condition is false
    • isTrueArgument

      public static <T> T isTrueArgument(String name, T value, boolean condition)
      Throw IllegalArgumentException if the condition if false, otherwise return the value. This is useful when arguments must be checked within an expression, as when using this to call another constructor, which must be the first line of the calling constructor.
      Type Parameters:
      T - the value type
      Parameters:
      name - the name of the state that is being checked
      value - the value of the argument
      condition - the condition about the parameter to check
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the condition is false
    • fail

      public static AssertionError fail() throws AssertionError
      Returns:
      Never completes normally. The return type is AssertionError to allow writing throw fail(). This may be helpful in non-void methods.
      Throws:
      AssertionError - Always
    • fail

      public static AssertionError fail(String msg) throws AssertionError
      Parameters:
      msg - The failure message.
      Returns:
      Never completes normally. The return type is AssertionError to allow writing throw fail("failure message"). This may be helpful in non-void methods.
      Throws:
      AssertionError - Always
    • assertNotNull

      public static <T> T assertNotNull(@Nullable T value) throws AssertionError
      Type Parameters:
      T - The type of value.
      Parameters:
      value - A value to check.
      Returns:
      value
      Throws:
      AssertionError - If value is null.
    • convertToType

      public static <T> T convertToType(Class<T> clazz, Object value, String errorMessage)
      Cast an object to the given class and return it, or throw IllegalArgumentException if it's not assignable to that class.
      Type Parameters:
      T - the Class type
      Parameters:
      clazz - the class to cast to
      value - the value to cast
      errorMessage - the error message to include in the exception
      Returns:
      value cast to clazz
      Throws:
      IllegalArgumentException - if value is not assignable to clazz