1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. 18 19package androidx.collection 20 21/** Returns the number of key/value pairs in the collection. */ 22inline val <T> SparseArrayCompat<T>.size get() = size() 23 24/** Returns true if the collection contains [key]. */ 25inline operator fun <T> SparseArrayCompat<T>.contains(key: Int) = indexOfKey(key) >= 0 26 27/** Allows the use of the index operator for storing values in the collection. */ 28inline operator fun <T> SparseArrayCompat<T>.set(key: Int, value: T) = put(key, value) 29 30/** Creates a new collection by adding or replacing entries from [other]. */ 31operator fun <T> SparseArrayCompat<T>.plus(other: SparseArrayCompat<T>): SparseArrayCompat<T> { 32 val new = SparseArrayCompat<T>(size() + other.size()) 33 new.putAll(this) 34 new.putAll(other) 35 return new 36} 37 38/** Returns true if the collection contains [key]. */ 39inline fun <T> SparseArrayCompat<T>.containsKey(key: Int) = indexOfKey(key) >= 0 40 41/** Returns true if the collection contains [value]. */ 42inline fun <T> SparseArrayCompat<T>.containsValue(value: T) = indexOfValue(value) != -1 43 44/** Return the value corresponding to [key], or [defaultValue] when not present. */ 45inline fun <T> SparseArrayCompat<T>.getOrDefault(key: Int, defaultValue: T) = 46 get(key) ?: defaultValue 47 48/** Return the value corresponding to [key], or from [defaultValue] when not present. */ 49inline fun <T> SparseArrayCompat<T>.getOrElse(key: Int, defaultValue: () -> T) = 50 get(key) ?: defaultValue() 51 52/** Return true when the collection contains elements. */ 53inline fun <T> SparseArrayCompat<T>.isNotEmpty() = size() != 0 54 55/** Removes the entry for [key] only if it is mapped to [value]. */ 56fun <T> SparseArrayCompat<T>.remove(key: Int, value: T): Boolean { 57 val index = indexOfKey(key) 58 if (index != -1 && value == valueAt(index)) { 59 removeAt(index) 60 return true 61 } 62 return false 63} 64 65/** Update this collection by adding or replacing entries from [other]. */ 66fun <T> SparseArrayCompat<T>.putAll(other: SparseArrayCompat<T>) = other.forEach(::put) 67 68/** Performs the given [action] for each key/value entry. */ 69inline fun <T> SparseArrayCompat<T>.forEach(action: (key: Int, value: T) -> Unit) { 70 for (index in 0 until size()) { 71 action(keyAt(index), valueAt(index)) 72 } 73} 74 75/** Return an iterator over the collection's keys. */ 76fun <T> SparseArrayCompat<T>.keyIterator(): IntIterator = object : IntIterator() { 77 var index = 0 78 override fun hasNext() = index < size() 79 override fun nextInt() = keyAt(index++) 80} 81 82/** Return an iterator over the collection's values. */ 83fun <T> SparseArrayCompat<T>.valueIterator(): Iterator<T> = object : Iterator<T> { 84 var index = 0 85 override fun hasNext() = index < size() 86 override fun next() = valueAt(index++) 87} 88