Class Range
Range is immutable.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final RangeA range containing all possible 8-bit values.static final RangeA range containing all possible char values.static final RangeAn alias to the range containing all possible 64-bit values.final longThe lower bound of the interval, inclusive.static booleanShould ranges take overflow into account or ignore it? IfignoreOverflowis true, then operations that would result in more than the max value are clipped to the max value (and similarly for the min).static final RangeA range containing all possible 32-bit values.static final RangeA range containing all possible 64-bit values.static final @InternedDistinct RangeThe empty range.static final RangeA range containing all possible 16-bit values.final longThe upper bound of the interval, inclusive.
- 
Method SummaryModifier and TypeMethodDescriptionbitwiseAnd(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.Returns the range of a variable that falls within this range after applying the bitwise complement operation.We give up the analysis for bitwise OR operation.bitwiseXor(Range right) We give up the analysis for bitwise XOR operation.Converts this range to an 8-bit byte range.Converts this range to a char range.booleancontains(long element) Returns true if the element is contained in this range.booleanReturns true if the other range is contained in this range.static Rangecreate(long from, long to) Constructs a range with its bounds specified by two parameters,fromandto.static Rangecreate(Collection<? extends Number> values) Create a Range from a collection of Numbers.static RangeReturns a Range representing all possible values for the given primitive type.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.booleaninthashCode()Returns the smallest range that includes all values contained in both of the two ranges.intRange()Converts this range to a 32-bit integral range.booleanReturn true if this range contains everybytevalue.booleanReturn true if this range contains everycharvalue.booleanReturns true if this range represents a constant value.booleanReturn true if this range contains everyintvalue.booleanReturn true if this range contains everylongvalue.booleanReturn true if this range contains no values.booleanReturn true if this range contains everyshortvalue.booleanisWiderThan(long value) Returns true 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.booleanisWithin(long lb, long ub) Returns true if this range is completely contained in the range specified by the given lower bound inclusive and upper bound inclusive.booleanReturns true if this range is completely contained in the scope of the Integer type.Returns the range with the highest to and from values of this range and the passed range.Returns the range with the lowest to and from values of this range and the passed range.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.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.refineEqualTo(Range right) Refines this range to reflect that some value in it can be equal to a value in the given range.refineGreaterThan(Range right) Refines this range to reflect that some value in it can be greater than a value in the given 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.refineLessThan(Range right) Refines this range to reflect that some value in it can be less than a value in the given 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.refineNotEqualTo(Range right) Refines this range to reflect that some value in it must not be equal to a value in the given range.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.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.Converts a this range to a 16-bit short 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.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.toString()Returns the range of a variable that falls within this range after applying the unary minus operation.Returns the range of a variable that falls within this range after applying the unary plus operation (which is a no-op).Returns the smallest range that includes all values contained in either of the two ranges.unsignedShiftRight(Range right) When this range only contains non-negative values, the refined result should be the same assignedShiftRight(Range).
- 
Field Details- 
frompublic final long fromThe lower bound of the interval, inclusive.
- 
topublic final long toThe upper bound of the interval, inclusive.
- 
ignoreOverflowpublic static boolean ignoreOverflowShould ranges take overflow into account or ignore it?- If ignoreOverflowis true, then operations that would result in more than the max value are clipped to the max value (and similarly for the min).
- If ignoreOverflowis false, then operations that would result in more than the max wrap around according to the rules of twos-complement arithmetic and produce a smaller value (and similarly for the min).
 Any checker that uses this library should 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 bloats the code. 
- If 
- 
LONG_EVERYTHINGA range containing all possible 64-bit values.
- 
INT_EVERYTHINGA range containing all possible 32-bit values.
- 
SHORT_EVERYTHINGA range containing all possible 16-bit values.
- 
CHAR_EVERYTHINGA range containing all possible char values.
- 
BYTE_EVERYTHINGA range containing all possible 8-bit values.
- 
NOTHINGThe empty range. This is the only Range object that contains nothing
- 
EVERYTHINGAn alias to the range containing all possible 64-bit values.
 
- 
- 
Method Details- 
createConstructs a range with its bounds specified by two parameters,fromandto. Requiresfrom <= to.- Parameters:
- from- the lower bound (inclusive)
- to- the upper bound (inclusive)
- Returns:
- the Range [from..to]
 
- 
createCreate a Range from a collection of Numbers.- Parameters:
- values- collection whose min and max values will be used as the range's from and to values
- Returns:
- a range that encompasses all the argument's values (NOTHINGif the argument is an empty collection)
 
- 
createReturns a Range representing all possible values for the given primitive type.- Parameters:
- typeKind- one of INT, SHORT, BYTE, CHAR, or LONG
- Returns:
- the range for the given primitive type
 
- 
toString
- 
equals
- 
hashCodepublic int hashCode()
- 
isLongEverythingpublic boolean isLongEverything()Return true if this range contains everylongvalue.
- 
isIntEverythingpublic boolean isIntEverything()Return true if this range contains everyintvalue.
- 
isShortEverythingpublic boolean isShortEverything()Return true if this range contains everyshortvalue.
- 
isCharEverythingpublic boolean isCharEverything()Return true if this range contains everycharvalue.
- 
isByteEverythingpublic boolean isByteEverything()Return true if this range contains everybytevalue.
- 
isNothingpublic boolean isNothing()Return true if this range contains no values.
- 
intRangeConverts this range to a 32-bit integral range.If ignoreOverflowis true and one of the bounds is outside the Integer range, then that bound is set to the bound of the Integer range.If ignoreOverflowis false and this range is too wide, i.e., wider than the full range of the Integer class, return INT_EVERYTHING.If ignoreOverflowis false and the bounds of this range are not representable as 32-bit integers, convert the bounds to Integer type in accordance with Java twos-complement overflow rules, e.g., Integer.MAX_VALUE + 1 is converted to Integer.MIN_VALUE.- Returns:
- this range, converted to a 32-bit integral range
 
- 
shortRangeConverts a this range to a 16-bit short range.If ignoreOverflowis true and one of the bounds is outside the Short range, then that bound is set to the bound of the Short range.If ignoreOverflowis false and this range is too wide, i.e., wider than the full range of the Short class, return SHORT_EVERYTHING.If ignoreOverflowis false and the bounds of this range are not representable as 16-bit integers, convert the bounds to Short type in accordance with Java twos-complement overflow rules, e.g., Short.MAX_VALUE + 1 is converted to Short.MIN_VALUE.- Returns:
- this range, converted to a 16-bit short range
 
- 
charRangeConverts this range to a char range.If ignoreOverflowis true and one of the bounds is outside the Character range, then that bound is set to the bound of the Character range.If ignoreOverflowis false and this range is too wide, i.e., wider than the full range of the Character class, return CHAR_EVERYTHING.If ignoreOverflowis false and the bounds of this range are not representable as 8-bit integers, convert the bounds to Character type in accordance with Java overflow rules (twos-complement), e.g., Character.MAX_VALUE + 1 is converted to Character.MIN_VALUE.
- 
byteRangeConverts this range to an 8-bit byte range.If ignoreOverflowis true and one of the bounds is outside the Byte range, then that bound is set to the bound of the Byte range.If ignoreOverflowis false and this range is too wide, i.e., wider than the full range of the Byte class, return BYTE_EVERYTHING.If ignoreOverflowis false and the bounds of this range are not representable as 8-bit integers, convert the bounds to Byte type in accordance with Java twos-complement overflow rules, e.g., Byte.MAX_VALUE + 1 is converted to Byte.MIN_VALUE.- Returns:
- this range, converted to an 8-bit byte range
 
- 
containspublic boolean contains(long element) Returns true if the element is contained in this range.- Parameters:
- element- the value to seek
- Returns:
- true if elementis in this range
 
- 
containsReturns true if the other range is contained in this range.- Parameters:
- other- the range that might be within this one
- Returns:
- true if otheris within this range
 
- 
unionReturns the smallest range that includes all values contained in either of the two ranges. We call this the union of two ranges.- Parameters:
- right- a range to union with this range
- Returns:
- a range resulting from the union of the specified range and this range
 
- 
intersectReturns the smallest range that includes all values contained in both of the two ranges. We call this the intersection of two ranges. If there is no overlap between the two ranges, returns an empty range.- Parameters:
- right- the range to intersect with this range
- Returns:
- a range resulting from the intersection of the specified range and this range
 
- 
minReturns the range with the lowest to and from values of this range and the passed range.- Parameters:
- other- the range to compare
- Returns:
- the range with the lowest to and from values of this range and the passed range
 
- 
maxReturns the range with the highest to and from values of this range and the passed range.- Parameters:
- other- the range to compare
- Returns:
- the range with the highest to and from values of this range and the passed range
 
- 
plusReturns 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. We call this the addition of two ranges.- Parameters:
- right- a range to be added to this range
- Returns:
- the range resulting from the addition of the specified range and this range
 
- 
minusReturns 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. We call this the subtraction of two ranges.- Parameters:
- right- the range to be subtracted from this range
- Returns:
- the range resulting from subtracting the specified range from this range
 
- 
timesReturns 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. We call this the multiplication of two ranges.- Parameters:
- right- the specified range to be multiplied by this range
- Returns:
- the range resulting from multiplying the specified range by this range
 
- 
divideReturns 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. We call this the division of two ranges.- Parameters:
- right- the specified range by which this range is divided
- Returns:
- the range resulting from dividing this range by the specified range
 
- 
remainderReturns 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.In the current implementation, the result might not be the smallest range that includes all the possible values. - Parameters:
- right- the specified range by which this range is divided
- Returns:
- the range of the remainder of dividing this range by the specified range
 
- 
shiftLeftReturns 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. We call this the left shift of a range.- Parameters:
- right- the range of bits by which this range is left shifted
- Returns:
- the range resulting from left shifting this range by the specified range
 
- 
signedShiftRightReturns 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. We call this the signed right shift operation of a range.- Parameters:
- right- the range of bits by which this range is signed right shifted
- Returns:
- the range resulting from signed right shifting this range by the specified range
 
- 
unsignedShiftRightWhen this range only contains non-negative values, the refined result should be the same assignedShiftRight(Range). We give up the analysis when this range contains negative value(s).
- 
bitwiseAndReturns 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. We call this the bitwise and operation of a range.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 EVERYTHINGinstead.- Parameters:
- right- the range of mask of the bitwise and operation
- Returns:
- the range resulting from the bitwise and operation of this range and the specified range of mask
 
- 
bitwiseOrWe give up the analysis for bitwise OR operation.
- 
bitwiseXorWe give up the analysis for bitwise XOR operation.
- 
unaryPlusReturns the range of a variable that falls within this range after applying the unary plus operation (which is a no-op).- Returns:
- this range
 
- 
unaryMinusReturns the range of a variable that falls within this range after applying the unary minus operation.- Returns:
- the resulted range of applying unary minus on an arbitrary value in this range
 
- 
bitwiseComplementReturns the range of a variable that falls within this range after applying the bitwise complement operation.- Returns:
- the resulting range of applying bitwise complement on an arbitrary value in this range
 
- 
refineLessThanRefines this range to reflect that some value in it can be less than a value in the given range. This is used for calculating the control-flow-refined result of the < operator. For example:
 Use the@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]. ... }refineGreaterThanEq(Range)method if you are also interested in refining the range ofbin the code above.- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
refineLessThanEqRefines this range to reflect that some value in it can be less than or equal to a value in the given range. This is used for calculating the control-flow-refined result of the <= operator. For example:
 Use the@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]. ... }refineGreaterThan(Range)method if you are also interested in refining the range ofbin the code above.- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
refineGreaterThanRefines this range to reflect that some value in it can be greater than a value in the given range. This is used for calculating the control-flow-refined result of the > operator. For example:
 Use the@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]. ... }refineLessThanEq(Range)method if you are also interested in refining the range ofbin the code above.- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
refineGreaterThanEqRefines this range to reflect that some value in it can be greater than or equal to a value in the given range. This is used for calculating the control-flow-refined result of the >= operator. For example:
 Use the@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]. ... }refineLessThan(Range)method if you are also interested in refining the range ofbin the code above.- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
refineEqualToRefines this range to reflect that some value in it can be equal to a value in the given range. This is used for calculating the control-flow-refined result of the == operator. For example:@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]. ... }- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
refineNotEqualToRefines this range to reflect that some value in it must not be equal to a value in the given range. This only changes the range if the given range (right) contains exactly one integer, and that integer is one of the bounds of this range. This is used for calculating the control-flow-refined result of the != operator. For example:@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. ... }- Parameters:
- right- the specified- Rangeto compare with
- Returns:
- the refined Range
 
- 
isWiderThanpublic boolean isWiderThan(long value) Returns true 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.- Parameters:
- value- the value to compare with
- Returns:
- true if wider than the given value
 
- 
isConstantpublic boolean isConstant()Returns true if this range represents a constant value.- Returns:
- true if this range represents a constant value
 
- 
isWithinpublic boolean isWithin(long lb, long ub) Returns true if this range is completely contained in the range specified by the given lower bound inclusive and upper bound inclusive.- Parameters:
- lb- lower bound for the range that might contain this one
- ub- upper bound for the range that might contain this one
- Returns:
- true if this range is within the given bounds
 
- 
isWithinIntegerpublic boolean isWithinInteger()Returns true if this range is completely contained in the scope of the Integer type.- Returns:
- true if the range is contained within the Integer range inclusive
 
 
-