Added in API level 21

Range


class Range<T : Comparable<T>!>
kotlin.Any
   ↳ android.util.Range

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(lower: T, upper: T)

Create a new immutable range.

Public methods
T
clamp(value: T)

Clamps value to this range.

Boolean
contains(range: Range<T>!)

Checks if another range is within the bounds of this range.

Boolean
contains(value: T)

Checks if the value is within the bounds of this range.

static Range<T>!
create(lower: T, upper: T)

Create a new immutable range, with the argument types inferred.

Boolean
equals(other: Any?)

Compare two ranges for equality.

Range<T>!
extend(range: Range<T>!)

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

Range<T>!
extend(value: T)

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

Range<T>!
extend(lower: T, upper: T)

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

T

Get the lower endpoint.

T

Get the upper endpoint.

Int

Returns a hash code value for the object.

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

Returns the intersection of this range and another range.

Range<T>!
intersect(lower: T, upper: T)

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

String

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

Public constructors

Range

Added in API level 21
Range(
    lower: T,
    upper: T)

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)
Exceptions
java.lang.NullPointerException if lower or upper is null

Public methods

clamp

Added in API level 21
fun clamp(value: T): T

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
Return
T value clamped to this range.

contains

Added in API level 21
fun contains(range: Range<T>!): Boolean

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<T>!: a non-null T reference
Return
Boolean true if the range is within this inclusive range, false otherwise
Exceptions
java.lang.NullPointerException if range was null

contains

Added in API level 21
fun contains(value: T): Boolean

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
Return
Boolean true if the value is within this inclusive range, false otherwise
Exceptions
java.lang.NullPointerException if value was null

create

Added in API level 21
static fun <T : Comparable<T>!> create(
    lower: T,
    upper: T
): Range<T>!

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)
Exceptions
java.lang.NullPointerException if lower or upper is null

equals

Added in API level 21
fun equals(other: Any?): Boolean

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 This value may be null.
Return
Boolean true if the ranges are equal, false otherwise

extend

Added in API level 21
fun extend(range: Range<T>!): Range<T>!

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<T>!: a non-null Range<T> reference
Return
Range<T>! the extension of this range and the other range.
Exceptions
java.lang.NullPointerException if range was null

extend

Added in API level 21
fun extend(value: T): Range<T>!

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
Return
Range<T>! the extension of this range and the value.
Exceptions
java.lang.NullPointerException if value was null

extend

Added in API level 21
fun extend(
    lower: T,
    upper: T
): Range<T>!

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
Return
Range<T>! the extension of this range and the other range.
Exceptions
java.lang.NullPointerException if lower or upper was null

getLower

Added in API level 21
fun getLower(): T

Get the lower endpoint.

Return
T a non-null T reference

getUpper

Added in API level 21
fun getUpper(): T

Get the upper endpoint.

Return
T a non-null T reference

hashCode

Added in API level 21
fun hashCode(): Int

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.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.
Return
Int a hash code value for this object.

intersect

Added in API level 21
fun intersect(range: Range<T>!): Range<T>!

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<T>!: a non-null Range<T> reference
Return
Range<T>! the intersection of this range and the other range.
Exceptions
java.lang.NullPointerException if range was null
java.lang.IllegalArgumentException if the ranges are disjoint.

intersect

Added in API level 21
fun intersect(
    lower: T,
    upper: T
): Range<T>!

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
Return
Range<T>! the intersection of this range and the other range
Exceptions
java.lang.NullPointerException if lower or upper was null
java.lang.IllegalArgumentException if the ranges are disjoint.

toString

Added in API level 21
fun toString(): String

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

Return
String string representation of the range