Range
public
final
class
Range
extends Object
java.lang.Object | |
↳ | android.util.Range<T extends java.lang.Comparable<? super T>> |
Immutable class for describing the range of two numeric values.
A range (or "interval") defines the inclusive boundaries around a contiguous span of
values of some Comparable
type; for example,
"integers from 1 to 100 inclusive."
All ranges are bounded, and the left side of the range is always <=
the right side of the range.
Although the implementation itself is immutable, there is no restriction that objects stored must also be immutable. If mutable objects are stored here, then the range effectively becomes mutable.
Summary
Public constructors | |
---|---|
Range(T lower, T upper)
Create a new immutable range. |
Public methods | |
---|---|
T
|
clamp(T value)
Clamps |
boolean
|
contains(Range<T> range)
Checks if another |
boolean
|
contains(T value)
Checks if the |
static
<T extends Comparable<? super T>>
Range<T>
|
create(T lower, T upper)
Create a new immutable range, with the argument types inferred. |
boolean
|
equals(Object obj)
Compare two ranges for equality. |
Range<T>
|
extend(Range<T> range)
Returns the smallest range that includes this range and
another |
Range<T>
|
extend(T value)
Returns the smallest range that includes this range and
the |
Range<T>
|
extend(T lower, T upper)
Returns the smallest range that includes this range and
the inclusive range specified by |
T
|
getLower()
Get the lower endpoint. |
T
|
getUpper()
Get the upper endpoint. |
int
|
hashCode()
Returns a hash code value for the object. |
Range<T>
|
intersect(T lower, T upper)
Returns the intersection of this range and the inclusive range
specified by |
Range<T>
|
intersect(Range<T> range)
Returns the intersection of this range and another |
String
|
toString()
Return the range as a string representation |
Inherited methods | |
---|---|
Public constructors
Range
public Range (T lower, T upper)
Create a new immutable range.
The endpoints are [lower, upper]
; that
is the range is bounded. lower
must be lesser or equal
to upper
.
Parameters | |
---|---|
lower |
T : The lower endpoint (inclusive) |
upper |
T : The upper endpoint (inclusive) |
Throws | |
---|---|
NullPointerException |
if lower or upper is null |
Public methods
clamp
public T clamp (T value)
Clamps value
to this range.
If the value is within this range, it is returned. Otherwise, if it
is <
than the lower endpoint, the lower endpoint is returned,
else the upper endpoint is returned. Comparisons are performed using the
Comparable
interface.
Parameters | |
---|---|
value |
T : a non-null T reference |
Returns | |
---|---|
T |
value clamped to this range. |
contains
public boolean contains (Range<T> range)
Checks if another range
is within the bounds of this range.
A range is considered to be within this range if both of its endpoints are within this range.
Parameters | |
---|---|
range |
Range : a non-null T reference |
Returns | |
---|---|
boolean |
true if the range is within this inclusive range, false otherwise |
Throws | |
---|---|
NullPointerException |
if range was null |
contains
public boolean contains (T value)
Checks if the value
is within the bounds of this range.
A value is considered to be within this range if it's >=
the lower endpoint and <=
the upper endpoint (using the Comparable
interface.)
Parameters | |
---|---|
value |
T : a non-null T reference |
Returns | |
---|---|
boolean |
true if the value is within this inclusive range, false otherwise |
Throws | |
---|---|
NullPointerException |
if value was null |
create
public static Range<T> create (T lower, T upper)
Create a new immutable range, with the argument types inferred.
The endpoints are [lower, upper]
; that
is the range is bounded. lower
must be lesser or equal
to upper
.
Parameters | |
---|---|
lower |
T : The lower endpoint (inclusive) |
upper |
T : The upper endpoint (inclusive) |
Returns | |
---|---|
Range<T> |
Throws | |
---|---|
NullPointerException |
if lower or upper is null |
equals
public boolean equals (Object obj)
Compare two ranges for equality.
A range is considered equal if and only if both the lower and upper endpoints are also equal.
Parameters | |
---|---|
obj |
Object : This value may be null . |
Returns | |
---|---|
boolean |
true if the ranges are equal, false otherwise |
extend
public Range<T> extend (Range<T> range)
Returns the smallest range that includes this range and
another range
.
E.g. if a <
b <
c <
d, the
extension of [a, c] and [b, d] ranges is [a, d].
As the endpoints are object references, there is no guarantee
which specific endpoint reference is used from the input ranges:
E.g. if a ==
a' <
b <
c, the
extension of [a, b] and [a', c] ranges could be either
[a, c] or ['a, c], where ['a, c] could be either the exact
input range, or a newly created range with the same endpoints.
Parameters | |
---|---|
range |
Range : a non-null Range<T> reference |
Returns | |
---|---|
Range<T> |
the extension of this range and the other range. |
Throws | |
---|---|
NullPointerException |
if range was null |
extend
public Range<T> extend (T value)
Returns the smallest range that includes this range and
the value
.
See extend(android.util.Range)
for more details, as this method is
equivalent to extend(Range.create(value, value))
.
Parameters | |
---|---|
value |
T : a non-null T reference |
Returns | |
---|---|
Range<T> |
the extension of this range and the value. |
Throws | |
---|---|
NullPointerException |
if value was null |
extend
public Range<T> extend (T lower, T upper)
Returns the smallest range that includes this range and
the inclusive range specified by [lower, upper]
.
See extend(android.util.Range)
for more details.
Parameters | |
---|---|
lower |
T : a non-null T reference |
upper |
T : a non-null T reference |
Returns | |
---|---|
Range<T> |
the extension of this range and the other range. |
Throws | |
---|---|
NullPointerException |
if lower or upper was null |
getLower
public T getLower ()
Get the lower endpoint.
Returns | |
---|---|
T |
a non-null T reference |
getUpper
public T getUpper ()
Get the upper endpoint.
Returns | |
---|---|
T |
a non-null T reference |
hashCode
public int hashCode ()
Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
HashMap
.
The general contract of hashCode
is:
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
equals
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns | |
---|---|
int |
a hash code value for this object. |
intersect
public Range<T> intersect (T lower, T upper)
Returns the intersection of this range and the inclusive range
specified by [lower, upper]
.
See intersect(android.util.Range)
for more details.
Parameters | |
---|---|
lower |
T : a non-null T reference |
upper |
T : a non-null T reference |
Returns | |
---|---|
Range<T> |
the intersection of this range and the other range |
Throws | |
---|---|
NullPointerException |
if lower or upper was null |
IllegalArgumentException |
if the ranges are disjoint. |
intersect
public Range<T> intersect (Range<T> range)
Returns the intersection of this range and another range
.
E.g. if a <
b <
c <
d, the
intersection of [a, c] and [b, d] ranges is [b, c].
As the endpoints are object references, there is no guarantee
which specific endpoint reference is used from the input ranges:
E.g. if a ==
a' <
b <
c, the
intersection of [a, b] and [a', c] ranges could be either
[a, b] or ['a, b], where [a, b] could be either the exact
input range, or a newly created range with the same endpoints.
Parameters | |
---|---|
range |
Range : a non-null Range<T> reference |
Returns | |
---|---|
Range<T> |
the intersection of this range and the other range. |
Throws | |
---|---|
NullPointerException |
if range was null |
IllegalArgumentException |
if the ranges are disjoint. |
toString
public String toString ()
Return the range as a string representation "[lower, upper]"
.
Returns | |
---|---|
String |
string representation of the range |