@GwtCompatible public final class IntCheck extends PrimitiveBaseCheck<IntCheck>
int
arguments. It can also be (and
is - if you use the Check
methods correctly) used for
short
and byte
arguments.
It provides methods like isPositive()
or
isBetween(int, int)
but also methods specific to integer numbers
like isValidIndex(Object[])
.
Constructor and Description |
---|
IntCheck(int arg)
For internal use only.
|
Modifier and Type | Method and Description |
---|---|
IntCheck |
is(int number)
Checks that the
int argument is equal to the given number,
throwing an exception otherwise. |
IntCheck |
isBetween(int min,
int max)
Checks that the
int argument is greater than or equal to
min and less than or equal to max , throwing an
exception otherwise. |
IntCheck |
isGreaterThan(int number)
Checks that the
int argument is (strictly) greater than the
given number, throwing an exception otherwise. |
IntCheck |
isLessThan(int number)
Checks that the
long argument is (strictly) less than the
given number, throwing an exception otherwise. |
IntCheck |
isNegative()
Checks that the
int argument is negative, throwing an
exception otherwise. |
IntCheck |
isPositive()
Checks that the
int argument is positive, throwing an
exception otherwise. |
IntCheck |
isValidIndex(Collection<?> collection)
Checks that the
int argument is a valid index for the given
Collection , throwing an exception otherwise. |
IntCheck |
isValidIndex(int size)
Checks that the
int argument is a valid index in a
Collection or array with the given size or length, throwing an
exception otherwise. |
IntCheck |
isValidIndex(Object[] array)
Checks that the
int argument is a valid index for the given
array, throwing an exception otherwise. |
check, disable
checkConversion, me, named, not
public IntCheck isPositive()
int
argument is positive, throwing an
exception otherwise. Note that zero is not a valid number. Often, you
want to check that a number is non-negative. Use the inverted
isNegative()
check for that:
Check.that(number).not().isNegative();
The message type used for exceptions thrown by this method is
MessageType.ARG_POSITIVE
.
IllegalArgumentException
- if argument is not positiveisNegative()
public IntCheck isNegative()
int
argument is negative, throwing an
exception otherwise.
The message type used for exceptions thrown by this method is
MessageType.ARG_NEGATIVE
.
IllegalArgumentException
- if argument is not negativeisPositive()
public IntCheck is(int number)
int
argument is equal to the given number,
throwing an exception otherwise. This check method exists merely for
using with a property check. See StringCheck.hasLengthWhich()
for
example. However, in conjunction with inversion (see BaseCheck.not()
),
this method can also be used to exclude a single value.
The message type used for exceptions thrown by this method is
MessageType.ARG_IS
.
number
- The number this argument must be equal toIllegalArgumentException
- if argument is not equal to the given numberpublic IntCheck isGreaterThan(int number)
int
argument is (strictly) greater than the
given number, throwing an exception otherwise. To create a
greater-than-or-equals check, you may either decrement the
number
argument by one or use an inverted
isLessThan(int)
check:
Check.that(number).not().isLessThan(1);
The message type used for exceptions thrown by this method is
MessageType.ARG_GREATER
.
number
- The number this argument must be greater thanIllegalArgumentException
- if argument is not greater than the given numberpublic IntCheck isLessThan(int number)
long
argument is (strictly) less than the
given number, throwing an exception otherwise. To create a
less-than-or-equals check, you may either increment the
number
argument by one or use an inverted
isGreaterThan(int)
check:
Check.that(number).not().isGreaterThan(1);
The message type used for exceptions thrown by this method is
MessageType.ARG_LESS
.
number
- The number this argument must be less thanIllegalArgumentException
- if argument is not less than the given numberpublic IntCheck isBetween(int min, int max)
int
argument is greater than or equal to
min
and less than or equal to max
, throwing an
exception otherwise.
To create a strict or one-sided-strict between test, you may either
increment or decrement the arguments of this check or use the
isGreaterThan(int)
and isLessThan(int)
checks, possibly
in conjunction with BaseCheck.not()
.
The message type used for exceptions thrown by this method is
MessageType.ARG_BETWEEN
.
min
- The minimum numbermax
- The maximum numberIllegalArgumentException
- if argument is not between the two given numbers as defined
abovepublic IntCheck isValidIndex(int size)
int
argument is a valid index in a
Collection
or array with the given size or length, throwing an
exception otherwise. This means that the argument must be less than
size
and not less than zero.
Two convenient methods exist: isValidIndex(Collection)
,
isValidIndex(Object[])
.
The message type used for exceptions thrown by this method is
MessageType.ARG_INDEX
.
size
- The size of the data structure the argument should be a valid
index forIllegalArgumentException
- if argument is not a valid index as defined abovepublic IntCheck isValidIndex(Collection<?> collection)
int
argument is a valid index for the given
Collection
, throwing an exception otherwise. This means that the
argument must be less than collection.size()
and not less
than zero.
The message type used for exceptions thrown by this method is
MessageType.ARG_INDEX
.
collection
- The collection the argument should be a valid index for. Must
not be null
.IllegalArgumentException
- if argument is not a valid index as defined abovepublic IntCheck isValidIndex(Object[] array)
int
argument is a valid index for the given
array, throwing an exception otherwise. This means that the argument must
be less than array.length
and not less than zero.
The message type used for exceptions thrown by this method is
MessageType.ARG_INDEX
.
array
- The array the argument should be a valid index for. Must not
be null
.IllegalArgumentException
- if argument is not a valid index as defined aboveCopyright © 2012–2017 Michael Faes. All rights reserved.