Add requireNotNull with lazy message.

This commit is contained in:
Ilya Gorbunov
2015-03-27 14:27:25 +03:00
committed by Ilya Gorbunov
parent 2f02024c24
commit 0717705f15
2 changed files with 31 additions and 1 deletions
@@ -41,6 +41,21 @@ public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was
}
}
/**
* 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.
*
@@ -65,7 +80,7 @@ public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
}
/**
* Throws an [IllegalArgumentException] with the given [message] if the [value] is null. Otherwise
* Throws an [IllegalStateException] with the given [message] if the [value] is null. Otherwise
* returns the not null value.
*
* @sample test.collections.PreconditionsTest.checkNotNull