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 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(Range<T> range)

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

Range<T> extend(T value)

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

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 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 [lower, upper].

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

Returns the intersection of this range and another range.

String toString()

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

Inherited methods

Public constructors

Range

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
public T getLower ()

Get the lower endpoint.

Returns
T a non-null T reference

getUpper

Added in API level 21
public T getUpper ()

Get the upper endpoint.

Returns
T a non-null T reference

hashCode

Added in API level 21
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 in equals 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 the hashCode 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 the hashCode 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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
public String toString ()

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

Returns
String string representation of the range