From 7d20a00e7721aea50a8bc253c10dd4fa29d2314f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 2 May 2018 14:39:12 +0200 Subject: [PATCH] Make Variant/CheckedValue subclassable It is useful to specialized these two classes outside of concepts, for example by providing alternative constructs on top of Variant and providing a type-bound domain-specific CheckedValue. Expose constructors and first()/second() to subclasses so that it is feasible to perform these things. Change-Id: I1253ba2b0ed115b0b3f6dc6f07138634f15091de Signed-off-by: Robert Varga --- .../yangtools/concepts/CheckedValue.java | 32 +++++++++---------- .../yangtools/concepts/Variant.java | 8 ++--- 2 files changed, 20 insertions(+), 20 deletions(-) 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 96323875ff..cac4c5b9b2 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 @@ -35,12 +35,12 @@ import org.eclipse.jdt.annotation.Nullable; @Beta @NonNullByDefault @ThreadSafe -public final class CheckedValue extends Variant { - private CheckedValue(final T value) { +public class CheckedValue extends Variant { + protected CheckedValue(final T value) { super(value); } - private CheckedValue(final E violation, final @Nullable Void dummy) { + protected CheckedValue(final E violation, final @Nullable Void dummy) { super(violation, dummy); } @@ -86,7 +86,7 @@ public final class CheckedValue extends Variant { * @return Contained value * @throws IllegalStateException if an error string is present. */ - public T get() { + public final T get() { if (isFirst()) { return first(); } @@ -101,7 +101,7 @@ public final class CheckedValue extends Variant { * string. * @throws IllegalStateException if a value is present. */ - public E getException() { + public final E getException() { if (isSecond()) { return second(); } @@ -113,7 +113,7 @@ public final class CheckedValue extends Variant { * * @return True if a value is present. */ - public boolean isPresent() { + public final boolean isPresent() { return isFirst(); } @@ -123,7 +123,7 @@ public final class CheckedValue extends Variant { * @param consumer block to be executed if a value is present * @throws NullPointerException if value is present and {@code consumer} is null */ - public void ifPresent(final Consumer consumer) { + public final void ifPresent(final Consumer consumer) { if (isFirst()) { consumer.accept(first()); } @@ -157,7 +157,7 @@ public final class CheckedValue extends Variant { * @param other Replacement value * @return Contained value or {code other} */ - public T orElse(final T other) { + public final T orElse(final T other) { return isFirst() ? first() : other; } @@ -168,7 +168,7 @@ public final class CheckedValue extends Variant { * @return Contained value or supplier's value * @throws NullPointerException if {@code supplier} is null */ - public T orElseGet(final Supplier supplier) { + public final T orElseGet(final Supplier supplier) { requireNonNull(supplier); return isFirst() ? first() : supplier.get(); } @@ -179,7 +179,7 @@ public final class CheckedValue extends Variant { * @return Contained value * @throws E When there is no contained value */ - public T orElseThrow() throws E { + public final T orElseThrow() throws E { if (isFirst()) { return first(); } @@ -194,7 +194,7 @@ public final class CheckedValue extends Variant { * @throws NullPointerException if {@code exceptionMapper} is null * @throws X When there is no contained value */ - public T orElseThrow(final Function exceptionMapper) throws X { + public final T orElseThrow(final Function exceptionMapper) throws X { requireNonNull(exceptionMapper); if (isFirst()) { return first(); @@ -210,7 +210,7 @@ public final class CheckedValue extends Variant { * @throws NullPointerException if {@code exceptionMapper} is null * @throws X When there is no contained value */ - public T orElseThrow(final Supplier supplier) throws X { + public final T orElseThrow(final Supplier supplier) throws X { requireNonNull(supplier); if (isFirst()) { return first(); @@ -225,7 +225,7 @@ public final class CheckedValue extends Variant { * @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) { + public final boolean completeFuture(final CompletableFuture future) { return isFirst() ? future.complete(first()) : future.completeExceptionally(second()); } @@ -236,7 +236,7 @@ public final class CheckedValue extends Variant { * @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) { + public final boolean completeFuture(final SettableFuture future) { return isFirst() ? future.set(first()) : future.setException(second()); } @@ -246,7 +246,7 @@ public final class CheckedValue extends Variant { * * @return A {@link CompletableFuture}. */ - public CompletableFuture toCompletableFuture() { + public final CompletableFuture toCompletableFuture() { if (isFirst()) { return CompletableFuture.completedFuture(first()); } @@ -262,7 +262,7 @@ public final class CheckedValue extends Variant { * * @return A {@link FluentFuture}. */ - public FluentFuture toFluentFuture() { + public final FluentFuture toFluentFuture() { final ListenableFuture future; if (isFirst()) { future = Futures.immediateFuture(first()); diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Variant.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Variant.java index 1d019560b1..0397055539 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Variant.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Variant.java @@ -33,21 +33,21 @@ public class Variant { private final @Nullable T first; private final @Nullable U second; - Variant(final T first) { + protected Variant(final T first) { this.first = requireNonNull(first); second = null; } - Variant(final U second, final @Nullable Void dummy) { + protected Variant(final U second, final @Nullable Void dummy) { first = null; this.second = requireNonNull(second); } - final T first() { + protected final T first() { return verifyNotNull(first); } - final U second() { + protected final U second() { return verifyNotNull(second); } -- 2.36.6