public class Range extends Object
Modifier and Type | Field and Description |
---|---|
static Range |
BYTE_EVERYTHING
A range containing all possible 8-bit values.
|
static Range |
EVERYTHING
A range containing all possible 64-bit values.
|
long |
from
The lower bound of the interval, inclusive.
|
static boolean |
ignoreOverflow
Should ranges take overflow into account or ignore it?
|
static Range |
INT_EVERYTHING
A range containing all possible 32-bit values.
|
static Range |
NOTHING
The empty range.
|
static Range |
SHORT_EVERYTHING
A range containing all possible 16-bit values.
|
long |
to
The upper bound of the interval, inclusive.
|
Constructor and Description |
---|
Range(long from,
long to)
Constructs a range with its bounds specified by two parameters,
from and to . |
Modifier and Type | Method and Description |
---|---|
Range |
bitwiseAnd(Range right)
Returns a range that includes all possible values resulting from performing the bitwise and
operation on a value in this range by a mask in the specified range.
|
Range |
bitwiseComplement()
Returns the range of a variable that falls within this range after applying the bitwise
complement operation.
|
Range |
bitwiseOr(Range right)
We give up the analysis for bitwise OR operation.
|
Range |
bitwiseXor(Range right)
We give up the analysis for bitwise XOR operation.
|
Range |
byteRange()
Converts a this range to a 8-bit byte range.
|
boolean |
contains(long element)
Returns true if the element is contained in this range.
|
boolean |
contains(Range other)
Returns true if the element is contained in this range.
|
Range |
divide(Range right)
Returns the smallest range that includes all possible values resulting from dividing (integer
division) an arbitrary value in this range by an arbitrary value in the specified range.
|
boolean |
equals(Object obj) |
int |
hashCode() |
Range |
intersect(Range right)
Returns the smallest range that includes all values contained in both of the two ranges.
|
Range |
intRange()
Converts a this range to a 32-bit integral range.
|
boolean |
isByteEverything()
Return true if this range contains every
byte value. |
boolean |
isConstant()
Determines if this range represents a constant value.
|
boolean |
isIntEverything()
Return true if this range contains every
int value. |
boolean |
isLongEverything()
Return true if this range contains every
long value. |
boolean |
isNothing()
Return true if this range contains no values.
|
boolean |
isShortEverything()
Return true if this range contains every
short value. |
boolean |
isWiderThan(long value)
Determines if the range is wider than a given value, i.e., if the number of possible values
enclosed by this range is more than the given value.
|
boolean |
isWithin(long lb,
long ub)
Determines if this range is completely contained in the range specified by the given lower
bound and upper bound.
|
Range |
minus(Range right)
Returns the smallest range that includes all possible values resulting from subtracting an
arbitrary value in the specified range from an arbitrary value in this range.
|
Range |
plus(Range right)
Returns the smallest range that includes all possible values resulting from adding an
arbitrary value in the specified range to an arbitrary value in this range.
|
Range |
refineEqualTo(Range right)
Refines this range to reflect that some value in it can be equal to a value in the given
range.
|
Range |
refineGreaterThan(Range right)
Refines this range to reflect that some value in it can be greater than a value in the given
range.
|
Range |
refineGreaterThanEq(Range right)
Refines this range to reflect that some value in it can be greater than or equal to a value
in the given range.
|
Range |
refineLessThan(Range right)
Refines this range to reflect that some value in it can be less than a value in the given
range.
|
Range |
refineLessThanEq(Range right)
Refines this range to reflect that some value in it can be less than or equal to a value in
the given range.
|
Range |
refineNotEqualTo(Range right)
Refines this range to reflect that some value in it must not be equal to a value in the given
range.
|
Range |
remainder(Range right)
Returns a range that includes all possible values of the remainder of dividing an arbitrary
value in this range by an arbitrary value in the specified range.
|
Range |
shiftLeft(Range right)
Returns a range that includes all possible values resulting from left shifting an arbitrary
value in this range by an arbitrary number of bits in the specified range.
|
Range |
shortRange()
Converts a this range to a 16-bit short range.
|
Range |
signedShiftRight(Range right)
Returns a range that includes all possible values resulting from signed right shifting an
arbitrary value in this range by an arbitrary number of bits in the specified range.
|
Range |
times(Range right)
Returns the smallest range that includes all possible values resulting from multiplying an
arbitrary value in the specified range by an arbitrary value in this range.
|
String |
toString() |
Range |
unaryMinus()
Returns the range of a variable that falls within this range after applying the unary minus
operation.
|
Range |
unaryPlus()
Returns the range of a variable that falls within this range after applying the unary plus
operation (which is a no-op).
|
Range |
union(Range right)
Returns the smallest range that includes all values contained in either of the two ranges.
|
Range |
unsignedShiftRight(Range right)
When this range only contains non-negative values, the refined result should be the same as
signedShiftRight(Range) . |
public final long from
public final long to
public static boolean ignoreOverflow
Any checker that uses this library should be sure to set this field. By default, this field is set to false (meaning overflow is taken into account), but a previous checker might have set it to true.
A static field is used because passing an instance field throughout the class (and at all of its use cases) results in the code being unacceptably bloated.
public static final Range EVERYTHING
public static final Range INT_EVERYTHING
public static final Range SHORT_EVERYTHING
public static final Range BYTE_EVERYTHING
public static final Range NOTHING
public Range(long from, long to)
from
and to
.from
- the lower bound (inclusive)to
- the upper bound (inclusive)public boolean isLongEverything()
long
value.public boolean isIntEverything()
int
value.public boolean isShortEverything()
short
value.public boolean isByteEverything()
byte
value.public boolean isNothing()
public Range intRange()
If ignoreOverflow
is true and one of the bounds is outside the Integer range,
then that bound is set to the bound of the Integer range.
If ignoreOverflow
is false and this range is too wide, i.e., wider than the full
range of the Integer class, return INT_EVERYTHING.
If ignoreOverflow
is false and the bounds of this range are not representable as
32-bit integers, convert the bounds to Integer type in accordance with Java overflow rules,
e.g., Integer.MAX_VALUE + 1 is converted to Integer.MIN_VALUE.
public Range shortRange()
If ignoreOverflow
is true and one of the bounds is outside the Short range, then
that bound is set to the bound of the Short range.
If ignoreOverflow
is false and this range is too wide, i.e., wider than the full
range of the Short class, return SHORT_EVERYTHING.
If ignoreOverflow
is false and the bounds of this range are not representable as
16-bit integers, convert the bounds to Integer type in accordance with Java overflow rules,
e.g., Short.MAX_VALUE + 1 is converted to Short.MIN_VALUE.
public Range byteRange()
If ignoreOverflow
is true and one of the bounds is outside the Byte range, then
that bound is set to the bound of the Byte range.
If ignoreOverflow
is false and this range is too wide, i.e., wider than the full
range of the Byte class, return BYTE_EVERYTHING.
If ignoreOverflow
is false and the bounds of this range are not representable as
8-bit integers, convert the bounds to Integer type in accordance with Java overflow rules,
e.g., Byte.MAX_VALUE + 1 is converted to Byte.MIN_VALUE.
public boolean contains(long element)
public boolean contains(Range other)
public Range union(Range right)
right
- a range to union with this rangepublic Range intersect(Range right)
right
- the range to intersect with this rangepublic Range plus(Range right)
right
- a range to be added to this rangepublic Range minus(Range right)
right
- the range to be subtracted from this rangepublic Range times(Range right)
right
- the specified range to be multiplied by this rangepublic Range divide(Range right)
right
- the specified range by which this range is dividedpublic Range remainder(Range right)
In the current implementation, the result might not be the smallest range that includes all the possible values.
right
- the specified range by which this range is dividedpublic Range shiftLeft(Range right)
right
- the range of bits by which this range is left shiftedpublic Range signedShiftRight(Range right)
right
- the range of bits by which this range is signed right shiftedpublic Range unsignedShiftRight(Range right)
signedShiftRight(Range)
. We give up the analysis when this range contains negative
value(s).public Range bitwiseAnd(Range right)
The current implementation is conservative: it only refines the cases where the range of
mask represents a constant. In other cases, it gives up on the refinement and returns EVERYTHING
instead.
right
- the range of mask of the bitwise and operationpublic Range unaryPlus()
public Range unaryMinus()
public Range bitwiseComplement()
public Range refineLessThan(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 3, to = 7) int b;
...
if (a < b) {
// range of a is now refined to [0, 6] because a value in range [7, 10]
// cannot be smaller than variable b with range [3, 7].
...
}
Use the refineGreaterThanEq(Range)
method if you are also interested in refining the
range of b
in the code above.right
- the specified Range
to compare withRange
public Range refineLessThanEq(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 3, to = 7) int b;
...
if (a <= b) {
// range of a is now refined to [0, 7] because a value in range [8, 10]
// cannot be less than or equal to variable b with range [3, 7].
...
}
Use the refineGreaterThan(Range)
method if you are also interested in refining the
range of b
in the code above.right
- the specified Range
to compare withRange
public Range refineGreaterThan(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 3, to = 7) int b;
...
if (a > b) {
// range of a is now refined to [4, 10] because a value in range [0, 3]
// cannot be greater than variable b with range [3, 7].
...
}
Use the refineLessThanEq(Range)
method if you are also interested in refining the
range of b
in the code above.right
- the specified Range
to compare withRange
public Range refineGreaterThanEq(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 3, to = 7) int b;
...
if (a >= b) {
// range of a is now refined to [3, 10] because a value in range [0, 2]
// cannot be greater than or equal to variable b with range [3, 7].
...
}
Use the refineLessThan(Range)
method if you are also interested in refining the
range of b
in the code above.right
- the specified Range
to compare withRange
public Range refineEqualTo(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 3, to = 15) int b;
...
if (a == b) {
// range of a is now refined to [3, 10] because a value in range [0, 2]
// cannot be equal to variable b with range [3, 15].
...
}
right
- the specified Range
to compare withRange
public Range refineNotEqualTo(Range right)
@IntRange(from = 0, to = 10) int a;
@IntRange(from = 0, to = 0) int b;
...
if (a != b) {
// range of a is now refined to [1, 10] because it cannot
// be zero.
...
}
right
- the specified Range
to compare withRange
public boolean isWiderThan(long value)
value
- the value to compare withpublic boolean isConstant()
public boolean isWithin(long lb, long ub)