Package org.drasyl.util
Class Preconditions
java.lang.Object
org.drasyl.util.Preconditions
Static convenience methods that help a method or constructor check whether it was invoked
correctly (that is, whether its preconditions were met).
If the precondition is not met, the Preconditions method throws an unchecked exception of
a specified type, which helps the method in which the exception was thrown communicate that its
caller has made a mistake.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byterequireInRange(byte obj, byte min, byte max) Checks that the specified number is in the given range [min,max].static byterequireInRange(byte obj, byte min, byte max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static doublerequireInRange(double obj, double min, double max) Checks that the specified number is in the given range [min,max].static doublerequireInRange(double obj, double min, double max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static floatrequireInRange(float obj, float min, float max) Checks that the specified number is in the given range [min,max].static floatrequireInRange(float obj, float min, float max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static intrequireInRange(int obj, int min, int max) Checks that the specified number is in the given range [min,max].static intrequireInRange(int obj, int min, int max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static longrequireInRange(long obj, long min, long max) Checks that the specified number is in the given range [min,max].static longrequireInRange(long obj, long min, long max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static shortrequireInRange(short obj, short min, short max) Checks that the specified number is in the given range [min,max].static shortrequireInRange(short obj, short min, short max, String message) Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not.static byterequireNonNegative(byte obj) Checks that the specified number is non-negative.static byterequireNonNegative(byte obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static doublerequireNonNegative(double obj) Checks that the specified number is non-negative.static doublerequireNonNegative(double obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static floatrequireNonNegative(float obj) Checks that the specified number is non-negative.static floatrequireNonNegative(float obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static intrequireNonNegative(int obj) Checks that the specified number is non-negative.static intrequireNonNegative(int obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static longrequireNonNegative(long obj) Checks that the specified number is non-negative.static longrequireNonNegative(long obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static shortrequireNonNegative(short obj) Checks that the specified number is non-negative.static shortrequireNonNegative(short obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static DurationChecks that the specified number is non-negative.static DurationrequireNonNegative(Duration obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not.static byterequirePositive(byte obj) Checks that the specified number is positive.static byterequirePositive(byte obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.static doublerequirePositive(double obj) Checks that the specified number is positive.static doublerequirePositive(double obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.static floatrequirePositive(float obj) Checks that the specified number is positive.static floatrequirePositive(float obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.static intrequirePositive(int obj) Checks that the specified number is positive.static intrequirePositive(int obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.static longrequirePositive(long obj) Checks that the specified number is positive.static longrequirePositive(long obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.static shortrequirePositive(short obj) Checks that the specified number is positive.static shortrequirePositive(short obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not.
-
Field Details
-
MUST_BE_NON_NEGATIVE
- See Also:
-
MUST_BE_POSITIVE
- See Also:
-
MUST_BE_IN_RANGE
- See Also:
-
-
Method Details
-
requireNonNegative
public static byte requireNonNegative(byte obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
public static int requireNonNegative(int obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
public static long requireNonNegative(long obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
public static short requireNonNegative(short obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
public static float requireNonNegative(float obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
public static double requireNonNegative(double obj) Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requireNonNegative
Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negativeNullPointerException- ifobjisnull
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative"); }- Parameters:
obj- the number to check for negativitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negativeNullPointerException- ifobjisnull
-
requirePositive
public static byte requirePositive(byte obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
public static int requirePositive(int obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
public static long requirePositive(long obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
public static short requirePositive(short obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for positivity- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
public static float requirePositive(float obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requirePositive
public static double requirePositive(double obj) Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar); }- Parameters:
obj- the number to check for negativity- Returns:
objif non-negative- Throws:
IllegalArgumentException- ifobjis negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requirePositive(bar, "bar must be positive"); }- Parameters:
obj- the number to check for positivitymessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif positive- Throws:
IllegalArgumentException- ifobjis not positive
-
requireInRange
public static byte requireInRange(byte obj, byte min, byte max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
public static int requireInRange(int obj, int min, int max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
public static long requireInRange(long obj, long min, long max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
public static short requireInRange(short obj, short min, short max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
public static float requireInRange(float obj, float min, float max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
public static double requireInRange(double obj, double min, double max) Checks that the specified number is in the given range [min,max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max); }- Parameters:
obj- the number to check for range- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-
requireInRange
Checks that the specified number is in the given range [min,max] and throws a customizedIllegalArgumentExceptionif it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:public Foo(int bar) { this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive"); }- Parameters:
obj- the number to check for rangemessage- detail message to be used in the event that aIllegalArgumentExceptionis thrown- Returns:
objif in range- Throws:
IllegalArgumentException- ifobjis not in range
-