diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/Sugar.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/Sugar.kt new file mode 100644 index 00000000..2b2d9582 --- /dev/null +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/Sugar.kt @@ -0,0 +1,69 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot + +/** + * Variant of [let] which takes an additional [predicate] and returns the result of applying + * [block] iff [predicate] evaluates to true, otherwise it returns the value unchanged. + * + * @param predicate boolean + * @param block function block that is applied to [this] iff [predicate] evaluates to true + * @return the result of applying [block] to [this] iff [predicate] evaluates to true, [this] otherwise + * @receiver [this] + */ +fun T.letIf(predicate: T.() -> Boolean, block: T.() -> T): T = + let { + if (predicate(this)) + block(this) + else + this + } + +/** + * Variant of [let] which takes an additional [condition] and returns the result of applying + * [block] iff [condition] is true, otherwise it returns the value unchanged. + * + * @param condition boolean + * @param block function block that is applied to [this] iff [condition] is true + * @return the result of applying [block] to [this] iff [condition] is true, [this] otherwise + * @receiver [this] + */ +fun T.letIf(condition: Boolean, block: T.() -> T): T = + let { + if (condition) + block(this) + else + this + } + +/** + * Variant of [apply] which takes an additional [predicate] and only applies the [block] + * iff [predicate] evaluates to true. + * + * @param predicate predicate on [this] + * @param block function block that is applied to [this] iff [predicate] evaluates to true + * @return the result of applying [block] to this iff [predicate] evaluates to true, otherwise [this] unchanged + * @receiver [this] + */ +fun T.applyIf(predicate: T.() -> Boolean, block: T.() -> Unit): T = + apply { + if (predicate(this)) + block(this) + } + +/** + * Variant of [apply] which takes an additional [condition] and only applies the [block] + * iff the [condition] is true. + * + * @param condition boolean + * @param block function block that is applied to [this] iff [condition] is true + * @return the result of applying [block] to this iff [condition] is true, otherwise [this] unchanged + * @receiver [this] + */ +fun T.applyIf(condition: Boolean, block: T.() -> Unit): T = + apply { + if (condition) + block(this) + } \ No newline at end of file diff --git a/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/SugarTest.kt b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/SugarTest.kt new file mode 100644 index 00000000..30883dfd --- /dev/null +++ b/wot-dijkstra/src/test/kotlin/org/pgpainless/wot/SugarTest.kt @@ -0,0 +1,67 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.wot + +import kotlin.test.Test +import kotlin.test.assertEquals + +class SugarTest { + + @Test + fun `test letIf(predicate, block) with a predicate that evaluates to true`() { + assertEquals("Hello, PGP!", "Hello, GPG!".letIf({ contains("GPG") }) { + replace("GPG", "PGP") + }) + } + + @Test + fun `test letIf(predicate, block) with a predicate that evaluates to false`() { + assertEquals("Hello, PGP!", "Hello, PGP!".letIf({ contains("GPG")}) { + replace("Hello", "Salut") + }) + } + + @Test + fun `test letIf(condition, block) with true condition`() { + assertEquals("hello, pgp!", "Hello, PGP!".letIf(true) { + lowercase() + }) + } + + @Test + fun `test letIf(condition, block) with false condition`() { + assertEquals("Hello, PGP!", "Hello, PGP!".letIf(false) { + lowercase() + }) + } + + @Test + fun `test applyIf(predicate, block) with predicate that evaluates to true`() { + assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B").applyIf({ !contains("C") }) { + add("C") + }) + } + + @Test + fun `test applyIf(predicate, block) with predicate that evaluates to false`() { + assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B", "C").applyIf({ !contains("C")}) { + add("C") + }) + } + + @Test + fun `test applyIf(condition, block) with true condition`() { + assertEquals(listOf("A", "B", "C"), mutableListOf("A", "B").applyIf(true) { + add("C") + }) + } + + @Test + fun `test applyIf(condition, block) with false condition`() { + assertEquals(listOf("A", "B"), mutableListOf("A", "B").applyIf(false) { + add("C") + }) + } +} \ No newline at end of file