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
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte
requireNonNegative
(byte obj) Checks that the specified number is non-negative.static byte
requireNonNegative
(byte obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if it is not.static int
requireNonNegative
(int obj) Checks that the specified number is non-negative.static int
requireNonNegative
(int obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if it is not.static long
requireNonNegative
(long obj) Checks that the specified number is non-negative.static long
requireNonNegative
(long obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if it is not.static short
requireNonNegative
(short obj) Checks that the specified number is non-negative.static short
requireNonNegative
(short obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if it is not.static byte
requirePositive
(byte obj) Checks that the specified number is positive.static byte
requirePositive
(byte obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentException
if it is not.static int
requirePositive
(int obj) Checks that the specified number is positive.static int
requirePositive
(int obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentException
if it is not.static long
requirePositive
(long obj) Checks that the specified number is positive.static long
requirePositive
(long obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentException
if it is not.static short
requirePositive
(short obj) Checks that the specified number is positive.static short
requirePositive
(short obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentException
if 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requireNonNegative
Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if positive- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if positive- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if non-negative- Throws:
IllegalArgumentException
- ifobj
is negative
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if positive- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if positive- Throws:
IllegalArgumentException
- ifobj
is not positive
-
requirePositive
Checks that the specified number is positive and throws a customizedIllegalArgumentException
if 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 aIllegalArgumentException
is thrown- Returns:
obj
if positive- Throws:
IllegalArgumentException
- ifobj
is not positive
-