/
Preconditions.kt
101 lines (92 loc) · 3.03 KB
/
Preconditions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package kotlin
// TODO should not need this - its here for the JS stuff
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
/**
* Throws an [IllegalArgumentException] with an optional [message] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingRequireWithMessage
*/
public fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
if (!value) {
throw IllegalArgumentException(message.toString())
}
}
/**
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingRequireWithLazyMessage
*/
public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
throw IllegalArgumentException(message.toString())
}
}
/**
* Throws an [IllegalArgumentException] with the given [message] if the [value] is null. Otherwise
* returns the not null value.
*
* @sample test.collections.PreconditionsTest.requireNotNull
*/
public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) {
throw IllegalArgumentException(message.toString())
} else {
return value
}
}
/**
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
* returns the not null value.
*
* @sample test.collections.PreconditionsTest.requireNotNullWithLazyMessage
*/
public fun <T:Any> requireNotNull(value: T?, lazyMessage: () -> Any): T {
if (value == null) {
val message = lazyMessage()
throw IllegalArgumentException(message.toString())
} else {
return value
}
}
/**
* Throws an [IllegalStateException] with an optional [message] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingCheckWithMessage
*/
public fun check(value: Boolean, message: Any = "Check failed"): Unit {
if (!value) {
throw IllegalStateException(message.toString())
}
}
/**
* Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingCheckWithLazyMessage
*/
public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
throw IllegalStateException(message.toString())
}
}
/**
* Throws an [IllegalStateException] with the given [message] if the [value] is null. Otherwise
* returns the not null value.
*
* @sample test.collections.PreconditionsTest.checkNotNull
*/
public fun <T:Any> checkNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) {
throw IllegalStateException(message.toString())
} else {
return value
}
}
/**
* Throws an [IllegalStateException] with the given [message]
*
* @sample test.collections.PreconditionsTest.error
*/
public fun error(message: Any): Nothing = throw IllegalStateException(message.toString())