added lazy message versions of require/assert/check methods, #KT-693 fixed

This commit is contained in:
James Strachan
2012-03-27 06:16:21 +01:00
parent 7aef088f7b
commit 5c7ee4d870
2 changed files with 88 additions and 6 deletions
+41 -6
View File
@@ -9,7 +9,7 @@ object Assertions {
* Throws an [[AssertionError]] if the `value` is false and runtime assertions have been
* enabled on the JVM using the `-ea` JVM option.
*/
inline fun assert(value:Boolean):Unit {
inline fun assert(value: Boolean): Unit {
if(Assertions._ENABLED) {
if(!value) {
throw AssertionError();
@@ -21,7 +21,7 @@ inline fun assert(value:Boolean):Unit {
* Throws an [[AssertionError]] with specified `message` if the `value` is false
* and runtime assertions have been enabled on the JVM using the `-ea` JVM option.
*/
inline fun assert(value:Boolean, message:Any) {
inline fun assert(value: Boolean, message: Any) {
if(Assertions._ENABLED) {
if(!value) {
throw AssertionError(message);
@@ -29,10 +29,23 @@ inline fun assert(value:Boolean, message:Any) {
}
}
/**
* Throws an [[AssertionError]] with specified `message` if the `value` is false
* and runtime assertions have been enabled on the JVM using the `-ea` JVM option.
*/
inline fun assert(value: Boolean, lazyMessage: () -> String) {
if(Assertions._ENABLED) {
if(!value) {
val message = lazyMessage()
throw AssertionError(message);
}
}
}
/**
* Throws an [[IllegalArgumentException]] if the `value` is false.
*/
inline fun require(value:Boolean):Unit {
inline fun require(value: Boolean): Unit {
if(!value) {
throw IllegalArgumentException();
}
@@ -41,16 +54,26 @@ inline fun require(value:Boolean):Unit {
/**
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
*/
inline fun require(value:Boolean, message:Any):Unit {
inline fun require(value: Boolean, message: Any): Unit {
if(!value) {
throw IllegalArgumentException(message.toString());
}
}
/**
* Throws an [[IllegalArgumentException]] with specified `message` if the `value` is false.
*/
inline fun require(value: Boolean, lazyMessage: () -> String): Unit {
if(!value) {
val message = lazyMessage()
throw IllegalArgumentException(message.toString());
}
}
/**
* Throws an [[IllegalStateException]] if the `value` is false.
*/
inline fun check(value:Boolean):Unit {
inline fun check(value: Boolean): Unit {
if(!value) {
throw IllegalStateException();
}
@@ -59,10 +82,22 @@ inline fun check(value:Boolean):Unit {
/**
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false.
*/
inline fun check(value:Boolean, message:Any):Unit {
inline fun check(value: Boolean, message: Any): Unit {
if(!value) {
throw IllegalStateException(message.toString());
}
}
/**
* Throws an [[IllegalStateException]] with specified `message` if the `value` is false.
*/
inline fun check(value: Boolean, lazyMessage: () -> String): Unit {
if(!value) {
val message = lazyMessage()
throw IllegalStateException(message.toString());
}
}