Class CircularRange<T>
- Type Parameters:
T- the range type
- All Implemented Interfaces:
Serializable
A range from a possible set of values that rolls over when defined circular
start or end is reached. Because the range is circular, there is no
concept of before and after and maximum range can be smaller than minimum.
This class is otherwise similar to Apache Commons Lang Range class.
Even though this class implements Serializable, it makes no
guarantee about supplied arguments. If serialization is important to you,
make sure supplied arguments are serializable.
- Since:
- 1.14.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Comparable<T>>
CircularRange<T> between(T fromInclusive, T toInclusive) Obtains a range with the specified minimum and maximum values also serving as circular start and end (all inclusive).static <T> CircularRange<T> between(T fromInclusive, T toInclusive, Comparator<T> comparator) Obtains a range with the specified minimum and maximum values also serving as circular start and end (all inclusive).static <T extends Comparable<T>>
CircularRange<T> between(T circleStartInclusive, T circleEndInclusive, T rangeFromInclusive, T rangeToInclusive) Obtains a range with the specified minimum and maximum values and circular start and end values (all inclusive).static <T> CircularRange<T> between(T circleStartInclusive, T circleEndInclusive, T rangeFromInclusive, T rangeToInclusive, Comparator<T> comparator) Obtains a range with the specified minimum and maximum values and circular start and end values (all inclusive).booleanChecks whether the specified element occurs within this range.booleancontainsRange(CircularRange<T> otherRange) Checks whether this range contains all the elements of the specified range.booleanGets the end value of this circular range.Gets the start value of this circular range.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()static <T extends Comparable<T>>
CircularRange<T> is(T element) Obtains a range using the specified element as both the minimum and maximum in this range and as both circular start and end.static <T> CircularRange<T> is(T element, Comparator<T> comparator) Obtains a range using the specified element as both the minimum and maximum in this range and as both circular start and end.booleanChecks whether this range ends with the specified element.booleanWhether or not the CircularRange is using the natural ordering of the elements.booleanisOverlappedBy(CircularRange<T> otherRange) Checks whether this range is overlapped by the specified range.booleanWhether or not the range rolls over the circular end.booleanisStartedBy(T element) Checks whether this range starts with the specified element.toString()Gets the range as aString.Formats the receiver using the given format.withCircularBoundaries(T circleStartInclusive, T circleEndInclusive) Obtains a new range with the specified circular start and end values (both inclusive).Obtains a new range with the specified minimum and maximum range values (both inclusive).
-
Method Details
-
is
Obtains a range using the specified element as both the minimum and maximum in this range and as both circular start and end.
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:
IllegalArgumentException- if the element is nullClassCastException- if the element is notComparable
-
is
Obtains a range using the specified element as both the minimum and maximum in this range and as both circular start and end.
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 benullcomparator- the comparator to be used, null for natural ordering- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if the element is nullClassCastException- if using natural ordering and the elements are notComparable
-
between
Obtains a range with the specified minimum and maximum values also serving as circular start and end (all inclusive).
The range uses the natural ordering of the elements to determine where values lie in the range.
The arguments must be passed in order (min,max).
- Type Parameters:
T- the type of the elements in this range- Parameters:
fromInclusive- the first value that defines the edge of the range, inclusivetoInclusive- the second value that defines the edge of the range, inclusive- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is null or not in orderClassCastException- if the elements are notComparable
-
between
public static <T> CircularRange<T> between(T fromInclusive, T toInclusive, Comparator<T> comparator) Obtains a range with the specified minimum and maximum values also serving as circular start and end (all inclusive).
The range uses the specified
Comparatorto determine where values lie in the range.The arguments must be passed in order (min,max).
- Type Parameters:
T- the type of the elements in this range- Parameters:
fromInclusive- the first value that defines the edge of the range, inclusivetoInclusive- the second value that defines the edge of the range, inclusivecomparator- the comparator to be used, null for natural ordering- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is nullClassCastException- if using natural ordering and the elements are notComparable
-
between
public static <T extends Comparable<T>> CircularRange<T> between(T circleStartInclusive, T circleEndInclusive, T rangeFromInclusive, T rangeToInclusive) Obtains a range with the specified minimum and maximum values and circular start and end values (all inclusive).
The range uses the natural ordering of the elements to determine where values lie in the range.
The circle arguments must be passed in order (min,max). The range arguments can be passed in any order (min,max or max,min). The order will be respected.
- Type Parameters:
T- the type of the elements in this range- Parameters:
circleStartInclusive- the value that defines the circular start, inclusivecircleEndInclusive- the value that defines the circular end, inclusiverangeFromInclusive- the first value that defines the edge of the range, inclusiverangeToInclusive- the second value that defines the edge of the range, inclusive- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is null or not in orderClassCastException- if the elements are notComparable
-
between
public static <T> CircularRange<T> between(T circleStartInclusive, T circleEndInclusive, T rangeFromInclusive, T rangeToInclusive, Comparator<T> comparator) Obtains a range with the specified minimum and maximum values and circular start and end values (all inclusive).
The range uses the specified
Comparatorto determine where values lie in the range.The circle arguments must be passed in order (min,max). The range arguments can be passed in any order (min,max or max,min). The order will be respected.
- Type Parameters:
T- the type of the elements in this range- Parameters:
circleStartInclusive- the value that defines the circular start, inclusivecircleEndInclusive- the value that defines the circular end, inclusiverangeFromInclusive- the first value that defines the edge of the range, inclusiverangeToInclusive- the second value that defines the edge of the range, inclusivecomparator- the comparator to be used, null for natural ordering- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is nullClassCastException- if using natural ordering and the elements are notComparable
-
withCircularBoundaries
Obtains a new range with the specified circular start and end values (both inclusive). The range values and comparator are the same.
- Parameters:
circleStartInclusive- the value that defines the circular start, inclusivecircleEndInclusive- the value that defines the circular end, inclusive- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is null
-
withRange
Obtains a new range with the specified minimum and maximum range values (both inclusive). The circular start and end are the same.
- Parameters:
rangeFromInclusive- the first value that defines the edge of the range, inclusiverangeToInclusive- the second value that defines the edge of the range, inclusive- Returns:
- the range object, not null
- Throws:
IllegalArgumentException- if either element is null
-
getMinimum
Gets the minimum value in this range.
- Returns:
- the minimum value in this range, not null
-
getMaximum
Gets the maximum value in this range.
- Returns:
- the maximum value in this range, not null
-
getCircleStart
Gets the start value of this circular range.
- Returns:
- the start value of this circular range, not null
-
getCircleEnd
Gets the end value of this circular range.
- Returns:
- the end value of this circular range, not null
-
getComparator
Gets 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
-
isNaturalOrdering
public boolean isNaturalOrdering()Whether or not the CircularRange 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
-
isRolling
public boolean isRolling()Whether or not the range rolls over the circular end. Basically, if the range maximum is smaller than the minimum.
- Returns:
trueif rolling over
-
contains
Checks 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
-
isStartedBy
Checks 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
-
isEndedBy
Checks 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
-
containsRange
Checks 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
-
isOverlappedBy
Checks 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
-
toString
Gets the range as a
String.The format of the String is '[min..max](start..end)'.
-
toString
Formats the receiver using the given format.
This uses
Formattableto perform the formatting. Five variables may be used to embed the minimum, maximum, circular start, circular end and comparator. Use%1$sfor the minimum element,%2$sfor the maximum element,%3$sfor the circular start,%4$sfor the circular end, and%5$sfor the comparator. The default format used bytoString()is[%1$s..%2$s](%3$s..%4$s).- Parameters:
format- the format string, optionally containing%1$s,%2$s,%3$s,%4$sand%3$s, not null- Returns:
- the formatted string, not null
-
equals
-
hashCode
public int hashCode()
-