Package org.bson.assertions
Class Assertions
java.lang.Object
org.bson.assertions.Assertions
Design by contract assertions.
This class is not part of the public API and may be removed or changed at any time.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
assertNotNull
(T value) 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 AssertionError
fail()
static AssertionError
static void
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
Throw IllegalArgumentException if the value is null.
-
Method Details
-
notNull
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
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
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
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
-
fail
- Returns:
- Never completes normally. The return type is
AssertionError
to allow writingthrow fail()
. This may be helpful in non-void
methods. - Throws:
AssertionError
- Always
-
fail
- Parameters:
msg
- The failure message.- Returns:
- Never completes normally. The return type is
AssertionError
to allow writingthrow fail("failure message")
. This may be helpful in non-void
methods. - Throws:
AssertionError
- Always
-
assertNotNull
- Type Parameters:
T
- The type ofvalue
.- Parameters:
value
- A value to check.- Returns:
value
- Throws:
AssertionError
- Ifvalue
isnull
.
-
convertToType
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
-