Skip to content

Commit 3b59e6a

Browse files
authored
Add precondition functions for InvalidParameterException (#3)
1 parent 9a459f8 commit 3b59e6a

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

library/error/api/error.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public final class org/kotlincrypto/error/PreconditionsKt {
2+
public static final fun requireParam (Z)V
3+
public static final fun requireParam (ZLkotlin/jvm/functions/Function0;)V
4+
}
5+

library/error/api/error.klib.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ open class org.kotlincrypto.error/ShortBufferException : org.kotlincrypto.error/
6767
constructor <init>() // org.kotlincrypto.error/ShortBufferException.<init>|<init>(){}[0]
6868
constructor <init>(kotlin/String?) // org.kotlincrypto.error/ShortBufferException.<init>|<init>(kotlin.String?){}[0]
6969
}
70+
71+
final inline fun org.kotlincrypto.error/requireParam(kotlin/Boolean) // org.kotlincrypto.error/requireParam|requireParam(kotlin.Boolean){}[0]
72+
final inline fun org.kotlincrypto.error/requireParam(kotlin/Boolean, kotlin/Function0<kotlin/Any>) // org.kotlincrypto.error/requireParam|requireParam(kotlin.Boolean;kotlin.Function0<kotlin.Any>){}[0]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
@file:Suppress("KotlinRedundantDiagnosticSuppress", "NOTHING_TO_INLINE")
17+
18+
package org.kotlincrypto.error
19+
20+
import kotlin.contracts.ExperimentalContracts
21+
import kotlin.contracts.contract
22+
23+
/**
24+
* Throws [InvalidParameterException] when [condition] is false.
25+
* */
26+
@OptIn(ExperimentalContracts::class)
27+
public inline fun requireParam(condition: Boolean) {
28+
contract {
29+
returns() implies condition
30+
}
31+
requireParam(condition) { "Parameter check failed." }
32+
}
33+
34+
/**
35+
* Throws [InvalidParameterException] with the result of calling [lazyMessage] when [condition] is false.
36+
* */
37+
@OptIn(ExperimentalContracts::class)
38+
public inline fun requireParam(condition: Boolean, lazyMessage: () -> Any) {
39+
contract {
40+
returns() implies condition
41+
}
42+
if (condition) return
43+
val message = lazyMessage()
44+
throw InvalidParameterException(message.toString())
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2025 Matthew Nelson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
package org.kotlincrypto.error
17+
18+
import kotlin.test.Test
19+
import kotlin.test.assertFailsWith
20+
21+
class PreconditionsUnitTest {
22+
23+
@Test
24+
fun givenRequireParam_whenConditionTrue_thenDoesNotThrow() {
25+
requireParam(true)
26+
requireParam(true) { "fail" }
27+
}
28+
29+
@Test
30+
fun givenRequireParam_whenConditionFalse_thenThrows() {
31+
assertFailsWith<InvalidParameterException> { requireParam(false) }
32+
assertFailsWith<InvalidParameterException> { requireParam(false) { "fail" } }
33+
}
34+
}

0 commit comments

Comments
 (0)