diff --git a/libraries/stdlib/src/kotlin/nullable/Nullables.kt b/libraries/stdlib/src/kotlin/nullable/Nullables.kt index 9a1ef16a495..c5641b10ce0 100644 --- a/libraries/stdlib/src/kotlin/nullable/Nullables.kt +++ b/libraries/stdlib/src/kotlin/nullable/Nullables.kt @@ -3,40 +3,40 @@ package kotlin.nullable import java.util.* /** Returns true if the element is not null and matches the given predicate */ -public inline fun T?.any(predicate: (T)-> Boolean): Boolean { +public inline fun T?.any(predicate: (T)-> Boolean): Boolean { return this != null && predicate(this) } /** Returns true if the element is not null and matches the given predicate */ -public inline fun T?.all(predicate: (T)-> Boolean): Boolean { +public inline fun T?.all(predicate: (T)-> Boolean): Boolean { return this != null && predicate(this) } /** Returns the 1 if the element is not null else 0 */ -public inline fun T?.count(predicate: (T)-> Boolean): Int { +public inline fun T?.count(predicate: (T)-> Boolean): Int { return if (this != null) 1 else 0 } /** Returns the first item which matches the predicate if this element is not null else null */ -public inline fun T?.find(predicate: (T)-> Boolean): T? { +public inline fun T?.find(predicate: (T)-> Boolean): T? { return if (this != null && predicate(this)) this else null } /** Returns a new List containing all elements in this collection which match the given predicate */ -public inline fun T?.filter(predicate: (T)-> Boolean): T? = find(predicate) +public inline fun T?.filter(predicate: (T)-> Boolean): T? = find(predicate) /** Filters all elements in this collection which match the given predicate into the given result collection */ -public inline fun > T?.filterTo(result: C, predicate: (T)-> Boolean): C { +public inline fun > T?.filterTo(result: C, predicate: (T)-> Boolean): C { if (this != null && predicate(this)) result.add(this) return result } /** Returns a List containing all the non null elements in this collection */ -public inline fun T?.filterNotNull(): Collection = filterNotNullTo(java.util.ArrayList()) +public inline fun T?.filterNotNull(): Collection = filterNotNullTo(java.util.ArrayList()) /** Filters all the null elements in this collection winto the given result collection */ -public inline fun > T?.filterNotNullTo(result: C): C { +public inline fun > T?.filterNotNullTo(result: C): C { if (this != null) { result.add(this) } @@ -44,10 +44,10 @@ public inline fun > T?.filterNotNullTo(result: C): } /** Returns a new collection containing all elements in this collection which do not match the given predicate */ -public inline fun T?.filterNot(predicate: (T)-> Boolean): Collection = filterNotTo(ArrayList(), predicate) +public inline fun T?.filterNot(predicate: (T)-> Boolean): Collection = filterNotTo(ArrayList(), predicate) /** Returns a new collection containing all elements in this collection which do not match the given predicate */ -public inline fun > T?.filterNotTo(result: C, predicate: (T)-> Boolean): C { +public inline fun > T?.filterNotTo(result: C, predicate: (T)-> Boolean): C { if (this != null && !predicate(this)) { result.add(this) } @@ -58,7 +58,7 @@ public inline fun > T?.filterNotTo(result: C, pred * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection */ -public inline fun T?.flatMap(transform: (T)-> MutableCollection): Collection { +public inline fun T?.flatMap(transform: (T)-> MutableCollection): Collection { return flatMapTo(ArrayList(), transform) } @@ -66,7 +66,7 @@ public inline fun T?.flatMap(transform: (T)-> MutableCollection): Coll * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection */ -public inline fun T?.flatMapTo(result: MutableCollection, transform: (T)-> MutableCollection): Collection { +public inline fun T?.flatMapTo(result: MutableCollection, transform: (T)-> MutableCollection): Collection { if (this != null) { val coll = transform(this) if (coll != null) { @@ -79,7 +79,7 @@ public inline fun T?.flatMapTo(result: MutableCollection, transform: ( } /** Performs the given operation on each element inside the collection */ -public inline fun T?.forEach(operation: (element: T) -> Unit) { +public inline fun T?.forEach(operation: (element: T) -> Unit) { if (this != null) { operation(this) } @@ -91,7 +91,7 @@ public inline fun T?.forEach(operation: (element: T) -> Unit) { * For example to sum together all numeric values in a collection of numbers it would be * {code}val total = numbers.fold(0){(a, b) -> a + b}{code} */ -public inline fun T?.fold(initial: T, operation: (it: T, it2: T) -> T): T { +public inline fun T?.fold(initial: T, operation: (it: T, it2: T) -> T): T { return if (this != null) { operation(initial, this) } else { @@ -102,7 +102,7 @@ public inline fun T?.fold(initial: T, operation: (it: T, it2: T) -> T): T { /** * Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values */ -public inline fun T?.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { +public inline fun T?.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { // maximum size is 1 so reverse is not needed return fold(initial, {x, y -> operation(y, x)}) } @@ -111,7 +111,7 @@ public inline fun T?.foldRight(initial: T, operation: (it: T, it2: T) -> T): * Iterates through the collection performing the transformation on each element and using the result * as the key in a map to group elements by the result */ -public inline fun T?.groupBy(result: MutableMap> = HashMap>(), toKey: (T)-> K): Map> { +public inline fun T?.groupBy(result: MutableMap> = HashMap>(), toKey: (T)-> K): Map> { if (this != null) { val key = toKey(this) val list = result.getOrPut(key){ ArrayList() } @@ -122,7 +122,7 @@ public inline fun T?.groupBy(result: MutableMap> = Hash /** Creates a String from the nullable or item with the given prefix and postfix if supplied */ -public inline fun T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String { +public inline fun T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String { val buffer = StringBuilder(prefix) var first = true if (this != null) { @@ -134,7 +134,7 @@ public inline fun T?.makeString(separator: String = ", ", prefix: String = " /** Returns the nullable result of transforming this with the given transformation function */ -public inline fun T?.map(transform : (T) -> R) : R? { +public inline fun T?.map(transform : (T) -> R) : R? { return if (this != null) { transform(this) } else { @@ -143,7 +143,7 @@ public inline fun T?.map(transform : (T) -> R) : R? { } /** Transforms each element of this collection with the given function then adds the results to the given collection */ -public inline fun > T?.mapTo(result: C, transform : (T) -> R) : C { +public inline fun > T?.mapTo(result: C, transform : (T) -> R) : C { if (this != null) { result.add(transform(this)) } @@ -151,28 +151,28 @@ public inline fun > T?.mapTo(result: C, transfo } /** Returns itself since it can't be reversed as it can contain at most one item */ -public inline fun T?.reverse(): T? { +public inline fun T?.reverse(): T? { return this } /** Copies the collection into the given collection */ -public inline fun > T?.toCollection(result: C): C { +public inline fun > T?.toCollection(result: C): C { if (this != null) result.add(this) return result } /** Converts the collection into a LinkedList */ -public inline fun T?.toLinkedList(): LinkedList = this.toCollection(LinkedList()) +public inline fun T?.toLinkedList(): LinkedList = this.toCollection(LinkedList()) /** Converts the collection into a List */ -public inline fun T?.toList(): List = this.toCollection(ArrayList()) +public inline fun T?.toList(): List = this.toCollection(ArrayList()) /** Converts the collection into a Set */ -public inline fun T?.toSet(): Set = this.toCollection(HashSet()) +public inline fun T?.toSet(): Set = this.toCollection(HashSet()) /** Converts the collection into a SortedSet */ -public inline fun T?.toSortedSet(): SortedSet = this.toCollection(TreeSet()) +public inline fun T?.toSortedSet(): SortedSet = this.toCollection(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun T?.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List {