Package com.mongodb.assertions
Class Assertions
- java.lang.Object
-
- com.mongodb.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.
Allassert...
methods throwAssertionError
and should be used to check conditions which may be violated if and only if the driver code is incorrect. The intended usage of this methods is the same as of the Javaassert
statement. The reason for not using theassert
statements is that they are not always enabled. We prefer having internal checks always done at the cost of our code doing a relatively small amount of additional work in production. Theassert...
methods return values to open possibilities of being used fluently.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
assertFalse(boolean value)
static <T> T
assertNotNull(T value)
static <T> T
assertNull(T value)
static boolean
assertTrue(boolean value)
static void
doesNotContainNull(String name, Collection<?> collection)
Throw IllegalArgumentException if the collection contains a null value.static AssertionError
fail()
static AssertionError
fail(String msg)
static void
isTrue(String name, boolean condition)
Throw IllegalStateException if the condition if false.static void
isTrue(String name, boolean condition, com.mongodb.internal.async.SingleResultCallback<?> callback)
Throw IllegalStateException if the condition if false.static void
isTrueArgument(String name, boolean condition)
Throw IllegalArgumentException if the condition if false.static <T> T
notNull(String name, T value)
Throw IllegalArgumentException if the value is null.static <T> T
notNull(String name, T value, com.mongodb.internal.async.SingleResultCallback<?> callback)
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
-
notNull
public static <T> T notNull(String name, T value, com.mongodb.internal.async.SingleResultCallback<?> callback)
Throw IllegalArgumentException if the value is null.- Type Parameters:
T
- the value type- Parameters:
name
- the parameter namevalue
- the value that should not be nullcallback
- the callback that also is passed the exception if the value is 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
-
isTrue
public static void isTrue(String name, boolean condition, com.mongodb.internal.async.SingleResultCallback<?> callback)
Throw IllegalStateException if the condition if false.- Parameters:
name
- the name of the state that is being checkedcondition
- the condition about the parameter to checkcallback
- the callback that also is passed the exception if the condition is not true- 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
-
doesNotContainNull
public static void doesNotContainNull(String name, Collection<?> collection)
Throw IllegalArgumentException if the collection contains a null value.- Parameters:
name
- the name of the collectioncollection
- the collection- Throws:
IllegalArgumentException
- if the collection contains a null value
-
assertNull
@Nullable public static <T> T assertNull(@Nullable T value) throws AssertionError
- Type Parameters:
T
- The type ofvalue
.- Parameters:
value
- A value to check.- Returns:
null
.- Throws:
AssertionError
- Ifvalue
is notnull
.
-
assertNotNull
public static <T> T assertNotNull(@Nullable T value) throws AssertionError
- Type Parameters:
T
- The type ofvalue
.- Parameters:
value
- A value to check.- Returns:
value
- Throws:
AssertionError
- Ifvalue
isnull
.
-
assertTrue
public static boolean assertTrue(boolean value) throws AssertionError
- Parameters:
value
- A value to check.- Returns:
true
.- Throws:
AssertionError
- Ifvalue
isfalse
.
-
assertFalse
public static boolean assertFalse(boolean value) throws AssertionError
- Parameters:
value
- A value to check.- Returns:
false
.- Throws:
AssertionError
- Ifvalue
istrue
.
-
fail
public static AssertionError fail() throws AssertionError
- 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
public static AssertionError fail(String msg) throws AssertionError
- 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
-
-