Android APIs
public final class

Range

extends Object
java.lang.Object
   ↳ android.util.Range<T extends java.lang.Comparable<? super T>>

Class Overview

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 value to this range.
boolean contains(Range<T> range)
Checks if another range is within the bounds of this range.
boolean contains(T value)
Checks if the value is within the bounds of this range.
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(T value)
Returns the smallest range that includes this range and the value.
Range<T> extend(Range<T> range)
Returns the smallest range that includes this range and another range.
Range<T> extend(T lower, T upper)
Returns the smallest range that includes this range and the inclusive range specified by [lower, upper].
T getLower()
Get the lower endpoint.
T getUpper()
Get the upper endpoint.
int hashCode()
Returns an integer hash code for this object.
Range<T> intersect(Range<T> range)
Returns the intersection of this range and another range.
Range<T> intersect(T lower, T upper)
Returns the intersection of this range and the inclusive range specified by [lower, upper].
String toString()
Return the range as a string representation "[lower, upper]".
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Range (T lower, T upper)

Added in API level 21

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 The lower endpoint (inclusive)
upper The upper endpoint (inclusive)
Throws
NullPointerException if lower or upper is null

Public Methods

public T clamp (T value)

Added in API level 21

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 a non-null T reference
Returns
  • value clamped to this range.

public boolean contains (Range<T> range)

Added in API level 21

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 a non-null T reference
Returns
  • true if the range is within this inclusive range, false otherwise
Throws
NullPointerException if range was null

public boolean contains (T value)

Added in API level 21

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 a non-null T reference
Returns
  • true if the value is within this inclusive range, false otherwise
Throws
NullPointerException if value was null

public static Range<T> create (T lower, T upper)

Added in API level 21

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 The lower endpoint (inclusive)
upper The upper endpoint (inclusive)
Throws
NullPointerException if lower or upper is null

public boolean equals (Object obj)

Added in API level 21

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 the object to compare this instance with.
Returns
  • true if the ranges are equal, false otherwise

public Range<T> extend (T value)

Added in API level 21

Returns the smallest range that includes this range and the value.

See extend(Range) for more details, as this method is equivalent to extend(Range.create(value, value)).

Parameters
value a non-null T reference
Returns
  • the extension of this range and the value.
Throws
NullPointerException if value was null

public Range<T> extend (Range<T> range)

Added in API level 21

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 a non-null Range reference
Returns
  • the extension of this range and the other range.
Throws
NullPointerException if range was null

public Range<T> extend (T lower, T upper)

Added in API level 21

Returns the smallest range that includes this range and the inclusive range specified by [lower, upper].

See extend(Range) for more details.

Parameters
lower a non-null T reference
upper a non-null T reference
Returns
  • the extension of this range and the other range.
Throws
NullPointerException if lower or upper was null

public T getLower ()

Added in API level 21

Get the lower endpoint.

Returns
  • a non-null T reference

public T getUpper ()

Added in API level 21

Get the upper endpoint.

Returns
  • a non-null T reference

public int hashCode ()

Added in API level 21

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public Range<T> intersect (Range<T> range)

Added in API level 21

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 a non-null Range reference
Returns
  • the intersection of this range and the other range.
Throws
NullPointerException if range was null
IllegalArgumentException if the ranges are disjoint.

public Range<T> intersect (T lower, T upper)

Added in API level 21

Returns the intersection of this range and the inclusive range specified by [lower, upper].

See intersect(Range) for more details.

Parameters
lower a non-null T reference
upper a non-null T reference
Returns
  • the intersection of this range and the other range
Throws
NullPointerException if lower or upper was null
IllegalArgumentException if the ranges are disjoint.

public String toString ()

Added in API level 21

Return the range as a string representation "[lower, upper]".

Returns
  • string representation of the range