🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / utils / Booleans.kt
index 3202b82..1d3e097 100644 (file)
@@ -4,3 +4,24 @@ package net.pterodactylus.sone.utils
  * Returns the value of [block] if `this` is true, returns `null` otherwise.
  */
 fun <R> Boolean.ifTrue(block: () -> R): R? = if (this) block() else null
+
+/**
+ * Returns the value of [block] if `this` is false, returns `null` otherwise.
+ */
+fun <R> Boolean.ifFalse(block: () -> R): R? = if (!this) block() else null
+
+/**
+ * Returns `this` but runs the given block if `this`  is `true`.
+ *
+ * @param block The block to run if `this` is `true`
+ * @return `this`
+ */
+fun Boolean.onTrue(block: () -> Unit): Boolean = also { if (this) block() }
+
+/**
+ * Returns `this` but runs the given block if `this`  is `false`.
+ *
+ * @param block The block to run if `this` is `false`
+ * @return `this`
+ */
+fun Boolean.onFalse(block: () -> Unit): Boolean = this.also { if (!this) block() }