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

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

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.utils

inline fun > Iterable<*>.filterIsInstanceAndTo(destination: C, predicate: (R) -> Boolean): C {
    for (element in this) {
        if (element is R && predicate(element)) {
            destination.add(element)
        }
    }
    return destination
}

inline fun > Iterable<*>.filterIsInstanceMapTo(destination: C, transform: (T) -> R): C {
    for (element in this) {
        if (element is T) {
            destination.add(transform(element))
        }
    }
    return destination
}

inline fun  Collection<*>.filterIsInstanceMapNotNull(transform: (T) -> R?): Collection {
    if (isEmpty()) return emptyList()
    return filterIsInstanceMapNotNullTo(SmartList(), transform)
}

inline fun  Collection<*>.filterIsInstanceAnd(predicate: (R) -> Boolean): List {
    if (isEmpty()) return emptyList()
    return filterIsInstanceAndTo(SmartList(), predicate)
}

inline fun > Iterable<*>.filterIsInstanceMapNotNullTo(
    destination: C,
    transform: (T) -> R?
): C {
    for (element in this) {
        if (element is T) {
            val result = transform(element)
            if (result != null) {
                destination.add(result)
            }
        }
    }
    return destination
}

/**
 * Returns the single element of the collection if it contains at most one element.
 *
 * If the collection is empty, returns `null`.
 *
 * If the collection contains exactly one element, returns that element.
 *
 * If the collection contains more than one element, throws an exception.
 */
fun  Collection.atMostOne(): T? {
    return when (size) {
        0 -> null
        1 -> this.iterator().next()
        else -> throw IllegalArgumentException("Collection has more than one element.")
    }
}

/**
 * Returns at most one element from the iterable that satisfies the given predicate.
 *
 * If there are no elements that satisfy [predicate], returns `null`.
 *
 * If there is exactly one element that satisfies [predicate], returns that element.
 *
 * If there are more such elements, throws an exception.
 */
inline fun  Iterable.atMostOne(predicate: (T) -> Boolean): T? = this.filter(predicate).atMostOne()

fun  MutableMap>.putToMultiMap(key: K, value: V) {
    val list = getOrPut(key) { mutableListOf() }
    list.add(value)
}

fun  Collection.closure(preserveOrder: Boolean = false, f: (T) -> Collection): Collection {
    if (size == 0) return this

    val result = if (preserveOrder) LinkedHashSet(this) else HashSet(this)
    var elementsToCheck = result
    var oldSize = 0
    while (result.size > oldSize) {
        oldSize = result.size
        val toAdd = if (preserveOrder) linkedSetOf() else hashSetOf()
        elementsToCheck.forEach { toAdd.addAll(f(it)) }
        result.addAll(toAdd)
        elementsToCheck = toAdd
    }

    return result
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy