From: Robert Varga Date: Wed, 1 May 2019 14:29:00 +0000 (+0200) Subject: Introduce AbstractAugmentable X-Git-Tag: release/fluorine-sr3~6 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=c4e04329a53e6569db365adc2ffc182b15bc2f3e;p=mdsal.git Introduce AbstractAugmentable Binding generated code hand-implements handling of augmentations, which leads to some amount of duplicated code, but notably a lot of distinct implementations of Augmentatable.augmentation(). We can improve the situation by providing a simple base class which holds an immutable map of augmentations implementing AugmentationHolder and Augmentable interfaces. JIRA: MDSAL-445 Change-Id: I693cd4fbec3d236f039e01448dc4994722b5582d Signed-off-by: Robert Varga (cherry picked from commit 5e590b00d38ddbc28079d6f10928275136aa6b78) --- diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AbstractAugmentable.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AbstractAugmentable.java new file mode 100644 index 0000000000..fa57186f35 --- /dev/null +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AbstractAugmentable.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.binding; + +import static java.util.Objects.requireNonNull; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import org.eclipse.jdt.annotation.NonNull; + +/** + * Abstract base class for implementing immutable {@link Augmentable} classes. This class is provided as a convenience, + * combining {@link AugmentationHolder} and providing {@link Augmentable#augmentation(Class)} implementation on top of + * held augmentations. + * + * @param Augmentable type + */ +@Beta +public abstract class AbstractAugmentable> implements Augmentable, AugmentationHolder { + private final @NonNull ImmutableMap>, Augmentation> augmentations; + + protected AbstractAugmentable() { + this.augmentations = ImmutableMap.of(); + } + + protected AbstractAugmentable(final Map>, Augmentation> augmentations) { + this.augmentations = ImmutableMap.copyOf(augmentations); + } + + protected AbstractAugmentable( + final ImmutableMap>, Augmentation> augmentations) { + this.augmentations = requireNonNull(augmentations); + } + + protected AbstractAugmentable(final AbstractAugmentable other) { + this(other.augmentations); + } + + @SuppressWarnings({ "unchecked", "checkstyle:methodTypeParameterName"}) + @Override + public final > E$$ augmentation(final Class augmentationType) { + return (E$$) augmentations.get(CodeHelpers.nonNullValue(augmentationType, "augmentationType")); + } + + @Override + public final ImmutableMap>, Augmentation> augmentations() { + return augmentations; + } +} diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AugmentationHolder.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AugmentationHolder.java index b23e72eb77..d2036605b1 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AugmentationHolder.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AugmentationHolder.java @@ -24,5 +24,5 @@ public interface AugmentationHolder { * * @return map of all augmentations. */ - Map>,Augmentation> augmentations(); + Map>, Augmentation> augmentations(); }