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
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.
T getLower()
Get the lower endpoint.
T getUpper()
Get the upper endpoint.
int hashCode()
Returns an integer hash code for this object.
boolean inRange(T value)
Checks if the value is within the bounds of this range.
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)

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

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

public T getLower ()

Get the lower endpoint.

Returns
  • a non-null T reference

public T getUpper ()

Get the upper endpoint.

Returns
  • a non-null T reference

public int hashCode ()

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 boolean inRange (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 >= then the lower endpoint and <= to 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 String toString ()

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

Returns
  • string representation of the range