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
requireInRange
(byte obj, byte min, byte max) Checks that the specified number is in the given range [min
,max
].static byte
requireInRange
(byte obj, byte min, byte max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static double
requireInRange
(double obj, double min, double max) Checks that the specified number is in the given range [min
,max
].static double
requireInRange
(double obj, double min, double max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static float
requireInRange
(float obj, float min, float max) Checks that the specified number is in the given range [min
,max
].static float
requireInRange
(float obj, float min, float max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static int
requireInRange
(int obj, int min, int max) Checks that the specified number is in the given range [min
,max
].static int
requireInRange
(int obj, int min, int max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static long
requireInRange
(long obj, long min, long max) Checks that the specified number is in the given range [min
,max
].static long
requireInRange
(long obj, long min, long max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static short
requireInRange
(short obj, short min, short max) Checks that the specified number is in the given range [min
,max
].static short
requireInRange
(short obj, short min, short max, String message) Checks that the specified number is in the given range [min
,max
] and throws a customizedIllegalArgumentException
if it is not.static 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 double
requireNonNegative
(double obj) Checks that the specified number is non-negative.static double
requireNonNegative
(double obj, String message) Checks that the specified number is non-negative and throws a customizedIllegalArgumentException
if it is not.static float
requireNonNegative
(float obj) Checks that the specified number is non-negative.static float
requireNonNegative
(float 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 Duration
Checks that the specified number is non-negative.static Duration
requireNonNegative
(Duration 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 double
requirePositive
(double obj) Checks that the specified number is positive.static double
requirePositive
(double obj, String message) Checks that the specified number is positive and throws a customizedIllegalArgumentException
if it is not.static float
requirePositive
(float obj) Checks that the specified number is positive.static float
requirePositive
(float 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.static Duration
requirePositive
(Duration obj) Checks that the specified number is positive.static Duration
requirePositive
(Duration 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_IN_RANGE
- See Also:
-
FLOAT_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:
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
-
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:
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 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:
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
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 negativeNullPointerException
- ifobj
isnull
-
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 negativeNullPointerException
- ifobj
isnull
-
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
-
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:
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 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:
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
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
-
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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is 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:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-
requireInRange
Checks that the specified number is in the given range [min
,max
] 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.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 aIllegalArgumentException
is thrown- Returns:
obj
if in range- Throws:
IllegalArgumentException
- ifobj
is not in range
-