From: Robert Varga Date: Mon, 16 Jan 2023 00:27:40 +0000 (+0100) Subject: Add Augmentable.augmentationOrElseThrow() X-Git-Tag: v11.0.4~2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=e3a11ed50a78f65cad4040328fa8178716cc8fac;p=mdsal.git Add Augmentable.augmentationOrElseThrow() A number of places assume, or check-or-throw on, presence of a particular Augmentation. Add convenience methods to short-circuit the equivalent of Optional.orElseThrow(), so users get more comfort expressing this. JIRA: MDSAL-806 Change-Id: I457a93ac1689087a7ceace57489e528b488ed207 Signed-off-by: Robert Varga --- diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Augmentable.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Augmentable.java index e4575233e4..c74f63423c 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Augmentable.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Augmentable.java @@ -7,9 +7,10 @@ */ package org.opendaylight.yangtools.yang.binding; -import static java.util.Objects.requireNonNull; - import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -26,16 +27,58 @@ import org.eclipse.jdt.annotation.Nullable; */ public interface Augmentable { /** - * Returns instance of augmentation. + * Returns instance of augmentation, if present. * - * @param augmentationType Type of augmentation to be returned. * @param Type capture for augmentation type - * @return instance of augmentation, or null if the augmentationType is not present. - * @throws NullPointerException if augmentationType is null + * @param augmentationType Type of augmentation to be returned + * @return instance of {@code A}, or {@code null} if the augmentationType is not present + * @throws NullPointerException if {@code augmentationType} is {@code null} */ - @SuppressWarnings("unchecked") default > @Nullable A augmentation(final Class augmentationType) { - return (A) augmentations().get(requireNonNull(augmentationType)); + return augmentationType.cast(augmentations().get(augmentationType)); + } + + /** + * Returns instance of augmentation, or throws {@link NoSuchElementException}. + * + * @param Type capture for augmentation type + * @param augmentationType Type of augmentation to be returned + * @return An instance of {@code A} + * @throws NullPointerException if {@code augmentationType} is {@code null} + * @throws NoSuchElementException if the corresponding augmentation is not present + * + * @apiNote + * The design here follows {@link Optional#orElseThrow()}, + */ + default > @NonNull A augmentationOrElseThrow(final Class augmentationType) { + final var augmentation = augmentation(augmentationType); + if (augmentation != null) { + return augmentation; + } + throw new NoSuchElementException(augmentationType.getName() + " is not present in " + this); + } + + /** + * Returns instance of augmentation, or throws {@link NoSuchElementException}. + * + * @param Type capture for augmentation type + * @param Type of the exception to be thrown + * @param augmentationType Type of augmentation to be returned + * @param exceptionSupplier the supplying function that produces an exception to be thrown + * @return An instance of {@code A} + * @throws NullPointerException if {@code augmentationType} is {@code null} + * @throws X if the corresponding augmentation is not present + * + * @apiNote + * The design here follows {@link Optional#orElseThrow(Supplier)}, + */ + default , X extends Throwable> @NonNull A augmentationOrElseThrow( + final Class augmentationType, final Supplier<@NonNull X> exceptionSupplier) throws X { + final var augmentation = augmentation(augmentationType); + if (augmentation != null) { + return augmentation; + } + throw exceptionSupplier.get(); } /**