Package org.geotools.util
Class Range<T extends Comparable<? super T>>
- Object
-
- Range<T>
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DateRange
,NumberRange
public class Range<T extends Comparable<? super T>> extends Object implements Serializable
A range between a minimum and maximum comparable. The minimum/maximum may be included, excluded or unbounded. The later case is indicated bynull
values on one or both ends.This class is a method compatible replacement for the
Range
class with the following differences:-
Unbounded ranges (i.e.
null
minimal or maximal values) are considered exclusive rather than inclusive, since an iteration over the values will never reach the infinite bound. This interpretation brings some simplification in implementation and usage (e.g. a loop over the values should not attempt to process thenull
value). -
subtract(org.geotools.util.Range<?>)
returns an empty array if the whole range is subtracted.
NumberRange
do that). This class is weakly parameterized in order to allow this flexibility. If any constructor or method is invoked with an argument value of illegal class, then anIllegalArgumentException
is thrown. TheClassCastException
is thrown only in case of bug in theRange
class or subclasses implementation.- Since:
- 2.5
- Author:
- Jody Garnett, Martin Desruisseaux
- See Also:
Range
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Range(Class<T> elementClass, T value)
Creates a new range bounded by a single inclusive value.Range(Class<T> elementClass, T minValue, boolean isMinIncluded, T maxValue, boolean isMaxIncluded)
Creates a new range bounded by the given values.Range(Class<T> elementClass, T minValue, T maxValue)
Creates a new range bounded by the given inclusive values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(Comparable<?> value)
Returnstrue
if this range contains the given value.boolean
contains(Range<?> range)
Returnstrue
if this range contains fully the given range.boolean
equals(Object object)
Compares this range with the given object for equality.Class<T>
getElementClass()
Returns the class of elements in this range.T
getMaxValue()
Returns the maximal value, ornull
if unbounded.T
getMinValue()
Returns the minimal value, ornull
if unbounded.int
hashCode()
Returns a hash code value for this range.Range<?>
intersect(Range<?> range)
Returns the intersection between this range and the provided range.boolean
intersects(Range<?> range)
Returnstrue
if this range intersects the given range.boolean
isEmpty()
Returnstrue
if this range is empty.boolean
isMaxIncluded()
Indicates ifgetMaxValue()
is included in the range.boolean
isMinIncluded()
Indicates ifgetMinValue()
is included in the range.Range<?>[]
subtract(Range<?> range)
Returns the range of values that are in this range but not in the given range.String
toString()
Returns a string representation of this range.Range<?>
union(Range<?> range)
Returns the union of this range with the given range.
-
-
-
Constructor Detail
-
Range
public Range(Class<T> elementClass, T value)
Creates a new range bounded by a single inclusive value. The minimum and maximum values are set to the given one.- Parameters:
elementClass
- The class of the range elements.value
- The minimal and maximum value (inclusive), ornull
for an unbounded range.
-
Range
public Range(Class<T> elementClass, T minValue, T maxValue)
Creates a new range bounded by the given inclusive values.- Parameters:
elementClass
- The class of the range elements.minValue
- The minimal value (inclusive), ornull
if none.maxValue
- The maximal value (inclusive), ornull
if none.
-
Range
public Range(Class<T> elementClass, T minValue, boolean isMinIncluded, T maxValue, boolean isMaxIncluded)
Creates a new range bounded by the given values.- Parameters:
elementClass
- The class of the range elements.minValue
- The minimal value, ornull
if none.isMinIncluded
-true
if the minimal value is inclusive, orfalse
if exclusive.maxValue
- The maximal value, ornull
if none.isMaxIncluded
-true
if the maximal value is inclusive, orfalse
if exclusive.
-
-
Method Detail
-
getElementClass
public Class<T> getElementClass()
Returns the class of elements in this range. The element class extendsComparable
.- Returns:
- The class of elements in this range.
- See Also:
Range.getElementClass()
-
getMinValue
public T getMinValue()
Returns the minimal value, ornull
if unbounded. IfisMinIncluded
istrue
, then the value is considered included in the set. Otherwise it is considered excluded.- Returns:
- The minimal value.
- See Also:
Range.getMinValue()
-
isMinIncluded
public boolean isMinIncluded()
Indicates ifgetMinValue()
is included in the range.- Returns:
true
if the minimal value is inclusive.- See Also:
Range.isMinIncluded
-
getMaxValue
public T getMaxValue()
Returns the maximal value, ornull
if unbounded. IfisMaxIncluded
istrue
, then the value is considered included in the set. Otherwise it is considered excluded.- Returns:
- The maximal value.
- See Also:
Range.getMaxValue()
-
isMaxIncluded
public boolean isMaxIncluded()
Indicates ifgetMaxValue()
is included in the range.- Returns:
true
if the maximal value is inclusive.- See Also:
Range.isMaxIncluded
-
isEmpty
public boolean isEmpty()
Returnstrue
if this range is empty. A range is empty if the minimum value is smaller than the maximum value, or if they are equals while at least one of them is exclusive.- Returns:
true
if this range is empty.- See Also:
Range.isEmpty()
-
contains
public boolean contains(Comparable<?> value) throws IllegalArgumentException
Returnstrue
if this range contains the given value. A range never contains thenull
value. This is consistent with the class javadoc stating that null minimum or maximum values are exclusive.- Parameters:
value
- The value to check for inclusion in this range.- Returns:
true
if the given value is included in this range.- Throws:
IllegalArgumentException
- is the given value can not be converted to a valid type through widening conversion.
-
contains
public boolean contains(Range<?> range) throws IllegalArgumentException
Returnstrue
if this range contains fully the given range.- Parameters:
range
- The range to check for inclusion in this range.- Returns:
true
if the given range is included in this range.- Throws:
IllegalArgumentException
- is the given range can not be converted to a valid type through widening conversion.
-
intersects
public boolean intersects(Range<?> range) throws IllegalArgumentException
Returnstrue
if this range intersects the given range.- Parameters:
range
- The range to check for intersection with this range.- Returns:
true
if the given range intersects this range.- Throws:
IllegalArgumentException
- is the given range can not be converted to a valid type through widening conversion.- See Also:
Range.intersects(javax.media.jai.util.Range)
-
intersect
public Range<?> intersect(Range<?> range) throws IllegalArgumentException
Returns the intersection between this range and the provided range.- Parameters:
range
- The range to intersect.- Returns:
- The intersection of this range with the provided range.
- Throws:
IllegalArgumentException
- is the given range can not be converted to a valid type through widening conversion.- See Also:
Range.intersect(javax.media.jai.util.Range)
-
subtract
public Range<?>[] subtract(Range<?> range) throws IllegalArgumentException
Returns the range of values that are in this range but not in the given range. This method returns an array of length 0, 1 or 2:- If the given range contains fully this range, returns an array of length 0.
- If the given range is in the middle of this range, then the subtraction results in two disjoint ranges which will be returned as two elements in the array.
- Otherwise returns an array of length 1.
- Parameters:
range
- The range to substract.- Returns:
- This range without the given range.
- Throws:
IllegalArgumentException
- is the given range can not be converted to a valid type through widening conversion.- See Also:
Range.subtract(javax.media.jai.util.Range)
-
union
public Range<?> union(Range<?> range) throws IllegalArgumentException
Returns the union of this range with the given range.- Parameters:
range
- The range to add to this range.- Returns:
- The union of this range with the given range.
- Throws:
IllegalArgumentException
- is the given range can not be converted to a valid type through widening conversion.- See Also:
Range.union(javax.media.jai.util.Range)
-
equals
public boolean equals(Object object)
Compares this range with the given object for equality.
-
hashCode
public int hashCode()
Returns a hash code value for this range.
-
-