From 0dd7127aa7258d8fd9d434559750c00ca49f66e6 Mon Sep 17 00:00:00 2001 From: Filip Krzywka Date: Wed, 10 Apr 2019 11:36:48 +0200 Subject: Extract transforming logic from validator Change-Id: Ic019b1796e17d24f14f41a817af6e5ecd8c7244b Issue-ID: DCAEGEN2-1416 Signed-off-by: Filip Krzywka --- .../org/onap/dcae/collectors/veshv/utils/arrow/core.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'sources/hv-collector-utils/src/main/kotlin/org/onap/dcae') diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt index cfed7f32..ceae62db 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt @@ -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 binding(c: suspend MonadContinuation.() -> A) : Option = Option.monad().binding(c).fix() @@ -78,6 +82,17 @@ fun Try.doOnFailure(action: (Throwable) -> Unit): Try = apply { fun A.mapBinding(c: suspend MonadContinuation.(A) -> B) : Option = let { OptionUtils.binding { c(it) } } +fun Option.flatFold(ifEmptyOrFalse: () -> T, ifTrue: () -> T) = + fold({ + ifEmptyOrFalse() + }, { + if (it) { + ifTrue() + } else { + ifEmptyOrFalse() + } + }) + -- cgit 1.2.3-korg