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 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 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 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 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_POWER_OF_TWO
- 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
-
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
-