Introduce AbstractAugmentable 83/81883/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 1 May 2019 14:29:00 +0000 (16:29 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 May 2019 18:55:41 +0000 (20:55 +0200)
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 <robert.varga@pantheon.tech>
(cherry picked from commit 5e590b00d38ddbc28079d6f10928275136aa6b78)

binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AbstractAugmentable.java [new file with mode: 0644]
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AugmentationHolder.java

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 (file)
index 0000000..fa57186
--- /dev/null
@@ -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 <T> Augmentable type
+ */
+@Beta
+public abstract class AbstractAugmentable<T extends Augmentable<T>> implements Augmentable<T>, AugmentationHolder<T> {
+    private final @NonNull ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations;
+
+    protected AbstractAugmentable() {
+        this.augmentations = ImmutableMap.of();
+    }
+
+    protected AbstractAugmentable(final Map<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
+        this.augmentations = ImmutableMap.copyOf(augmentations);
+    }
+
+    protected AbstractAugmentable(
+            final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations) {
+        this.augmentations = requireNonNull(augmentations);
+    }
+
+    protected AbstractAugmentable(final AbstractAugmentable<T> other) {
+        this(other.augmentations);
+    }
+
+    @SuppressWarnings({ "unchecked", "checkstyle:methodTypeParameterName"})
+    @Override
+    public final <E$$ extends Augmentation<T>> E$$ augmentation(final Class<E$$> augmentationType) {
+        return (E$$) augmentations.get(CodeHelpers.nonNullValue(augmentationType, "augmentationType"));
+    }
+
+    @Override
+    public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
+        return augmentations;
+    }
+}
index b23e72eb77094c3e1287c2023211cab42f8998e6..d2036605b1b679c91e31d2c1cb8d6d6df9be7f96 100644 (file)
@@ -24,5 +24,5 @@ public interface AugmentationHolder<T> {
      *
      * @return map of all augmentations.
      */
-    Map<Class<? extends Augmentation<T>>,Augmentation<T>> augmentations();
+    Map<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations();
 }