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
@@ -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")
}