# Range

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary: [Ctors](https://developer.android.com/reference/android/util/Range#pubctors) \| [Methods](https://developer.android.com/reference/android/util/Range#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/util/Range#inhmethods)  

# Range

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/util/Range "View this page in Kotlin") \|Java


`
public

final

class
Range
`


`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


`

`


`

|---|-----------------------------------------------------------------------------------------------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object)                                                         ||
| ↳ | android.util.Range\<T extends [java.lang.Comparable](https://developer.android.com/reference/java/lang/Comparable)\<? super T\>\> |

<br />

*** ** * ** ***

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](https://developer.android.com/reference/java/lang/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.

<br />

## Summary

|                                                          ### Public constructors                                                           ||
|-----------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[Range](https://developer.android.com/reference/android/util/Range#Range(T,%20T))`(T lower, T upper) ` Create a new immutable range. |

|                                                                                                                                                                                                                    ### Public methods                                                                                                                                                                                                                     ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` T`                                                                                                                                                                                   | ` `[clamp](https://developer.android.com/reference/android/util/Range#clamp(T))`(T value) ` Clamps `value` to this range.                                                                                                                                         |
| ` boolean`                                                                                                                                                                             | ` `[contains](https://developer.android.com/reference/android/util/Range#contains(android.util.Range<T>))`(`[Range](https://developer.android.com/reference/android/util/Range)`<T> range) ` Checks if another `range` is within the bounds of this range.        |
| ` boolean`                                                                                                                                                                             | ` `[contains](https://developer.android.com/reference/android/util/Range#contains(T))`(T value) ` Checks if the `value` is within the bounds of this range.                                                                                                       |
| ` static <T extends `[Comparable](https://developer.android.com/reference/java/lang/Comparable)`<? super T>> `[Range](https://developer.android.com/reference/android/util/Range)`<T>` | ` `[create](https://developer.android.com/reference/android/util/Range#create(T,%20T))`(T lower, T upper) ` Create a new immutable range, with the argument types inferred.                                                                                       |
| ` boolean`                                                                                                                                                                             | ` `[equals](https://developer.android.com/reference/android/util/Range#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Compare two ranges for equality.                                                    |
| ` `[Range](https://developer.android.com/reference/android/util/Range)`<T>`                                                                                                            | ` `[extend](https://developer.android.com/reference/android/util/Range#extend(android.util.Range<T>))`(`[Range](https://developer.android.com/reference/android/util/Range)`<T> range) ` Returns the smallest range that includes this range and another `range`. |
| ` `[Range](https://developer.android.com/reference/android/util/Range)`<T>`                                                                                                            | ` `[extend](https://developer.android.com/reference/android/util/Range#extend(T))`(T value) ` Returns the smallest range that includes this range and the `value`.                                                                                                |
| ` `[Range](https://developer.android.com/reference/android/util/Range)`<T>`                                                                                                            | ` `[extend](https://developer.android.com/reference/android/util/Range#extend(T,%20T))`(T lower, T upper) ` Returns the smallest range that includes this range and the inclusive range specified by `[lower, upper]`.                                            |
| ` T`                                                                                                                                                                                   | ` `[getLower](https://developer.android.com/reference/android/util/Range#getLower())`() ` Get the lower endpoint.                                                                                                                                                 |
| ` T`                                                                                                                                                                                   | ` `[getUpper](https://developer.android.com/reference/android/util/Range#getUpper())`() ` Get the upper endpoint.                                                                                                                                                 |
| ` int`                                                                                                                                                                                 | ` `[hashCode](https://developer.android.com/reference/android/util/Range#hashCode())`() ` Returns a hash code value for the object.                                                                                                                               |
| ` `[Range](https://developer.android.com/reference/android/util/Range)`<T>`                                                                                                            | ` `[intersect](https://developer.android.com/reference/android/util/Range#intersect(T,%20T))`(T lower, T upper) ` Returns the intersection of this range and the inclusive range specified by `[lower, upper]`.                                                   |
| ` `[Range](https://developer.android.com/reference/android/util/Range)`<T>`                                                                                                            | ` `[intersect](https://developer.android.com/reference/android/util/Range#intersect(android.util.Range<T>))`(`[Range](https://developer.android.com/reference/android/util/Range)`<T> range) ` Returns the intersection of this range and another `range`.        |
| ` `[String](https://developer.android.com/reference/java/lang/String)                                                                                                                  | ` `[toString](https://developer.android.com/reference/android/util/Range#toString())`() ` Return the range as a string representation `"[lower, upper]"`.                                                                                                         |

| ### Inherited methods |
|-----------------------|---|
| From class ` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                        | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                             | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                            | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this `Object`.                                                                                                                                                 | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                   | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                              | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                           | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                              | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.                                                                                             | ||

## Public constructors

### Range

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/java/lang/Comparable#compareTo(T))
to `upper`.

<br />

<br />

|                     Parameters                      ||
|---------|--------------------------------------------|
| `lower` | `T`: The lower endpoint (inclusive) <br /> |
| `upper` | `T`: The upper endpoint (inclusive) <br /> |

|                                                             Throws                                                              ||
|------------------------------------------------------------------------------------------------|---------------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `lower` or `upper` is `null` |

## Public methods

### clamp

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/java/lang/Comparable) interface.

<br />

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `value` | `T`: a non-`null` `T` reference <br /> |

|                  Returns                   ||
|-----|---------------------------------------|
| `T` | `value` clamped to this range. <br /> |

### contains

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

<br />

|                     Parameters                      ||
|---------|--------------------------------------------|
| `range` | `Range`: a non-`null` `T` reference <br /> |

|                                         Returns                                         ||
|-----------|------------------------------------------------------------------------------|
| `boolean` | `true` if the range is within this inclusive range, `false` otherwise <br /> |

|                                                        Throws                                                         ||
|------------------------------------------------------------------------------------------------|-----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `range` was `null` |

### contains

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/java/lang/Comparable)
interface.)

<br />

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `value` | `T`: a non-`null` `T` reference <br /> |

|                                         Returns                                         ||
|-----------|------------------------------------------------------------------------------|
| `boolean` | `true` if the value is within this inclusive range, `false` otherwise <br /> |

|                                                        Throws                                                         ||
|------------------------------------------------------------------------------------------------|-----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `value` was `null` |

### create

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/java/lang/Comparable#compareTo(T))
to `upper`.

<br />

<br />

|                     Parameters                      ||
|---------|--------------------------------------------|
| `lower` | `T`: The lower endpoint (inclusive) <br /> |
| `upper` | `T`: The upper endpoint (inclusive) <br /> |

|                                     Returns                                      ||
|--------------------------------------------------------------------------|--------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | <br /> |

|                                                             Throws                                                              ||
|------------------------------------------------------------------------------------------------|---------------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `lower` or `upper` is `null` |

### equals

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

<br />

|                    Parameters                     ||
|-------|--------------------------------------------|
| `obj` | `Object`: This value may be `null`. <br /> |

|                               Returns                               ||
|-----------|----------------------------------------------------------|
| `boolean` | `true` if the ranges are equal, `false` otherwise <br /> |

### extend

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

<br />

|                         Parameters                         ||
|---------|---------------------------------------------------|
| `range` | `Range`: a non-`null` `Range<T>` reference <br /> |

|                                                              Returns                                                              ||
|--------------------------------------------------------------------------|---------------------------------------------------------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | the extension of this range and the other range. <br /> |

|                                                        Throws                                                         ||
|------------------------------------------------------------------------------------------------|-----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `range` was `null` |

### extend

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Range<T> extend (T value)
```

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


See [extend(android.util.Range)](https://developer.android.com/reference/android/util/Range#extend(android.util.Range<T>)) for more details, as this method is
equivalent to `extend(Range.create(value, value))`.

<br />

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `value` | `T`: a non-`null` `T` reference <br /> |

|                                                           Returns                                                           ||
|--------------------------------------------------------------------------|---------------------------------------------------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | the extension of this range and the value. <br /> |

|                                                        Throws                                                         ||
|------------------------------------------------------------------------------------------------|-----------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `value` was `null` |

### extend

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/util/Range#extend(android.util.Range<T>)) for more details.

<br />

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `lower` | `T`: a non-`null` `T` reference <br /> |
| `upper` | `T`: a non-`null` `T` reference <br /> |

|                                                              Returns                                                              ||
|--------------------------------------------------------------------------|---------------------------------------------------------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | the extension of this range and the other range. <br /> |

|                                                              Throws                                                              ||
|------------------------------------------------------------------------------------------------|----------------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException) | if `lower` or `upper` was `null` |

### getLower

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public T getLower ()
```

Get the lower endpoint.

<br />

|                Returns                 ||
|-----|-----------------------------------|
| `T` | a non-`null` `T` reference <br /> |

### getUpper

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public T getUpper ()
```

Get the upper endpoint.

<br />

|                Returns                 ||
|-----|-----------------------------------|
| `T` | a non-`null` `T` reference <br /> |

### hashCode

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/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](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)) 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](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)) 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.

<br />

<br />

|                     Returns                      ||
|-------|-------------------------------------------|
| `int` | a hash code value for this object. <br /> |

### intersect

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/util/Range#intersect(android.util.Range<T>)) for more details.

<br />

<br />

|                   Parameters                    ||
|---------|----------------------------------------|
| `lower` | `T`: a non-`null` `T` reference <br /> |
| `upper` | `T`: a non-`null` `T` reference <br /> |

|                                                               Returns                                                               ||
|--------------------------------------------------------------------------|-----------------------------------------------------------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | the intersection of this range and the other range <br /> |

|                                                                  Throws                                                                  ||
|--------------------------------------------------------------------------------------------------------|----------------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)         | if `lower` or `upper` was `null` |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the ranges are disjoint.      |

### intersect

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

<br />

|                         Parameters                         ||
|---------|---------------------------------------------------|
| `range` | `Range`: a non-`null` `Range<T>` reference <br /> |

|                                                               Returns                                                                ||
|--------------------------------------------------------------------------|------------------------------------------------------------|
| [Range](https://developer.android.com/reference/android/util/Range)`<T>` | the intersection of this range and the other range. <br /> |

|                                                               Throws                                                                ||
|--------------------------------------------------------------------------------------------------------|-----------------------------|
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)         | if `range` was `null`       |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the ranges are disjoint. |

### toString

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

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

<br />

|                                                    Returns                                                    ||
|--------------------------------------------------------------------|-------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | string representation of the range <br /> |