|
| 1 | +package ch.tutteli.kbox |
| 2 | + |
| 3 | +import kotlin.contracts.ExperimentalContracts |
| 4 | +import kotlin.contracts.InvocationKind |
| 5 | +import kotlin.contracts.contract |
| 6 | + |
| 7 | +/** |
| 8 | + * Delegates to [let] in case the given [predicate] holds, returns `this` otherwise. |
| 9 | + * |
| 10 | + * This function shall complement [let] for cases such as invoking a builder method conditionally which without [letIf] |
| 11 | + * would be written as follows `if (predicate) t.let { ... } else t` |
| 12 | + * |
| 13 | + * @since 3.2.0 |
| 14 | + */ |
| 15 | +@OptIn(ExperimentalContracts::class) |
| 16 | +inline fun <T : U, R : U, U> T.letIf(predicate: Boolean, block: (T) -> R): U { |
| 17 | + contract { |
| 18 | + callsInPlace(block, InvocationKind.AT_MOST_ONCE) |
| 19 | + //TODO add `predicate holds in block` once we use Kotlin 2.2.20 |
| 20 | + } |
| 21 | + return if (predicate) let(block) else this |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Delegates to [run] in case the given [predicate] holds, returns `this` otherwise. |
| 26 | + * |
| 27 | + * This function shall complement [run] for cases such as invoking a builder method conditionally which without [runIf] |
| 28 | + * would be written as follows `if (predicate) t.run { ... } else t` |
| 29 | + * |
| 30 | + * @since 3.2.0 |
| 31 | + */ |
| 32 | +@OptIn(ExperimentalContracts::class) |
| 33 | +inline fun <T : U, R : U, U> T.runIf(predicate: Boolean, block: T.() -> R): U { |
| 34 | + contract { |
| 35 | + callsInPlace(block, InvocationKind.AT_MOST_ONCE) |
| 36 | + //TODO add `predicate holds in block` once we use Kotlin 2.2.20 |
| 37 | + } |
| 38 | + return if (predicate) run(block) else this |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Delegates to [also] in case the given [predicate] holds, returns `this` otherwise. |
| 43 | + * |
| 44 | + * This function shall complement [also] and make simple conditional based side effects more readable. |
| 45 | + * |
| 46 | + * @since 3.2.0 |
| 47 | + */ |
| 48 | +@OptIn(ExperimentalContracts::class) |
| 49 | +inline fun <T> T.alsoIf(predicate: Boolean, block: (T) -> Unit): T { |
| 50 | + contract { |
| 51 | + callsInPlace(block, InvocationKind.AT_MOST_ONCE) |
| 52 | + //TODO add `predicate holds in block` once we use Kotlin 2.2.20 |
| 53 | + } |
| 54 | + return if (predicate) also(block) else this |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Delegates to [apply] in case the given [predicate] holds, returns `this` otherwise. |
| 59 | + * |
| 60 | + * This function shall complement [apply] and make simple conditional based side effects more readable. |
| 61 | + * |
| 62 | + * @since 3.2.0 |
| 63 | + */ |
| 64 | +@OptIn(ExperimentalContracts::class) |
| 65 | +inline fun <T> T.applyIf(predicate: Boolean, block: T.() -> Unit): T { |
| 66 | + contract { |
| 67 | + callsInPlace(block, InvocationKind.AT_MOST_ONCE) |
| 68 | + //TODO add `predicate holds in block` once we use Kotlin 2.2.20 |
| 69 | + } |
| 70 | + return if (predicate) apply(block) else this |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Returns `null` if the [predicate] holds, calls the [provider] otherwise. |
| 75 | + * |
| 76 | + * This function shall complement [kotlin.takeUnless] for cases where the cost to call the provider |
| 77 | + * is high or the code involved to define the provider is long and a postfix `takeUnless` is less readable |
| 78 | + * than stating it at the beginning. |
| 79 | + * |
| 80 | + * @since 3.2.0 |
| 81 | + */ |
| 82 | +//TODO move to xIf with 4.0.0 |
| 83 | +@OptIn(ExperimentalContracts::class) |
| 84 | +inline fun <R> takeUnless(predicate: Boolean, provider: () -> R): R? { |
| 85 | + contract { |
| 86 | + callsInPlace(provider, InvocationKind.AT_MOST_ONCE) |
| 87 | + //TODO add `predicate holds in block` once we use Kotlin 2.2.20 |
| 88 | + } |
| 89 | + return if (predicate.not()) provider() else null |
| 90 | +} |
| 91 | + |
0 commit comments