@GwtCompatible public final class NumberCheck extends ObjectBaseCheck<Number,NumberCheck>
Number
arguments. It should be (and is -
if you use the Check
methods correctly) used only for number
arguments of types that do not correspond to one of the primitive types (
byte, short, int, long, float, double
). These types have their
own check classes (IntCheck
for example) with additional checks.
This class provides check methods like isPositive()
,
isGreaterThan(Number)
or isBetween(Number, Number)
.
Attention: As there is no way to compare two arbitrary
Number instances in general, there is no guarantee that the implementations
of this class' check methods handle instances of custom Number subclasses
correctly. However, you can expect them to handle all Number classes provided
by the JDK (including BigInteger
and BigDecimal
) correctly.
For custom subclasses, read the following implementation notes.
Checks involving a comparison between two arbitrary Number instances first
convert both numbers to BigDecimal
and then compare the two objects
using compareTo
. The conversion has
special cases for most of the JDK classes and a default implementation
covering the rest. This implementation uses the number's
toString
method to construct a BigDecimal. So to
make sure your custom subclasses are handled correctly, ensure that their
String representation can be parsed by the
BigDecimal(String)
constructor.
Checks that do not involve two arbitrary Numbers, e.g. isPositive()
have different implementations. Read their documentations' implementation
notes for more information.
arg, nullAllowed
Constructor and Description |
---|
NumberCheck(Number arg)
For internal use only.
|
Modifier and Type | Method and Description |
---|---|
NumberCheck |
is(Number number)
Checks that the argument is equal (equivalent) to the given
Number , throwing an exception otherwise. |
NumberCheck |
isBetween(Number min,
Number max)
Checks that the
Number argument is greater than or equal to
min and less than or equal to max , throwing an
exception otherwise. |
NumberCheck |
isGreaterThan(Number number)
Checks that the argument is (strictly) greater than the given
Number , throwing an exception otherwise. |
NumberCheck |
isLessThan(Number number)
Checks that the argument is (strictly) less than the given
Number
, throwing an exception otherwise. |
NumberCheck |
isNegative()
Checks that the argument is negative, throwing an exception otherwise.
|
NumberCheck |
isPositive()
Checks that the argument is positive, throwing an exception otherwise.
|
check, checkNull, checkWithCause, hasClass, intPropertyCheck, isEqualTo, isInstanceOf, isNotNull, isNullOr, isSameAs, objectPropertyCheck
checkConversion, me, named, not
public NumberCheck(Number arg)
public NumberCheck isPositive()
isNegative()
check for
that:
Check.that(number).not().isNegative();
The message type used for exceptions thrown by this method is
MessageType.ARG_POSITIVE
.
Implementation note: This check uses the
signum
method for instances of
BigDecimal
and doubleValue
for all
other objects.
IllegalArgumentException
- if argument is not positiveisNegative()
public NumberCheck isNegative()
The message type used for exceptions thrown by this method is
MessageType.ARG_NEGATIVE
.
Implementation note: This check uses the
signum
method for instances of
BigDecimal
and doubleValue
for all
other objects.
IllegalArgumentException
- if argument is not negativeisPositive()
public NumberCheck is(Number number)
Number
, throwing an exception otherwise. Note that the definition
of equality is different from the one that is usually associated with
equals
. This check may also consider two
Numbers to be equal if they have different classes. For example, while
new Double(0).equals(new Integer(0))
is false
, this method considers the two numbers to be equal.
The message type used for exceptions thrown by this method is
MessageType.ARG_IS
.
Implementation note: This check uses the conversion
described in the NumberCheck
documentation. Read this before
using this check with custom Number subclasses.
number
- The number this argument must be equal toIllegalArgumentException
- if argument is not equal to the given numberpublic NumberCheck isGreaterThan(Number number)
Number
, throwing an exception otherwise. To create a
greater-than-or-equals check, you may use an inverted
isLessThan(Number)
check:
Check.that(number).not().isLessThan(1.0);
The message type used for exceptions thrown by this method is
MessageType.ARG_GREATER
.
Implementation note: This check uses the conversion
described in the NumberCheck
documentation. Read this before
using this check with custom Number subclasses.
number
- The number this argument must be greater thanIllegalArgumentException
- if argument is not greater than the given numberpublic NumberCheck isLessThan(Number number)
Number
, throwing an exception otherwise. To create a less-than-or-equals check,
you may use an inverted isGreaterThan(Number)
check:
Check.that(number).not().isGreaterThan(1.0);
The message type used for exceptions thrown by this method is
MessageType.ARG_LESS
.
Implementation note: This check uses the conversion
described in the NumberCheck
documentation. Read this before
using this check with custom Number subclasses.
number
- The number this argument must be less thanIllegalArgumentException
- if argument is not less than the given numberpublic NumberCheck isBetween(Number min, Number max)
Number
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 use the
isGreaterThan(Number)
and isLessThan(Number)
checks,
possibly in conjunction with BaseCheck.not()
.
The message type used for exceptions thrown by this method is
MessageType.ARG_BETWEEN
.
Implementation note: This check uses the conversion
described in the NumberCheck
documentation. Read this before
using this check with custom Number subclasses.
min
- The minimum numbermax
- The maximum numberIllegalArgumentException
- if argument is not between the two given numbers as defined
aboveCopyright © 2012–2017 Michael Faes. All rights reserved.