Class Preconditions
- java.lang.Object
-
- de.rwth.swc.coffee4j.algorithmic.util.Preconditions
-
public final class Preconditions extends Object
Preconditions which can be used to validate the argument passed to methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
check(boolean expression)
Checks whether the given expression evaluates totrue
.static void
check(boolean expression, String message)
Checks whether the given expression evaluates totrue
.static void
check(boolean expression, Supplier<String> messageSupplier)
Checks whether the given expression evaluates totrue
.static <T> Collection<T>
doesNotContainNull(Collection<T> collection)
Checks if the givenCollection
contains anynull
element.static <T> Collection<T>
doesNotContainNull(Collection<T> collection, String message)
Checks if the givenCollection
contains anynull
element.static <T> Collection<T>
doesNotContainNull(Collection<T> collection, Supplier<String> messageSupplier)
Checks if the givenCollection
contains anynull
element.static <T> T
notNull(T object)
Checks if the given object isnull
.static <T> T
notNull(T object, String message)
Checks if the given object ifnull
.static <T> T
notNull(T object, Supplier<String> messageSupplier)
Checks if the given object isnull
.
-
-
-
Method Detail
-
notNull
public static <T> T notNull(T object)
Checks if the given object isnull
. If this is the case, aNullPointerException
is thrown.- Type Parameters:
T
- the type of the object. This is needed to return the correct type again- Parameters:
object
- the object to be checked- Returns:
- the object which was passed if it is not
null
- Throws:
NullPointerException
- if the passed object isnull
-
notNull
public static <T> T notNull(T object, String message)
Checks if the given object ifnull
. If this is the case, aNullPointerException
with the given message is thrown.- Type Parameters:
T
- the type of the object. This is needed to returned the correct type again- Parameters:
object
- the object to be checkedmessage
- the message for the exception if the object isnull
- Returns:
- the object which was passed if it is not
null
- Throws:
NullPointerException
- if the passed object isnull
-
notNull
public static <T> T notNull(T object, Supplier<String> messageSupplier)
Checks if the given object isnull
. If this is the case, aNullPointerException
with the message provided by theSupplier
is thrown.- Type Parameters:
T
- the type of the object. This is needed to returned the correct * type again- Parameters:
object
- the object to be checkedmessageSupplier
- a supplier for the message of the exception which will be thrown if object isnull
- Returns:
- the object which was passed if it is not
null
- Throws:
NullPointerException
- if the passed object isnull
-
doesNotContainNull
public static <T> Collection<T> doesNotContainNull(Collection<T> collection)
Checks if the givenCollection
contains anynull
element. If this is the case, aIllegalArgumentException
is thrown.- Type Parameters:
T
- the type of the collection- Parameters:
collection
- to be examined for anull
element. Must not benull
- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException
- if collection containsnull
NullPointerException
- if the collection it wasnull
itself. Note that someCollection
implementations may throw aNullPointerException
during the check if they do not permit thenull
type
-
doesNotContainNull
public static <T> Collection<T> doesNotContainNull(Collection<T> collection, String message)
Checks if the givenCollection
contains anynull
element. If this is the case, aIllegalArgumentException
with the given message is thrown.- Type Parameters:
T
- the type of the collection- Parameters:
collection
- to be examined for anull
element. Must not benull
message
- for the exception if it is thrown- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException
- if collection containsnull
NullPointerException
- if the collection it wasnull
itself. Note that someCollection
implementations may throw aNullPointerException
during the check if they do not permit thenull
type
-
doesNotContainNull
public static <T> Collection<T> doesNotContainNull(Collection<T> collection, Supplier<String> messageSupplier)
Checks if the givenCollection
contains anynull
element. If this is the case, aIllegalArgumentException
with the message given by the supplier.- Type Parameters:
T
- the type of the collection- Parameters:
collection
- to be examined for anull
element. Must not benull
messageSupplier
- for the exception message if it is thrown- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException
- if collection containsnull
NullPointerException
- if the collection it wasnull
itself. Note that someCollection
implementations may throw aNullPointerException
during the check if they do not permit thenull
type
-
check
public static void check(boolean expression)
Checks whether the given expression evaluates totrue
. If this is not the case, anIllegalArgumentException
is thrown.- Parameters:
expression
- the expression to be evaluated- Throws:
IllegalArgumentException
- if the expression evaluates tofalse
-
check
public static void check(boolean expression, String message)
Checks whether the given expression evaluates totrue
. If this is not the case, anIllegalArgumentException
with the given message is thrown.- Parameters:
expression
- the expression to be evaluatedmessage
- the message for the exception- Throws:
IllegalArgumentException
- if the expression evaluates tofalse
-
check
public static void check(boolean expression, Supplier<String> messageSupplier)
Checks whether the given expression evaluates totrue
. If this is not the case, anIllegalArgumentException
with the message given by theSupplier
is thrown.- Parameters:
expression
- the expression to be evaluatedmessageSupplier
- the supplier for the exception message- Throws:
IllegalArgumentException
- if the expression evaluates tofalse
-
-