Extract transforming logic from validator
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / utils / arrow / core.kt
index cfed7f3..ceae62d 100644 (file)
@@ -17,6 +17,9 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
+@file:Suppress("TooManyFunctions")
+
 package org.onap.dcae.collectors.veshv.utils.arrow
 
 import arrow.core.Either
@@ -37,6 +40,7 @@ import java.util.concurrent.atomic.AtomicReference
  * @since July 2018
  */
 
+
 object OptionUtils {
     fun <A> binding(c: suspend MonadContinuation<ForOption, *>.() -> A)
             : Option<A> = Option.monad().binding(c).fix()
@@ -78,6 +82,17 @@ fun <A> Try<A>.doOnFailure(action: (Throwable) -> Unit): Try<A> = apply {
 fun <A, B> A.mapBinding(c: suspend MonadContinuation<ForOption, *>.(A) -> B)
         : Option<B> = let { OptionUtils.binding { c(it) } }
 
+fun <T> Option<Boolean>.flatFold(ifEmptyOrFalse: () -> T, ifTrue: () -> T) =
+        fold({
+            ifEmptyOrFalse()
+        }, {
+            if (it) {
+                ifTrue()
+            } else {
+                ifEmptyOrFalse()
+            }
+        })
+