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 voidcheck(boolean expression)Checks whether the given expression evaluates totrue.static voidcheck(boolean expression, String message)Checks whether the given expression evaluates totrue.static voidcheck(boolean expression, Supplier<String> messageSupplier)Checks whether the given expression evaluates totrue.static <T> Collection<T>doesNotContainNull(Collection<T> collection)Checks if the givenCollectioncontains anynullelement.static <T> Collection<T>doesNotContainNull(Collection<T> collection, String message)Checks if the givenCollectioncontains anynullelement.static <T> Collection<T>doesNotContainNull(Collection<T> collection, Supplier<String> messageSupplier)Checks if the givenCollectioncontains anynullelement.static <T> TnotNull(T object)Checks if the given object isnull.static <T> TnotNull(T object, String message)Checks if the given object ifnull.static <T> TnotNull(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, aNullPointerExceptionis 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, aNullPointerExceptionwith 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, aNullPointerExceptionwith the message provided by theSupplieris 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 givenCollectioncontains anynullelement. If this is the case, aIllegalArgumentExceptionis thrown.- Type Parameters:
T- the type of the collection- Parameters:
collection- to be examined for anullelement. Must not benull- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException- if collection containsnullNullPointerException- if the collection it wasnullitself. Note that someCollectionimplementations may throw aNullPointerExceptionduring the check if they do not permit thenulltype
-
doesNotContainNull
public static <T> Collection<T> doesNotContainNull(Collection<T> collection, String message)
Checks if the givenCollectioncontains anynullelement. If this is the case, aIllegalArgumentExceptionwith the given message is thrown.- Type Parameters:
T- the type of the collection- Parameters:
collection- to be examined for anullelement. Must not benullmessage- for the exception if it is thrown- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException- if collection containsnullNullPointerException- if the collection it wasnullitself. Note that someCollectionimplementations may throw aNullPointerExceptionduring the check if they do not permit thenulltype
-
doesNotContainNull
public static <T> Collection<T> doesNotContainNull(Collection<T> collection, Supplier<String> messageSupplier)
Checks if the givenCollectioncontains anynullelement. If this is the case, aIllegalArgumentExceptionwith the message given by the supplier.- Type Parameters:
T- the type of the collection- Parameters:
collection- to be examined for anullelement. Must not benullmessageSupplier- for the exception message if it is thrown- Returns:
- the same collection which was initially given to this method
- Throws:
IllegalArgumentException- if collection containsnullNullPointerException- if the collection it wasnullitself. Note that someCollectionimplementations may throw aNullPointerExceptionduring the check if they do not permit thenulltype
-
check
public static void check(boolean expression)
Checks whether the given expression evaluates totrue. If this is not the case, anIllegalArgumentExceptionis 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, anIllegalArgumentExceptionwith 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, anIllegalArgumentExceptionwith the message given by theSupplieris thrown.- Parameters:
expression- the expression to be evaluatedmessageSupplier- the supplier for the exception message- Throws:
IllegalArgumentException- if the expression evaluates tofalse
-
-