forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guards.kt
39 lines (35 loc) · 1.26 KB
/
Guards.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Copyright 2010-2018 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 templates
import templates.Family.*
import templates.SequenceClass.*
object Guards : TemplateGroupBase() {
private val THIS = "\$this"
val f_requireNoNulls = fn("requireNoNulls()") {
include(Iterables, Sequences, InvariantArraysOfObjects, Lists)
} builder {
doc { "Returns an original collection containing all the non-`null` elements, throwing an [IllegalArgumentException] if there are any `null` elements." }
sequenceClassification(intermediate, stateless)
typeParam("T : Any")
toNullableT = true
returns("SELF")
body {
"""
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $THIS.")
}
}
@Suppress("UNCHECKED_CAST")
return this as SELF
"""
}
body(Sequences) {
"""
return map { it ?: throw IllegalArgumentException("null element found in $THIS.") }
"""
}
}
}