Class CircularRange<T>
- java.lang.Object
-
- com.norconex.commons.lang.CircularRange<T>
-
- Type Parameters:
T- the range type
- All Implemented Interfaces:
Serializable
public final class CircularRange<T> extends Object implements 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
Rangeclass.- Since:
- 1.14.0
- Author:
- Pascal Essiembre
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <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).booleancontains(T element)Checks whether the specified element occurs within this range.booleancontainsRange(CircularRange<T> otherRange)Checks whether this range contains all the elements of the specified range.booleanequals(Object obj)Compares this range to another object to test if they are equal.TgetCircleEnd()Gets the end value of this circular range.TgetCircleStart()Gets the start value of this circular range.Comparator<T>getComparator()Gets the comparator being used to determine if objects are within the range.TgetMaximum()Gets the maximum value in this range.TgetMinimum()Gets the minimum value in this range.inthashCode()Gets a suitable hash code for the range.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.booleanisEndedBy(T element)Checks whether this range ends with the specified element.booleanisNaturalOrdering()Whether 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.booleanisRolling()Whether or not the range rolls over the circular end.booleanisStartedBy(T element)Checks whether this range starts with the specified element.StringtoString()Gets the range as aString.StringtoString(String format)Formats the receiver using the given format.CircularRange<T>withCircularBoundaries(T circleStartInclusive, T circleEndInclusive)Obtains a new range with the specified circular start and end values (both inclusive).CircularRange<T>withRange(T rangeFromInclusive, T rangeToInclusive)Obtains a new range with the specified minimum and maximum range values (both inclusive).
-
-
-
Method Detail
-
is
public 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.
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
public 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.
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
public static <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).
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:
rangeFromInclusive- the first value that defines the edge of the range, inclusiverangeToInclusive- the second value that defines the edge of the range, inclusivecircleStartInclusive- 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 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:
rangeFromInclusive- the first value that defines the edge of the range, inclusiverangeToInclusive- the second value that defines the edge of the range, inclusivecircleStartInclusive- the value that defines the circular start, inclusivecircleEndInclusive- the value that defines the circular end, 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
public CircularRange<T> withCircularBoundaries(T circleStartInclusive, T circleEndInclusive)
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
public CircularRange<T> withRange(T rangeFromInclusive, T rangeToInclusive)
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
public T getMinimum()
Gets the minimum value in this range.
- Returns:
- the minimum value in this range, not null
-
getMaximum
public T getMaximum()
Gets the maximum value in this range.
- Returns:
- the maximum value in this range, not null
-
getCircleStart
public T getCircleStart()
Gets the start value of this circular range.
- Returns:
- the start value of this circular range, not null
-
getCircleEnd
public T getCircleEnd()
Gets the end value of this circular range.
- Returns:
- the end value of this circular range, not null
-
getComparator
public Comparator<T> 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
public boolean contains(T element)
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
public boolean isStartedBy(T element)
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
public boolean isEndedBy(T element)
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
public boolean containsRange(CircularRange<T> otherRange)
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
public boolean isOverlappedBy(CircularRange<T> otherRange)
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
-
equals
public boolean equals(Object obj)
Compares this range to another object to test if they are equal.
.To be equal, the minimum and maximum values as well as circular start and end values must be equal, which ignores any differences in the comparator.
-
hashCode
public int hashCode()
Gets a suitable hash code for the range.
-
toString
public String toString()
Gets the range as a
String.The format of the String is '[min..max](start..end)'.
-
toString
public String toString(String format)
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
-
-