Refactor extensions
parent
6934daecff
commit
be67b36b6a
@ -1,34 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.utils.ext
|
|
||||||
|
|
||||||
import androidx.core.os.LocaleListCompat
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
fun LocaleListCompat.getOrThrow(index: Int) = get(index) ?: throw kotlin.NoSuchElementException()
|
|
||||||
|
|
||||||
fun LocaleListCompat.toList(): List<Locale> = createList(size()) { i -> getOrThrow(i) }
|
|
||||||
|
|
||||||
operator fun LocaleListCompat.iterator() = object : Iterator<Locale> {
|
|
||||||
private var index = 0
|
|
||||||
override fun hasNext(): Boolean = index < size()
|
|
||||||
override fun next(): Locale = getOrThrow(index++)
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <R, C : MutableCollection<in R>> LocaleListCompat.mapTo(
|
|
||||||
destination: C,
|
|
||||||
block: (Locale) -> R,
|
|
||||||
): C {
|
|
||||||
val len = size()
|
|
||||||
for (i in 0 until len) {
|
|
||||||
val item = get(i) ?: continue
|
|
||||||
destination.add(block(item))
|
|
||||||
}
|
|
||||||
return destination
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <T> LocaleListCompat.map(block: (Locale) -> T): List<T> {
|
|
||||||
return mapTo(ArrayList(size()), block)
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <T> LocaleListCompat.mapToSet(block: (Locale) -> T): Set<T> {
|
|
||||||
return mapTo(LinkedHashSet(size()), block)
|
|
||||||
}
|
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
package org.koitharu.kotatsu.utils.ext
|
||||||
|
|
||||||
|
import androidx.core.os.LocaleListCompat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
operator fun LocaleListCompat.iterator(): ListIterator<Locale> = LocaleListCompatIterator(this)
|
||||||
|
|
||||||
|
fun LocaleListCompat.toList(): List<Locale> = List(size()) { i -> getOrThrow(i) }
|
||||||
|
|
||||||
|
inline fun <T> LocaleListCompat.map(block: (Locale) -> T): List<T> {
|
||||||
|
return List(size()) { i -> block(getOrThrow(i)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> LocaleListCompat.mapToSet(block: (Locale) -> T): Set<T> {
|
||||||
|
return Set(size()) { i -> block(getOrThrow(i)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun LocaleListCompat.getOrThrow(index: Int) = get(index) ?: throw NoSuchElementException()
|
||||||
|
|
||||||
|
private class LocaleListCompatIterator(private val list: LocaleListCompat) : ListIterator<Locale> {
|
||||||
|
|
||||||
|
private var index = 0
|
||||||
|
|
||||||
|
override fun hasNext() = index < list.size()
|
||||||
|
|
||||||
|
override fun hasPrevious() = index > 0
|
||||||
|
|
||||||
|
override fun next() = list.get(index++) ?: throw NoSuchElementException()
|
||||||
|
|
||||||
|
override fun nextIndex() = index
|
||||||
|
|
||||||
|
override fun previous() = list.get(--index) ?: throw NoSuchElementException()
|
||||||
|
|
||||||
|
override fun previousIndex() = index - 1
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue