Class Range
- All Implemented Interfaces:
Serializable
This example creates a range of Integers whose minimum value is 1 and the maximum value is 5. The
range is inclusive at both ends:
Range intRange = new Range(Integer.class, new Integer(1), new Integer(5));
A Range can be unbounded at either or both of its ends. An unbounded end is specified by passing null
for the value of that end. A Range unbounded at both of its ends represents a range of all possible
values for the Class of elements in that Range. The isMinIncluded() method
will always return true for a Range unbounded on the minimum side and correspondingly the
isMaxIncluded() method will always return true for a Range unbounded on the maximum side.
An empty range is defined as a Range whose minimum value is greater than it's maximum value if the
ends are included, or as a Range whose minimum value is greater than or equal to it's maximum value, if
the minimum or the maximum value is excluded.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRange(Class elementClass, Comparable minValue, boolean isMinIncluded, Comparable maxValue, boolean isMaxIncluded) Constructs aRangeobject given theClassof the elements in theRange, the minimum value and the maximum value.Range(Class elementClass, Comparable minValue, Comparable maxValue) Constructs aRangeobject given theClassof the elements in theRange, the minimum value and the maximum value. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(Comparable value) Returns true if the specified value is within thisRange, i.e. is either equal to or greater than the minimum value of thisRangeand is either lesser than or equal to the maximum value of thisRange.booleanReturns true if the suppliedRangeis fully contained within thisRange.booleanReturns true if thisRangeand the givenRangeboth have elements of the sameClass, their minimum and maximum values are the same, and their isMinIncluded() and isMaxIncluded() methods return the same values.Returns theClassof the elements of thisRange.Returns the maximum value of thisRange.Returns the minimum value of thisRange.inthashCode()Compute a hash code value for thisRangeobject.Returns the intersection of thisRangewith the givenRange.booleanintersects(Range range) Returns true if thisRangeintersects the givenRange.booleanisEmpty()Returns true if thisRangeis empty, i.e. if the minimum value is greater than the maximum value, if both are included, or if the minimum value is greater than equal to the maximum value if either the minimum or maximum value is excluded.booleanReturns true if the maximum value is included within thisRange.booleanReturns true if the minimum value is included within thisRange.Range[]Returns theRangeof values that are in thisRangebut not in the givenRange.toString()Returns aStringrepresentation of thisRange.Returns the union of thisRangewith the givenRange.
-
Constructor Details
-
Range
Constructs aRangeobject given theClassof the elements in theRange, the minimum value and the maximum value. The minimum and the maximum value are considered inclusive.An unbounded range can be specified by passing in a null for either of the two values, in which case the
Rangeis unbounded on one side, or for both, in which case theRangerepresents an all inclusive set.- Parameters:
elementClass- TheClassof theRangeelements.minValue- The lowest value included in theRange.maxValue- The highest value included in theRange.- Throws:
IllegalArgumentException- if minValue and maxValue are both null, and elementClass is not one of the subclasses ofComparable.IllegalArgumentException- if minValue is not the sameClassas elementClass.IllegalArgumentException- if maxValue is not the sameClassas elementClass.
-
Range
public Range(Class elementClass, Comparable minValue, boolean isMinIncluded, Comparable maxValue, boolean isMaxIncluded) Constructs aRangeobject given theClassof the elements in theRange, the minimum value and the maximum value. Whether the minimum value and the maximum value are considered inclusive is specified via theisMinIncludedandisMaxIncludedvariables.An unbounded range can be specified by passing in a null for either of the two values, in which case the
Rangeis unbounded at one end, or for both, in which case theRangerepresents an all inclusive set. If null is passed in for either variable, thebooleanvariables have no effect.- Parameters:
elementClass- TheClassof theRangeelements.minValue- The lowest value for theRange.isMinIncluded- A boolean that defines whether the minimum value is included in theRange.maxValue- The highest value for theRange.isMaxIncluded- A boolean that defines whether the maximum value is included in theRange.- Throws:
IllegalArgumentException- if minValue and maxValue are both null, and elementClass is not one of the subclasses ofComparable.IllegalArgumentException- if minValue is not the sameClassas elementClass.IllegalArgumentException- if maxValue is not the sameClassas elementClass.
-
-
Method Details
-
isMinIncluded
public boolean isMinIncluded()Returns true if the minimum value is included within thisRange. If the range is unbounded at this end, this method will return true. -
isMaxIncluded
public boolean isMaxIncluded()Returns true if the maximum value is included within thisRange. If the range is unbounded at this end, this method will return true. -
getElementClass
Returns theClassof the elements of thisRange. -
getMinValue
Returns the minimum value of thisRange. Returns null if theRangeis unbounded at this end. -
getMaxValue
Returns the maximum value of thisRange. Returns null if theRangeis unbounded at this end. -
contains
Returns true if the specified value is within thisRange, i.e. is either equal to or greater than the minimum value of thisRangeand is either lesser than or equal to the maximum value of thisRange.- Parameters:
value- The value to be checked for being within thisRange.- Throws:
IllegalArgumentException- if theClassof the value parameter is not the same as the elementClass of thisRange.
-
contains
Returns true if the suppliedRangeis fully contained within thisRange. Fully contained is defined as having the minimum and maximum values of the fully contained range lie within the range of values of the containingRange.- Throws:
IllegalArgumentException- if theClassof the elements of the givenRangeis not the same as theClassof the elements of thisRange.IllegalArgumentException- if the givenRangeis null
-
intersects
Returns true if thisRangeintersects the givenRange.- Throws:
IllegalArgumentException- if theClassof the elements of the givenRangeis not the same as theClassof the elements of thisRange.IllegalArgumentException- if the givenRangeis null
-
union
Returns the union of thisRangewith the givenRange. If thisRangeand the givenRangeare disjoint, theRangereturned as a result of the union will have a minimum value set to the minimum of the two disjoint range's minimum values, and the maximum set to the maximum of the two disjoint range's maximum values, thus including the disjoint range within it.- Throws:
IllegalArgumentException- if theClassof the elements of the givenRangeis not the same as theClassof the elements of thisRange.IllegalArgumentException- if the givenRangeis null
-
intersect
Returns the intersection of thisRangewith the givenRange.- Throws:
IllegalArgumentException- if theClassof the elements of the givenRangeis not the same as theClassof the elements of thisRange.IllegalArgumentException- if the givenRangeis null
-
subtract
Returns theRangeof values that are in thisRangebut not in the givenRange. If the subtraction results in two disjointRanges, they will be returned as two elements of aRangearray, otherwise the resultantRangewill be returned as the first element of a one element array.When this
Rangeand the givenRangeare both unbounded at both the ends (i.e both theRanges are all inclusive), this method will return null as the first element of one element array, as a result of the subtraction.When this
Rangeis completely contained in the givenRange, an emptyRangeis returned.- Throws:
IllegalArgumentException- if theClassof the elements of the givenRangeis not the same as theClassof the elements of thisRange.
-
hashCode
public int hashCode()Compute a hash code value for thisRangeobject. -
equals
Returns true if thisRangeand the givenRangeboth have elements of the sameClass, their minimum and maximum values are the same, and their isMinIncluded() and isMaxIncluded() methods return the same values.If this
Rangeand the givenRangeare both empty and theClassof their elements is the same, they will be found to be equal and true will be returned. -
isEmpty
public boolean isEmpty()Returns true if thisRangeis empty, i.e. if the minimum value is greater than the maximum value, if both are included, or if the minimum value is greater than equal to the maximum value if either the minimum or maximum value is excluded. -
toString
Returns aStringrepresentation of thisRange.
-