[utils] Move atMostOne to core/util.runtime

There is nothing backend-specific about this helper function.
This commit is contained in:
Sergej Jaskiewicz
2023-08-22 21:07:15 +02:00
committed by teamcity
parent fa600a58ac
commit 56cb0bf071
15 changed files with 44 additions and 29 deletions
@@ -1,16 +0,0 @@
/*
* Copyright 2000-2019 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.backend.common
fun <T> Collection<T>.atMostOne(): T? {
return when (this.size) {
0 -> null
1 -> this.iterator().next()
else -> throw IllegalArgumentException("Collection has more than one element.")
}
}
inline fun <T> Iterable<T>.atMostOne(predicate: (T) -> Boolean): T? = this.filter(predicate).atMostOne()
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.atMostOne
class SimpleMemberScope(val members: List<DeclarationDescriptor>) : MemberScopeImpl() {