Added firstOrNullIsInstance and firstIsInstance methods

This commit is contained in:
Valentin Kipyatkov
2014-11-19 22:33:24 +03:00
parent 46c393d2db
commit a0c9c0e7b2
6 changed files with 46 additions and 7 deletions
@@ -24,12 +24,12 @@ import org.jetbrains.jet.lang.psi.JetPsiFactory
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.PsiErrorElement
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
fun getTypeReference(declaration: JetCallableDeclaration): JetTypeReference? {
return declaration.getFirstChild()!!.siblings(forward = true)
.dropWhile { it.getNode()!!.getElementType() != JetTokens.COLON }
.filterIsInstance(javaClass<JetTypeReference>())
.firstOrNull()
.firstOrNullIsInstance<JetTypeReference>()
}
fun setTypeReference(declaration: JetCallableDeclaration, addAfter: PsiElement?, typeRef: JetTypeReference?): JetTypeReference? {
@@ -32,9 +32,9 @@ import org.jetbrains.jet.lang.resolve.TypeResolver.FlexibleTypeCapabilitiesProvi
import kotlin.platform.platformStatic
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.jet.context.LazinessToken
import javax.inject.Inject
import org.jetbrains.jet.lang.resolve.lazy.LazyEntity
import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil
import org.jetbrains.jet.utils.addToStdlib.firstOrNullIsInstance
public class TypeResolver(
private val annotationResolver: AnnotationResolver,
@@ -276,7 +276,7 @@ public class TypeResolver(
public fun resolveClass(scope: JetScope, userType: JetUserType, trace: BindingTrace): ClassifierDescriptor? {
val classifierDescriptor = qualifiedExpressionResolver.lookupDescriptorsForUserType(userType, scope, trace)
.filterIsInstance(javaClass<ClassifierDescriptor>()).firstOrNull()
.firstOrNullIsInstance<ClassifierDescriptor>()
if (classifierDescriptor != null) {
ImportsResolver.reportPlatformClassMappedToKotlin(moduleDescriptor, trace, userType, classifierDescriptor)
}
@@ -18,6 +18,7 @@ package org.jetbrains.jet.utils.addToStdlib
import java.util.HashMap
import java.util.Collections
import java.util.NoSuchElementException
deprecated("Replace with filterKeys when bootstrapped")
public fun <K, V> Map<K, V>.filterKeys_tmp(predicate: (K)->Boolean): Map<K, V> {
@@ -33,3 +34,39 @@ public fun <K, V> Map<K, V>.filterKeys_tmp(predicate: (K)->Boolean): Map<K, V> {
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
public fun <T: Any> T?.singletonOrEmptySet(): Set<T> = if (this != null) Collections.singleton(this) else Collections.emptySet()
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Stream<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Iterable<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T : Any> Array<*>.firstOrNullIsInstance(): T? {
for (element in this) if (element is T) return element
return null
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Stream<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Iterable<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}
[suppress("NOTHING_TO_INLINE")]
public inline fun <reified T> Array<*>.firstIsInstance(): T {
for (element in this) if (element is T) return element
throw NoSuchElementException("No element of given type found")
}