Bump upstreams
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / AugmentableCodecDataObject.java
index 91c64cfc8b012972e985aee13df47d018dbbcdf6..6a74a24661bd80ba433b8f13ea1305ff893f67b2 100644 (file)
@@ -10,15 +10,16 @@ package org.opendaylight.mdsal.binding.dom.codec.impl;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
-import java.util.Optional;
+import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 
 /**
  * A base class for {@link DataObject}s which are also {@link Augmentable}, backed by {@link DataObjectCodecContext}.
@@ -42,10 +43,11 @@ public abstract class AugmentableCodecDataObject<T extends DataObject & Augmenta
 
     // Used via VarHandle
     @SuppressWarnings("unused")
+    @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
     private volatile ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> cachedAugmentations;
 
-    protected AugmentableCodecDataObject(final DataObjectCodecContext<T, ?> context,
-            final NormalizedNodeContainer<?, ?, ?> data) {
+    protected AugmentableCodecDataObject(final AbstractDataObjectCodecContext<T, ?> context,
+            final DataContainerNode data) {
         super(context, data);
     }
 
@@ -54,26 +56,22 @@ public abstract class AugmentableCodecDataObject<T extends DataObject & Augmenta
     public final <A extends Augmentation<T>> @Nullable A augmentation(final Class<A> augmentationType) {
         requireNonNull(augmentationType, "Supplied augmentation must not be null.");
 
-        final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> aug = acquireAugmentations();
+        final var aug = acquireAugmentations();
         if (aug != null) {
             return (A) aug.get(augmentationType);
         }
 
         @SuppressWarnings("rawtypes")
-        final Optional<DataContainerCodecContext<?, ?>> optAugCtx = codecContext().possibleStreamChild(
-            (Class) augmentationType);
-        if (optAugCtx.isPresent()) {
-            final DataContainerCodecContext<?, ?> augCtx = optAugCtx.get();
-            // Due to binding specification not representing grouping instantiations we can end up having the same
-            // augmentation applied to a grouping multiple times. While these augmentations have the same shape, they
-            // are still represented by distinct binding classes and therefore we need to make sure the result matches
-            // the augmentation the user is requesting -- otherwise a strict receiver would end up with a cryptic
-            // ClassCastException.
-            if (augmentationType.isAssignableFrom(augCtx.getBindingClass())) {
-                final Optional<NormalizedNode<?, ?>> augData = codecData().getChild(augCtx.getDomPathArgument());
-                if (augData.isPresent()) {
-                    return (A) augCtx.deserialize(augData.get());
-                }
+        final var augCtx = (AugmentationCodecContext<A>) codecContext().streamChild((Class) augmentationType);
+        // Due to binding specification not representing grouping instantiations we can end up having the same
+        // augmentation applied to a grouping multiple times. While these augmentations have the same shape, they are
+        // still represented by distinct binding classes and therefore we need to make sure the result matches
+        // the augmentation the user is requesting -- otherwise a strict receiver would end up with a cryptic
+        // ClassCastException.
+        if (augCtx != null && augmentationType.isAssignableFrom(augCtx.getBindingClass())) {
+            final var augObj = augCtx.filterFrom(codecData());
+            if (augObj != null) {
+                return augObj;
             }
         }
         return null;
@@ -81,7 +79,7 @@ public abstract class AugmentableCodecDataObject<T extends DataObject & Augmenta
 
     @Override
     public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
-        final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> local = acquireAugmentations();
+        final var local = acquireAugmentations();
         return local != null ? local : loadAugmentations();
     }
 
@@ -90,9 +88,10 @@ public abstract class AugmentableCodecDataObject<T extends DataObject & Augmenta
     }
 
     @SuppressWarnings("unchecked")
-    private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> loadAugmentations() {
-        final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> ret = ImmutableMap.copyOf(
-            codecContext().getAllAugmentationsFrom(codecData()));
+    private @NonNull ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> loadAugmentations() {
+        @SuppressWarnings("rawtypes")
+        final Map extracted = codecContext().getAllAugmentationsFrom(codecData());
+        final var ret = ImmutableMap.<Class<? extends Augmentation<T>>, Augmentation<T>>copyOf(extracted);
         final Object witness = CACHED_AUGMENTATIONS.compareAndExchangeRelease(this, null, ret);
         return witness == null ? ret : (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) witness;
     }