Last active
December 17, 2019 14:48
-
-
Save Aidanvii7/97bc6804699ff6a9d728d81ae0a7b83c to your computer and use it in GitHub Desktop.
A collection of extension functions for Android's SparseArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.aidanvii.extensions | |
import android.util.SparseArray | |
import java.util.* | |
val SparseArray<*>.max: Int get () = size() - 1; | |
inline fun <V> SparseArray<V>.forEachValue(action: (V) -> Unit): SparseArray<V> { | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
action(value) | |
} | |
return this | |
} | |
inline fun <V> SparseArray<V>.forEachPair(action: (Int, V) -> Unit): SparseArray<V> { | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
action(key, value) | |
} | |
return this | |
} | |
inline fun <V> SparseArray<V>.forEachValueThenRemove(action: (V) -> Unit): SparseArray<V> { | |
val toRemove = ArrayList<Int>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
action(value) | |
toRemove.add(key) | |
} | |
toRemove.forEach { remove(it) } | |
return this | |
} | |
inline fun <V> SparseArray<V>.forEachPairThenRemove(action: (Int, V) -> Unit): SparseArray<V> { | |
val toRemove = ArrayList<Int>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
action(key, value) | |
toRemove.add(key) | |
} | |
toRemove.forEach { remove(it) } | |
return this | |
} | |
inline fun <V> SparseArray<V>.forEachValueThenMaybeRemove(action: (V) -> Boolean): SparseArray<V> { | |
val toRemove = ArrayList<Int>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
val removeEntry = action(value) | |
if (removeEntry) { | |
toRemove.add(key) | |
} | |
} | |
toRemove.forEach { remove(it) } | |
return this | |
} | |
inline fun <V> SparseArray<V>.forEachPairThenMaybeRemove(action: (Int, V) -> Boolean): SparseArray<V> { | |
val toRemove = ArrayList<Int>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
val removeEntry = action(key, value) | |
if (removeEntry) { | |
toRemove.add(key) | |
} | |
} | |
toRemove.forEach { remove(it) } | |
return this | |
} | |
inline fun <V> SparseArray<V>.filterOnKey(filterPredicate: (Int) -> Boolean): SparseArray<V> { | |
val filtered = SparseArray<V>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
if (filterPredicate(key)) { | |
filtered.put(key, value) | |
} | |
} | |
return filtered | |
} | |
inline fun <V> SparseArray<V>.filterOnValue(filterPredicate: (V) -> Boolean): SparseArray<V> { | |
val filtered = SparseArray<V>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
if (filterPredicate(value)) { | |
filtered.put(key, value) | |
} | |
} | |
return filtered | |
} | |
inline fun <V> SparseArray<V>.filterOnPair(filterPredicate: (Int, V) -> Boolean): SparseArray<V> { | |
val filtered = SparseArray<V>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
if (filterPredicate(key, value)) { | |
filtered.put(key, value) | |
} | |
} | |
return filtered | |
} | |
inline fun <V1, V2> SparseArray<V1>.map(mapper: (V1) -> V2): SparseArray<V2> { | |
val mapped = SparseArray<V2>() | |
for (index in 0..max) { | |
val key = keyAt(index) | |
val value = get(key) | |
mapped.put(key, mapper(value)) | |
} | |
return mapped | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment