Package org.bson.assertions
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.static void
isTrue(String name, boolean condition)
Throw IllegalStateException if the condition if false.static void
isTrueArgument(String name, boolean condition)
Throw IllegalArgumentException if the condition if false.static <T> T
isTrueArgument(String name, T value, boolean condition)
Throw IllegalArgumentException if the condition if false, otherwise return the value.static <T> T
notNull(String name, T value)
Throw IllegalArgumentException if the value is null.
-
-
-
Method Detail
-
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 namevalue
- 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 checkedcondition
- 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 checkedcondition
- 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 usingthis
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 checkedvalue
- the value of the argumentcondition
- the condition about the parameter to check- Returns:
- the value
- Throws:
IllegalArgumentException
- if the condition is false
-
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 tovalue
- the value to casterrorMessage
- the error message to include in the exception- Returns:
- value cast to clazz
- Throws:
IllegalArgumentException
- if value is not assignable to clazz
-
-