Class Range<T>
- Type Parameters:
- T- The type of range values.
- All Implemented Interfaces:
- Serializable
- Direct Known Subclasses:
- NumberRange
The objects need to either be implementations of Comparable
 or you need to supply a Comparator.
#ThreadSafe# if the objects and comparator are thread-safe.
- Since:
- 3.0
- See Also:
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T extends Comparable<? super T>>
 Range<T>between(T fromInclusive, T toInclusive) Deprecated.static <T> Range<T>between(T fromInclusive, T toInclusive, Comparator<T> comparator) Deprecated.booleanChecks whether the specified element occurs within this range.booleancontainsRange(Range<T> otherRange) Checks whether this range contains all the elements of the specified range.intelementCompareTo(T element) Checks where the specified element occurs relative to this range.booleanCompares this range to another object to test if they are equal.Fits the given element into this range by returning the given element or, if out of bounds, the range minimum if below, or the range maximum if above.Gets the comparator being used to determine if objects are within the range.Gets the maximum value in this range.Gets the minimum value in this range.inthashCode()Gets a suitable hash code for the range.intersectionWith(Range<T> other) Calculate the intersection ofthisand an overlapping Range.static <T extends Comparable<? super T>>
 Range<T>is(T element) Creates a range using the specified element as both the minimum and maximum in this range.static <T> Range<T>is(T element, Comparator<T> comparator) Creates a range using the specified element as both the minimum and maximum in this range.booleanChecks whether this range is after the specified element.booleanisAfterRange(Range<T> otherRange) Checks whether this range is completely after the specified range.booleanChecks whether this range is before the specified element.booleanisBeforeRange(Range<T> otherRange) Checks whether this range is completely before the specified range.booleanChecks whether this range ends with the specified element.booleanWhether or not the Range is using the natural ordering of the elements.booleanisOverlappedBy(Range<T> otherRange) Checks whether this range is overlapped by the specified range.booleanisStartedBy(T element) Checks whether this range starts with the specified element.static <T extends Comparable<? super T>>
 Range<T>of(T fromInclusive, T toInclusive) Creates a range with the specified minimum and maximum values (both inclusive).static <T> Range<T>of(T fromInclusive, T toInclusive, Comparator<T> comparator) Creates a range with the specified minimum and maximum values (both inclusive).toString()Gets the range as aString.Formats the receiver using the given format.
- 
Method Details- 
between@Deprecated public static <T extends Comparable<? super T>> Range<T> between(T fromInclusive, T toInclusive) Deprecated.Creates a range with the specified minimum and maximum values (both inclusive).The range uses the natural ordering of the elements to determine where values lie in the range. The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values. - Type Parameters:
- T- the type of the elements in this range
- Parameters:
- fromInclusive- the first value that defines the edge of the range, inclusive
- toInclusive- the second value that defines the edge of the range, inclusive
- Returns:
- the range object, not null
- Throws:
- NullPointerException- when fromInclusive is null.
- NullPointerException- when toInclusive is null.
- ClassCastException- if the elements are not- Comparable
 
- 
between@Deprecated public static <T> Range<T> between(T fromInclusive, T toInclusive, Comparator<T> comparator) Deprecated.Creates a range with the specified minimum and maximum values (both inclusive).The range uses the specified Comparatorto determine where values lie in the range.The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values. - Type Parameters:
- T- the type of the elements in this range
- Parameters:
- fromInclusive- the first value that defines the edge of the range, inclusive
- toInclusive- the second value that defines the edge of the range, inclusive
- comparator- the comparator to be used, null for natural ordering
- Returns:
- the range object, not null
- Throws:
- NullPointerException- when fromInclusive is null.
- NullPointerException- when toInclusive is null.
- ClassCastException- if using natural ordering and the elements are not- Comparable
 
- 
isCreates a range using the specified element as both the minimum and maximum in this range.The range uses the natural ordering of the elements to determine where values lie in the range. - Type Parameters:
- T- the type of the elements in this range
- Parameters:
- element- the value to use for this range, not null
- Returns:
- the range object, not null
- Throws:
- NullPointerException- if the element is null
- ClassCastException- if the element is not- Comparable
 
- 
isCreates a range using the specified element as both the minimum and maximum in this range.The range uses the specified Comparatorto determine where values lie in the range.- Type Parameters:
- T- the type of the elements in this range
- Parameters:
- element- the value to use for this range, must not be- null
- comparator- the comparator to be used, null for natural ordering
- Returns:
- the range object, not null
- Throws:
- NullPointerException- if the element is null
- ClassCastException- if using natural ordering and the elements are not- Comparable
 
- 
ofCreates a range with the specified minimum and maximum values (both inclusive).The range uses the natural ordering of the elements to determine where values lie in the range. The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values. - Type Parameters:
- T- the type of the elements in this range
- Parameters:
- fromInclusive- the first value that defines the edge of the range, inclusive
- toInclusive- the second value that defines the edge of the range, inclusive
- Returns:
- the range object, not null
- Throws:
- NullPointerException- if either element is null
- ClassCastException- if the elements are not- Comparable
- Since:
- 3.13.0
 
- 
ofCreates a range with the specified minimum and maximum values (both inclusive).The range uses the specified Comparatorto determine where values lie in the range.The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values. - Type Parameters:
- T- the type of the elements in this range
- Parameters:
- fromInclusive- the first value that defines the edge of the range, inclusive
- toInclusive- the second value that defines the edge of the range, inclusive
- comparator- the comparator to be used, null for natural ordering
- Returns:
- the range object, not null
- Throws:
- NullPointerException- when fromInclusive is null.
- NullPointerException- when toInclusive is null.
- ClassCastException- if using natural ordering and the elements are not- Comparable
- Since:
- 3.13.0
 
- 
containsChecks whether the specified element occurs within this range.- Parameters:
- element- the element to check for, null returns false
- Returns:
- true if the specified element occurs within this range
 
- 
containsRangeChecks whether this range contains all the elements of the specified range.This method may fail if the ranges have two different comparators or element types. - Parameters:
- otherRange- the range to check, null returns false
- Returns:
- true if this range contains the specified range
- Throws:
- RuntimeException- if ranges cannot be compared
 
- 
elementCompareToChecks where the specified element occurs relative to this range.The API is reminiscent of the Comparable interface returning -1if the element is before the range,0if contained within the range and1if the element is after the range.- Parameters:
- element- the element to check for, not null
- Returns:
- -1, 0 or +1 depending on the element's location relative to the range
- Throws:
- NullPointerException- if- elementis- null
 
- 
equalsCompares this range to another object to test if they are equal.To be equal, the minimum and maximum values must be equal, which ignores any differences in the comparator. 
- 
fitFits the given element into this range by returning the given element or, if out of bounds, the range minimum if below, or the range maximum if above.Range<Integer> range = Range.between(16, 64); range.fit(-9) --> 16 range.fit(0) --> 16 range.fit(15) --> 16 range.fit(16) --> 16 range.fit(17) --> 17 ... range.fit(63) --> 63 range.fit(64) --> 64 range.fit(99) --> 64- Parameters:
- element- the element to check for, not null
- Returns:
- the minimum, the element, or the maximum depending on the element's location relative to the range
- Throws:
- NullPointerException- if- elementis- null
- Since:
- 3.10
 
- 
getComparatorGets the comparator being used to determine if objects are within the range.Natural ordering uses an internal comparator implementation, thus this method never returns null. See isNaturalOrdering().- Returns:
- the comparator being used, not null
 
- 
getMaximumGets the maximum value in this range.- Returns:
- the maximum value in this range, not null
 
- 
getMinimumGets the minimum value in this range.- Returns:
- the minimum value in this range, not null
 
- 
hashCodeGets a suitable hash code for the range.
- 
intersectionWithCalculate the intersection ofthisand an overlapping Range.- Parameters:
- other- overlapping Range
- Returns:
- range representing the intersection of thisandother(thisif equal)
- Throws:
- IllegalArgumentException- if- otherdoes not overlap- this
- Since:
- 3.0.1
 
- 
isAfterChecks whether this range is after the specified element.- Parameters:
- element- the element to check for, null returns false
- Returns:
- true if this range is entirely after the specified element
 
- 
isAfterRangeChecks whether this range is completely after the specified range.This method may fail if the ranges have two different comparators or element types. - Parameters:
- otherRange- the range to check, null returns false
- Returns:
- true if this range is completely after the specified range
- Throws:
- RuntimeException- if ranges cannot be compared
 
- 
isBeforeChecks whether this range is before the specified element.- Parameters:
- element- the element to check for, null returns false
- Returns:
- true if this range is entirely before the specified element
 
- 
isBeforeRangeChecks whether this range is completely before the specified range.This method may fail if the ranges have two different comparators or element types. - Parameters:
- otherRange- the range to check, null returns false
- Returns:
- true if this range is completely before the specified range
- Throws:
- RuntimeException- if ranges cannot be compared
 
- 
isEndedByChecks whether this range ends with the specified element.- Parameters:
- element- the element to check for, null returns false
- Returns:
- true if the specified element occurs within this range
 
- 
isNaturalOrderingWhether or not the Range is using the natural ordering of the elements.Natural ordering uses an internal comparator implementation, thus this method is the only way to check if a null comparator was specified. - Returns:
- true if using natural ordering
 
- 
isOverlappedByChecks whether this range is overlapped by the specified range.Two ranges overlap if there is at least one element in common. This method may fail if the ranges have two different comparators or element types. - Parameters:
- otherRange- the range to test, null returns false
- Returns:
- true if the specified range overlaps with this
  range; otherwise, false
- Throws:
- RuntimeException- if ranges cannot be compared
 
- 
isStartedByChecks whether this range starts with the specified element.- Parameters:
- element- the element to check for, null returns false
- Returns:
- true if the specified element occurs within this range
 
- 
toStringGets the range as aString.The format of the String is '[min..max]'. 
- 
toStringFormats the receiver using the given format.This uses Formattableto perform the formatting. Three variables may be used to embed the minimum, maximum and comparator. Use%1$sfor the minimum element,%2$sfor the maximum element and%3$sfor the comparator. The default format used bytoString()is[%1$s..%2$s].- Parameters:
- format- the format string, optionally containing- %1$s,- %2$sand- %3$s, not null
- Returns:
- the formatted string, not null
 
 
- 
of(Comparable, Comparable).