lightbulb_outline Please take our October 2018 developer survey. Start survey

androidx.core.util

Classes

AtomicFile

Static library support version of the framework's android.util.AtomicFile, a helper class for performing atomic operations on a file by creating a backup file until a write has successfully completed.

ObjectsCompat

This class consists of static utility methods for operating on objects.

Pair

Container to ease passing around a tuple of two objects.

PatternsCompat

Commonly used regular expression patterns.

Pools

Helper class for creating pools of objects.

Top-level functions summary

ArrayMap<K, V>

Returns an empty new ArrayMap.

ArrayMap<K, V>
arrayMapOf(vararg pairs: Pair<K, V>)

Returns a new ArrayMap with the specified contents, given as a list of pairs where the first component is the key and the second component is the value.

ArraySet<T>

Returns an empty new ArraySet.

ArraySet<T>
arraySetOf(vararg values: T)

Returns a new ArraySet with the specified contents.

LruCache<K, V>
lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> })

Creates an LruCache with the given parameters.

infix Range<T>
T.rangeTo(that: T)

Creates a range from this Comparable value to that.

Extension functions summary

For android.util.AtomicFile
Unit

Perform the write operations inside block on this file.

Unit

Sets the content of this file as an array of bytes.

Unit
AtomicFile.writeText(text: String, charset: Charset = Charsets.UTF_8)

Sets the content of this file as text encoded using UTF-8 or specified charset.

ByteArray

Gets the entire content of this file as a byte array.

String
AtomicFile.readText(charset: Charset = Charsets.UTF_8)

Gets the entire content of this file as a String using UTF-8 or specified charset.

For kotlin.Short
Half

Returns a Half instance representing given Short.

For kotlin.Float
Half

Returns a Half instance representing given Float.

For kotlin.Double
Half

Returns a Half instance representing given Double.

For kotlin.String
Half

Returns a Half instance representing given String.

For android.util.LongSparseArray
operator Boolean

Returns true if the collection contains key.

operator Unit
LongSparseArray<T>.set(key: Long, value: T)

Allows the use of the index operator for storing values in the collection.

operator LongSparseArray<T>

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

T
LongSparseArray<T>.getOrDefault(key: Long, defaultValue: T)

Return the value corresponding to key, or defaultValue when not present.

T
LongSparseArray<T>.getOrElse(key: Long, defaultValue: () -> T)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains no elements.

Boolean

Return true when the collection contains elements.

Boolean
LongSparseArray<T>.remove(key: Long, value: T)

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
LongSparseArray<T>.forEach(action: (key: Long, value: T) -> Unit)

Performs the given action for each key/value entry.

LongIterator

Return an iterator over the collection's keys.

Iterator<T>

Return an iterator over the collection's values.

For android.util.SparseArray
operator Boolean

Returns true if the collection contains key.

operator Unit
SparseArray<T>.set(key: Int, value: T)

Allows the use of the index operator for storing values in the collection.

operator SparseArray<T>

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

T
SparseArray<T>.getOrDefault(key: Int, defaultValue: T)

Return the value corresponding to key, or defaultValue when not present.

T
SparseArray<T>.getOrElse(key: Int, defaultValue: () -> T)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains no elements.

Boolean

Return true when the collection contains elements.

Boolean
SparseArray<T>.remove(key: Int, value: T)

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
SparseArray<T>.forEach(action: (key: Int, value: T) -> Unit)

Performs the given action for each key/value entry.

IntIterator

Return an iterator over the collection's keys.

Iterator<T>

Return an iterator over the collection's values.

For android.util.SparseBooleanArray
operator Boolean

Returns true if the collection contains key.

operator Unit

Allows the use of the index operator for storing values in the collection.

operator SparseBooleanArray

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

Boolean

Return the value corresponding to key, or defaultValue when not present.

Boolean
SparseBooleanArray.getOrElse(key: Int, defaultValue: () -> Boolean)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains no elements.

Boolean

Return true when the collection contains elements.

Boolean

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
SparseBooleanArray.forEach(action: (key: Int, value: Boolean) -> Unit)

Performs the given action for each key/value entry.

IntIterator

Return an iterator over the collection's keys.

BooleanIterator

Return an iterator over the collection's values.

For android.util.SparseIntArray
operator Boolean

Returns true if the collection contains key.

operator Unit
SparseIntArray.set(key: Int, value: Int)

Allows the use of the index operator for storing values in the collection.

operator SparseIntArray

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

Int
SparseIntArray.getOrDefault(key: Int, defaultValue: Int)

Return the value corresponding to key, or defaultValue when not present.

Int
SparseIntArray.getOrElse(key: Int, defaultValue: () -> Int)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains no elements.

Boolean

Return true when the collection contains elements.

Boolean
SparseIntArray.remove(key: Int, value: Int)

Removes the entry for key only if it is mapped to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
SparseIntArray.forEach(action: (key: Int, value: Int) -> Unit)

Performs the given action for each key/value entry.

IntIterator

Return an iterator over the collection's keys.

IntIterator

Return an iterator over the collection's values.

For android.util.SparseLongArray
operator Boolean

Returns true if the collection contains key.

operator Unit
SparseLongArray.set(key: Int, value: Long)

Allows the use of the index operator for storing values in the collection.

operator SparseLongArray

Creates a new collection by adding or replacing entries from other.

Boolean

Returns true if the collection contains key.

Boolean

Returns true if the collection contains value.

Long
SparseLongArray.getOrDefault(key: Int, defaultValue: Long)

Return the value corresponding to key, or defaultValue when not present.

Long
SparseLongArray.getOrElse(key: Int, defaultValue: () -> Long)

Return the value corresponding to key, or from defaultValue when not present.

Boolean

Return true when the collection contains no elements.

Boolean

Return true when the collection contains elements.

Boolean
SparseLongArray.remove(key: Int, value: Long)

Removes the entry for key only if it is set to value.

Unit

Update this collection by adding or replacing entries from other.

Unit
SparseLongArray.forEach(action: (key: Int, value: Long) -> Unit)

Performs the given action for each key/value entry.

IntIterator

Return an iterator over the collection's keys.

LongIterator

Return an iterator over the collection's values.

For android.util.Range
operator Range<T>
Range<T>.plus(value: T)

Return the smallest range that includes this and value.

operator Range<T>
Range<T>.plus(other: Range<T>)

Return the smallest range that includes this and other.

infix Range<T>
Range<T>.and(other: Range<T>)

Return the intersection of this range and other.

ClosedRange<T>

Returns this Range as a ClosedRange.

For android.util.Pair
operator F
Pair<F, S>.component1()

Returns the first component of the pair.

operator S
Pair<F, S>.component2()

Returns the second component of the pair.

Pair<F, S>

Returns this Pair as a kotlin.Pair.

For android.util.Size
operator Int

Returns "width", the first component of this Size.

operator Int

Returns "height", the second component of this Size.

For android.util.SizeF
operator Float

Returns "width", the first component of this SizeF.

operator Float

Returns "height", the second component of this SizeF.

For kotlin.Pair
Pair<F, S>

Returns this kotlin.Pair as an Android Pair.

For kotlin.ranges.ClosedRange
Range<T>

Returns this ClosedRange as a Range.

Extension properties summary

For java.util.Locale
Int

Returns layout direction for a given locale.

For android.util.LongSparseArray
Int

Returns the number of key/value pairs in the collection.

For android.util.SparseArray
Int

Returns the number of key/value pairs in the collection.

For android.util.SparseBooleanArray
Int

Returns the number of key/value pairs in the collection.

For android.util.SparseIntArray
Int

Returns the number of key/value pairs in the collection.

For android.util.SparseLongArray
Int

Returns the number of key/value entries in the collection.

Top-level functions

arrayMapOf

inline fun <K, V> arrayMapOf(): ArrayMap<K, V>

Returns an empty new ArrayMap.

arrayMapOf

fun <K, V> arrayMapOf(vararg pairs: Pair<K, V>): ArrayMap<K, V>

Returns a new ArrayMap with the specified contents, given as a list of pairs where the first component is the key and the second component is the value.

If multiple pairs have the same key, the resulting map will contain the value from the last of those pairs.

arraySetOf

inline fun <T> arraySetOf(): ArraySet<T>

Returns an empty new ArraySet.

arraySetOf

fun <T> arraySetOf(vararg values: T): ArraySet<T>

Returns a new ArraySet with the specified contents.

lruCache

inline fun <K : Any, V : Any> lruCache(maxSize: Int, crossinline sizeOf: (key: K, value: V) -> Int = { _, _ -> 1 }, crossinline create: (key: K) -> V? = { null as V? }, crossinline onEntryRemoved: (evicted: Boolean, key: K, oldValue: V, newValue: V?) -> Unit = { _, _, _, _ -> }): LruCache<K, V>

Creates an LruCache with the given parameters.

Parameters
maxSize for caches that do not specify sizeOf, this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache.
sizeOf function that returns the size of the entry for key and value in user-defined units. The default implementation returns 1.
create a create called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.
onEntryRemoved a function called for entries that have been evicted or removed.

rangeTo

inline infix fun <T : Comparable<T>> T.rangeTo(that: T): Range<T>

Creates a range from this Comparable value to that.

Exceptions
IllegalArgumentException if this value is comparatively smaller than that.

Extension functions

and

inline infix fun <T : Comparable<T>> Range<T>.and(other: Range<T>): Range<T>

Return the intersection of this range and other.

Exceptions
IllegalArgumentException if this is disjoint from other.

component1

inline operator fun <F, S> Pair<F, S>.component1(): F

Returns the first component of the pair.

This method allows to use destructuring declarations when working with pairs, for example:


val (first, second) = myPair

component1

inline operator fun Size.component1(): Int

Returns "width", the first component of this Size.

This method allows to use destructuring declarations when working with sizes, for example:


val (w, h) = mySize

component1

inline operator fun SizeF.component1(): Float

Returns "width", the first component of this SizeF.

This method allows to use destructuring declarations when working with sizes, for example:


val (w, h) = mySize

component2

inline operator fun <F, S> Pair<F, S>.component2(): S

Returns the second component of the pair.

This method allows to use destructuring declarations when working with pairs, for example:


val (first, second) = myPair

component2

inline operator fun Size.component2(): Int

Returns "height", the second component of this Size.

This method allows to use destructuring declarations when working with sizes, for example:


val (w, h) = mySize

component2

inline operator fun SizeF.component2(): Float

Returns "height", the second component of this SizeF.

This method allows to use destructuring declarations when working with sizes, for example:


val (w, h) = mySize

contains

inline operator fun <T> LongSparseArray<T>.contains(key: Long): Boolean

Returns true if the collection contains key.

contains

inline operator fun <T> SparseArray<T>.contains(key: Int): Boolean

Returns true if the collection contains key.

contains

inline operator fun SparseBooleanArray.contains(key: Int): Boolean

Returns true if the collection contains key.

contains

inline operator fun SparseIntArray.contains(key: Int): Boolean

Returns true if the collection contains key.

contains

inline operator fun SparseLongArray.contains(key: Int): Boolean

Returns true if the collection contains key.

containsKey

inline fun <T> LongSparseArray<T>.containsKey(key: Long): Boolean

Returns true if the collection contains key.

containsKey

inline fun <T> SparseArray<T>.containsKey(key: Int): Boolean

Returns true if the collection contains key.

containsKey

inline fun SparseBooleanArray.containsKey(key: Int): Boolean

Returns true if the collection contains key.

containsKey

inline fun SparseIntArray.containsKey(key: Int): Boolean

Returns true if the collection contains key.

containsKey

inline fun SparseLongArray.containsKey(key: Int): Boolean

Returns true if the collection contains key.

containsValue

inline fun <T> LongSparseArray<T>.containsValue(value: T): Boolean

Returns true if the collection contains value.

containsValue

inline fun <T> SparseArray<T>.containsValue(value: T): Boolean

Returns true if the collection contains value.

containsValue

inline fun SparseBooleanArray.containsValue(value: Boolean): Boolean

Returns true if the collection contains value.

containsValue

inline fun SparseIntArray.containsValue(value: Int): Boolean

Returns true if the collection contains value.

containsValue

inline fun SparseLongArray.containsValue(value: Long): Boolean

Returns true if the collection contains value.

forEach

inline fun <T> LongSparseArray<T>.forEach(action: (key: Long, value: T) -> Unit): Unit

Performs the given action for each key/value entry.

forEach

inline fun <T> SparseArray<T>.forEach(action: (key: Int, value: T) -> Unit): Unit

Performs the given action for each key/value entry.

forEach

inline fun SparseBooleanArray.forEach(action: (key: Int, value: Boolean) -> Unit): Unit

Performs the given action for each key/value entry.

forEach

inline fun SparseIntArray.forEach(action: (key: Int, value: Int) -> Unit): Unit

Performs the given action for each key/value entry.

forEach

inline fun SparseLongArray.forEach(action: (key: Int, value: Long) -> Unit): Unit

Performs the given action for each key/value entry.

getOrDefault

inline fun <T> LongSparseArray<T>.getOrDefault(key: Long, defaultValue: T): T

Return the value corresponding to key, or defaultValue when not present.

getOrDefault

inline fun <T> SparseArray<T>.getOrDefault(key: Int, defaultValue: T): T

Return the value corresponding to key, or defaultValue when not present.

getOrDefault

inline fun SparseBooleanArray.getOrDefault(key: Int, defaultValue: Boolean): Boolean

Return the value corresponding to key, or defaultValue when not present.

getOrDefault

inline fun SparseIntArray.getOrDefault(key: Int, defaultValue: Int): Int

Return the value corresponding to key, or defaultValue when not present.

getOrDefault

inline fun SparseLongArray.getOrDefault(key: Int, defaultValue: Long): Long

Return the value corresponding to key, or defaultValue when not present.

getOrElse

inline fun <T> LongSparseArray<T>.getOrElse(key: Long, defaultValue: () -> T): T

Return the value corresponding to key, or from defaultValue when not present.

getOrElse

inline fun <T> SparseArray<T>.getOrElse(key: Int, defaultValue: () -> T): T

Return the value corresponding to key, or from defaultValue when not present.

getOrElse

inline fun SparseBooleanArray.getOrElse(key: Int, defaultValue: () -> Boolean): Boolean

Return the value corresponding to key, or from defaultValue when not present.

getOrElse

inline fun SparseIntArray.getOrElse(key: Int, defaultValue: () -> Int): Int

Return the value corresponding to key, or from defaultValue when not present.

getOrElse

inline fun SparseLongArray.getOrElse(key: Int, defaultValue: () -> Long): Long

Return the value corresponding to key, or from defaultValue when not present.

isEmpty

inline fun <T> LongSparseArray<T>.isEmpty(): Boolean

Return true when the collection contains no elements.

isEmpty

inline fun <T> SparseArray<T>.isEmpty(): Boolean

Return true when the collection contains no elements.

isEmpty

inline fun SparseBooleanArray.isEmpty(): Boolean

Return true when the collection contains no elements.

isEmpty

inline fun SparseIntArray.isEmpty(): Boolean

Return true when the collection contains no elements.

isEmpty

inline fun SparseLongArray.isEmpty(): Boolean

Return true when the collection contains no elements.

isNotEmpty

inline fun <T> LongSparseArray<T>.isNotEmpty(): Boolean

Return true when the collection contains elements.

isNotEmpty

inline fun <T> SparseArray<T>.isNotEmpty(): Boolean

Return true when the collection contains elements.

isNotEmpty

inline fun SparseBooleanArray.isNotEmpty(): Boolean

Return true when the collection contains elements.

isNotEmpty

inline fun SparseIntArray.isNotEmpty(): Boolean

Return true when the collection contains elements.

isNotEmpty

inline fun SparseLongArray.isNotEmpty(): Boolean

Return true when the collection contains elements.

keyIterator

fun <T> LongSparseArray<T>.keyIterator(): LongIterator

Return an iterator over the collection's keys.

keyIterator

fun <T> SparseArray<T>.keyIterator(): IntIterator

Return an iterator over the collection's keys.

keyIterator

fun SparseBooleanArray.keyIterator(): IntIterator

Return an iterator over the collection's keys.

keyIterator

fun SparseIntArray.keyIterator(): IntIterator

Return an iterator over the collection's keys.

keyIterator

fun SparseLongArray.keyIterator(): IntIterator

Return an iterator over the collection's keys.

plus

operator fun <T> LongSparseArray<T>.plus(other: LongSparseArray<T>): LongSparseArray<T>

Creates a new collection by adding or replacing entries from other.

plus

operator fun <T> SparseArray<T>.plus(other: SparseArray<T>): SparseArray<T>

Creates a new collection by adding or replacing entries from other.

plus

operator fun SparseBooleanArray.plus(other: SparseBooleanArray): SparseBooleanArray

Creates a new collection by adding or replacing entries from other.

plus

operator fun SparseIntArray.plus(other: SparseIntArray): SparseIntArray

Creates a new collection by adding or replacing entries from other.

plus

operator fun SparseLongArray.plus(other: SparseLongArray): SparseLongArray

Creates a new collection by adding or replacing entries from other.

plus

inline operator fun <T : Comparable<T>> Range<T>.plus(value: T): Range<T>

Return the smallest range that includes this and value.

plus

inline operator fun <T : Comparable<T>> Range<T>.plus(other: Range<T>): Range<T>

Return the smallest range that includes this and other.

putAll

fun <T> LongSparseArray<T>.putAll(other: LongSparseArray<T>): Unit

Update this collection by adding or replacing entries from other.

putAll

fun <T> SparseArray<T>.putAll(other: SparseArray<T>): Unit

Update this collection by adding or replacing entries from other.

putAll

fun SparseBooleanArray.putAll(other: SparseBooleanArray): Unit

Update this collection by adding or replacing entries from other.

putAll

fun SparseIntArray.putAll(other: SparseIntArray): Unit

Update this collection by adding or replacing entries from other.

putAll

fun SparseLongArray.putAll(other: SparseLongArray): Unit

Update this collection by adding or replacing entries from other.

readBytes

inline fun AtomicFile.readBytes(): ByteArray

Gets the entire content of this file as a byte array.

This method is not recommended on huge files. It has an internal limitation of 2 GB file size.

readText

fun AtomicFile.readText(charset: Charset = Charsets.UTF_8): String

Gets the entire content of this file as a String using UTF-8 or specified charset.

This method is not recommended on huge files. It has an internal limitation of 2 GB file size.

remove

fun <T> LongSparseArray<T>.remove(key: Long, value: T): Boolean

Removes the entry for key only if it is mapped to value.

remove

fun <T> SparseArray<T>.remove(key: Int, value: T): Boolean

Removes the entry for key only if it is mapped to value.

remove

fun SparseBooleanArray.remove(key: Int, value: Boolean): Boolean

Removes the entry for key only if it is mapped to value.

remove

fun SparseIntArray.remove(key: Int, value: Int): Boolean

Removes the entry for key only if it is mapped to value.

remove

fun SparseLongArray.remove(key: Int, value: Long): Boolean

Removes the entry for key only if it is set to value.

set

inline operator fun <T> LongSparseArray<T>.set(key: Long, value: T): Unit

Allows the use of the index operator for storing values in the collection.

set

inline operator fun <T> SparseArray<T>.set(key: Int, value: T): Unit

Allows the use of the index operator for storing values in the collection.

set

inline operator fun SparseBooleanArray.set(key: Int, value: Boolean): Unit

Allows the use of the index operator for storing values in the collection.

set

inline operator fun SparseIntArray.set(key: Int, value: Int): Unit

Allows the use of the index operator for storing values in the collection.

set

inline operator fun SparseLongArray.set(key: Int, value: Long): Unit

Allows the use of the index operator for storing values in the collection.

toAndroidPair

inline fun <F, S> Pair<F, S>.toAndroidPair(): Pair<F, S>

Returns this kotlin.Pair as an Android Pair.

toClosedRange

fun <T : Comparable<T>> Range<T>.toClosedRange(): ClosedRange<T>

Returns this Range as a ClosedRange.

toHalf

inline fun Short.toHalf(): Half

Returns a Half instance representing given Short.

See Also

toHalf

inline fun Float.toHalf(): Half

Returns a Half instance representing given Float.

See Also

toHalf

inline fun Double.toHalf(): Half

Returns a Half instance representing given Double.

See Also

toHalf

inline fun String.toHalf(): Half

Returns a Half instance representing given String.

See Also

toKotlinPair

inline fun <F, S> Pair<F, S>.toKotlinPair(): Pair<F, S>

Returns this Pair as a kotlin.Pair.

toRange

fun <T : Comparable<T>> ClosedRange<T>.toRange(): Range<T>

Returns this ClosedRange as a Range.

tryWrite

inline fun AtomicFile.tryWrite(block: (out: FileOutputStream) -> Unit): Unit

Perform the write operations inside block on this file. If block throws an exception the write will be failed. Otherwise the write will be applied atomically to the file.

valueIterator

fun <T> LongSparseArray<T>.valueIterator(): Iterator<T>

Return an iterator over the collection's values.

valueIterator

fun <T> SparseArray<T>.valueIterator(): Iterator<T>

Return an iterator over the collection's values.

valueIterator

fun SparseBooleanArray.valueIterator(): BooleanIterator

Return an iterator over the collection's values.

valueIterator

fun SparseIntArray.valueIterator(): IntIterator

Return an iterator over the collection's values.

valueIterator

fun SparseLongArray.valueIterator(): LongIterator

Return an iterator over the collection's values.

writeBytes

fun AtomicFile.writeBytes(array: ByteArray): Unit

Sets the content of this file as an array of bytes.

writeText

fun AtomicFile.writeText(text: String, charset: Charset = Charsets.UTF_8): Unit

Sets the content of this file as text encoded using UTF-8 or specified charset. If this file exists, it becomes overwritten.

Extension properties

layoutDirection

val Locale.layoutDirection: Int

Returns layout direction for a given locale.

size

inline val <T> LongSparseArray<T>.size: Int

Returns the number of key/value pairs in the collection.

size

inline val <T> SparseArray<T>.size: Int

Returns the number of key/value pairs in the collection.

size

inline val SparseBooleanArray.size: Int

Returns the number of key/value pairs in the collection.

size

inline val SparseIntArray.size: Int

Returns the number of key/value pairs in the collection.

size

inline val SparseLongArray.size: Int

Returns the number of key/value entries in the collection.