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

org.jetbrains.kotlin.utils.collections.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.utils

import java.util.*

fun  Iterable.keysToMap(value: (K) -> V): Map {
    return associateBy({ it }, value)
}

fun  Iterable.keysToMapExceptNulls(value: (K) -> V?): Map {
    val map = LinkedHashMap()
    for (k in this) {
        val v = value(k)
        if (v != null) {
            map[k] = v
        }
    }
    return map
}

fun  Iterable.mapToIndex(): Map {
    val map = LinkedHashMap()
    for ((index, k) in this.withIndex()) {
        map[k] = index
    }
    return map
}

inline fun  MutableMap.getOrPutNullable(key: K, defaultValue: () -> V): V {
    return if (!containsKey(key)) {
        val answer = defaultValue()
        put(key, answer)
        answer
    } else {
        @Suppress("UNCHECKED_CAST")
        get(key) as V
    }
}

inline fun > C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this

inline fun > M.ifEmpty(body: () -> M): M = if (isEmpty()) body() else this

inline fun  Array.ifEmpty(body: () -> Array): Array = if (isEmpty()) body() else this

fun  MutableCollection.addIfNotNull(t: T?) {
    if (t != null) add(t)
}

suspend fun  SequenceScope.yieldIfNotNull(t: T?) = if (t != null) yield(t) else Unit

fun  newHashMapWithExpectedSize(expectedSize: Int): HashMap =
    HashMap(capacity(expectedSize))

fun  newHashSetWithExpectedSize(expectedSize: Int): HashSet =
    HashSet(capacity(expectedSize))

fun  newLinkedHashMapWithExpectedSize(expectedSize: Int): LinkedHashMap =
    LinkedHashMap(capacity(expectedSize))

fun  newLinkedHashSetWithExpectedSize(expectedSize: Int): LinkedHashSet =
    LinkedHashSet(capacity(expectedSize))

private fun capacity(expectedSize: Int): Int =
    if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1

fun  ArrayList.compact(): List =
    when (size) {
        0 -> emptyList()
        1 -> listOf(first())
        else -> apply { trimToSize() }
    }


/**
 * The same as [org.jetbrains.kotlin.utils.compact] extension function, but it could be used with the
 * immutable list type (without [java.util.ArrayList.trimToSize] for the collections with more than 1 element)
 * @see org.jetbrains.kotlin.utils.compact
 */
fun  List.compactIfPossible(): List =
    when (size) {
        0 -> emptyList()
        1 -> listOf(first())
        else -> apply {
            if (this is ArrayList<*>) trimToSize()
        }
    }

fun  List.indexOfFirst(startFrom: Int, predicate: (T) -> Boolean): Int {
    for (index in startFrom..lastIndex) {
        if (predicate(this[index])) return index
    }
    return -1
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy