From: Robert Varga Date: Sat, 21 Apr 2018 13:26:09 +0000 (+0200) Subject: Add CheckedValue future bridge methods X-Git-Tag: v2.0.4~10 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=ff9f0636054cd491dd858a359f88385a8fb10ef7;p=yangtools.git Add CheckedValue future bridge methods This patch adds convenience methods to convert a CheckedValue into a immediately-complete {Completable,Fluent}Future as well as bridge methods to complete existing {Completable,Settable}Futures with the contained value. Change-Id: I9abf3c76b62e591f83b5a219bea434429053164e Signed-off-by: Robert Varga --- diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/CheckedValue.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/CheckedValue.java index 413ef742c3..96323875ff 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/CheckedValue.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/CheckedValue.java @@ -10,6 +10,11 @@ package org.opendaylight.yangtools.concepts; import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; +import com.google.common.util.concurrent.FluentFuture; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -168,6 +173,12 @@ public final class CheckedValue extends Variant { return isFirst() ? first() : supplier.get(); } + /** + * Return contained value if present or throw the exception alternative. + * + * @return Contained value + * @throws E When there is no contained value + */ public T orElseThrow() throws E { if (isFirst()) { return first(); @@ -175,6 +186,14 @@ public final class CheckedValue extends Variant { throw second(); } + /** + * Return contained value if present or throw the exception alternative mapped through provided mapper. + * + * @param exceptionMapper Exception mapper + * @return Contained value + * @throws NullPointerException if {@code exceptionMapper} is null + * @throws X When there is no contained value + */ public T orElseThrow(final Function exceptionMapper) throws X { requireNonNull(exceptionMapper); if (isFirst()) { @@ -183,6 +202,14 @@ public final class CheckedValue extends Variant { throw exceptionMapper.apply(second()); } + /** + * Return contained value if present or throw the exception supplied by supplier. + * + * @param supplier Exception supplier + * @return Contained value + * @throws NullPointerException if {@code exceptionMapper} is null + * @throws X When there is no contained value + */ public T orElseThrow(final Supplier supplier) throws X { requireNonNull(supplier); if (isFirst()) { @@ -190,4 +217,58 @@ public final class CheckedValue extends Variant { } throw supplier.get(); } + + /** + * Complete target {@link CompletableFuture} either successfully or exceptionally based on the state of this object. + * + * @param future Future to complete + * @return True if this call has transitioned the future to a completed state, false otherwise. + * @throws NullPointerException if {code future} is null + */ + public boolean completeFuture(final CompletableFuture future) { + return isFirst() ? future.complete(first()) : future.completeExceptionally(second()); + } + + /** + * Complete target {@link SettableFuture} either successfully or exceptionally based on the state of this object. + * + * @param future Future to complete + * @return True if this call has transitioned the future to a completed state, false otherwise. + * @throws NullPointerException if {code future} is null + */ + public boolean completeFuture(final SettableFuture future) { + return isFirst() ? future.set(first()) : future.setException(second()); + } + + /** + * Transform this object into an immediately-completed {@link CompletableFuture}. The future will be successful + * if this object has a contained value or unsuccessful if this objects contains an exception. + * + * @return A {@link CompletableFuture}. + */ + public CompletableFuture toCompletableFuture() { + if (isFirst()) { + return CompletableFuture.completedFuture(first()); + } + // FIXME: Java 9: use CompletableFuture.failedFuture() + final CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(second()); + return future; + } + + /** + * Transform this object into an immediately-completed {@link FluentFuture}. The future will be successful + * if this object has a contained value or unsuccessful if this objects contains an exception. + * + * @return A {@link FluentFuture}. + */ + public FluentFuture toFluentFuture() { + final ListenableFuture future; + if (isFirst()) { + future = Futures.immediateFuture(first()); + } else { + future = Futures.immediateFailedFuture(second()); + } + return FluentFuture.from(future); + } }