All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.hnau.jutils.collections.ListUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package ru.hnau.jutils.collections


fun  List.circledGet(pos: Int): T {
    val size = this.size
    val normalizedPos = (pos % size).let { if (it < 0) it + size else it }
    return get(normalizedPos)
}

//Для случая, когда список может быть пустым
fun  List.circledGetOrNull(pos: Int) =
        if (this.isEmpty()) null else circledGet(pos)

fun  List.getOrFirst(pos: Int): T = getOrNull(pos) ?: get(0)

fun  List.getOrFirstOrNull(pos: Int): T? =
        if (this.isEmpty()) null else getOrNull(pos)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy