Hide CodecContextSupplier
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObject.java
index d075bfb64aeb93830075020d6e1fe5413c6004d8..a32e7ea3094eb8ad498349f388231385df1f1729 100644 (file)
@@ -7,21 +7,17 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
-import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.MoreObjects;
-import com.google.common.base.MoreObjects.ToStringHelper;
+import com.google.common.base.VerifyException;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
 
 /**
  * A base class for {@link DataObject}s backed by {@link DataObjectCodecContext}. While this class is public, it not
@@ -45,48 +41,34 @@ public abstract class CodecDataObject<T extends DataObject> implements DataObjec
         }
     }
 
-    private final @NonNull DataObjectCodecContext<T, ?> context;
-    @SuppressWarnings("rawtypes")
-    private final @NonNull NormalizedNodeContainer data;
+    private final @NonNull AbstractDataObjectCodecContext<T, ?> context;
+    private final @NonNull DataContainerNode data;
 
     // Accessed via a VarHandle
-    @SuppressWarnings("unused")
     // FIXME: consider using a primitive int-based cache (with 0 being uninit)
+    @SuppressWarnings("unused")
+    @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
     private volatile Integer cachedHashcode;
 
-    protected CodecDataObject(final DataObjectCodecContext<T, ?> context, final NormalizedNodeContainer<?, ?, ?> data) {
+    protected CodecDataObject(final AbstractDataObjectCodecContext<T, ?> context, final DataContainerNode data) {
         this.data = requireNonNull(data, "Data must not be null");
         this.context = requireNonNull(context, "Context must not be null");
     }
 
     @Override
     public final int hashCode() {
-        final Integer cached = (Integer) CACHED_HASH_CODE.getAcquire(this);
+        final var cached = (Integer) CACHED_HASH_CODE.getAcquire(this);
         return cached != null ? cached : loadHashCode();
     }
 
     @Override
-    @SuppressFBWarnings(value = "EQ_UNUSUAL", justification = "State is examined indirectly enough to confuse SpotBugs")
     public final boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        final Class<? extends DataObject> iface = implementedInterface();
-        if (!iface.isInstance(obj)) {
-            return false;
-        }
-        @SuppressWarnings("unchecked")
-        final T other = (T) iface.cast(obj);
-        // Note: we do not want to compare NormalizedNode data here, as we may be looking at different instantiations
-        //       of the same grouping -- in which case normalized node will not compare as equal.
-        return codecAugmentedEquals(other);
+        // Indirection to keep checkstyle happy
+        return codecEquals(obj);
     }
 
     @Override
-    public final String toString() {
-        return codecAugmentedFillToString(MoreObjects.toStringHelper(implementedInterface()).omitNullValues())
-                .toString();
-    }
+    public abstract String toString();
 
     protected final Object codecMember(final VarHandle handle, final String localName) {
         final Object cached = handle.getAcquire(this);
@@ -95,66 +77,65 @@ public abstract class CodecDataObject<T extends DataObject> implements DataObjec
 
     protected final Object codecMember(final VarHandle handle, final Class<? extends DataObject> bindingClass) {
         final Object cached = handle.getAcquire(this);
-        return cached != null ? unmaskNull(cached) : loadMember(handle, context.streamChild(bindingClass));
+        return cached != null ? unmaskNull(cached) : loadMember(handle, context.getStreamChild(bindingClass));
     }
 
-    protected final Object codecMember(final VarHandle handle, final NodeContextSupplier supplier) {
+    protected final Object codecMember(final VarHandle handle, final CodecContextSupplier supplier) {
         final Object cached = handle.getAcquire(this);
-        return cached != null ? unmaskNull(cached) : loadMember(handle, supplier.get());
+        return cached != null ? unmaskNull(cached) : loadMember(handle, supplier.getCodecContext());
+    }
+
+    protected final @NonNull Object codecMemberOrEmpty(final @Nullable Object value,
+            final @NonNull Class<? extends DataObject> bindingClass) {
+        return value != null ? value : emptyObject(bindingClass);
+    }
+
+    private @NonNull Object emptyObject(final @NonNull Class<? extends DataObject> bindingClass) {
+        final var childContext = context.getStreamChild(bindingClass);
+        if (childContext instanceof StructuralContainerCodecContext<?> structural) {
+            return structural.emptyObject();
+        }
+        throw new VerifyException("Unexpected context " + childContext);
     }
 
-    protected final Object codecKey(final VarHandle handle) {
+    protected final @NonNull Object codecKey(final VarHandle handle) {
         final Object cached = handle.getAcquire(this);
         return cached != null ? cached : loadKey(handle);
     }
 
     protected abstract int codecHashCode();
 
-    protected abstract boolean codecEquals(T other);
+    protected abstract boolean codecEquals(Object obj);
 
-    protected abstract ToStringHelper codecFillToString(ToStringHelper helper);
-
-    final @NonNull DataObjectCodecContext<T, ?> codecContext() {
+    final @NonNull AbstractDataObjectCodecContext<T, ?> codecContext() {
         return context;
     }
 
-    @SuppressWarnings("rawtypes")
-    final @NonNull NormalizedNodeContainer codecData() {
+    final @NonNull DataContainerNode codecData() {
         return data;
     }
 
-    // Non-final to allow specialization in AugmentableCodecDataObject
-    int codecAugmentedHashCode() {
-        return codecHashCode();
-    }
-
-    // Non-final to allow specialization in AugmentableCodecDataObject
-    boolean codecAugmentedEquals(final T other) {
-        return codecEquals(other);
-    }
-
-    // Non-final to allow specialization in AugmentableCodecDataObject
-    ToStringHelper codecAugmentedFillToString(final ToStringHelper helper) {
-        return codecFillToString(helper);
-    }
-
     // Helper split out of codecMember to aid its inlining
-    private Object loadMember(final VarHandle handle, final NodeCodecContext childCtx) {
-        @SuppressWarnings("unchecked")
-        final Optional<NormalizedNode<?, ?>> child = data.getChild(childCtx.getDomPathArgument());
+    private Object loadMember(final VarHandle handle, final CodecContext childCtx) {
+        final var child = data.childByArg(childCtx.getDomPathArgument());
 
         // We do not want to use Optional.map() here because we do not want to invoke defaultObject() when we have
         // normal value because defaultObject() may end up throwing an exception intentionally.
-        final Object obj = child.isPresent() ? childCtx.deserializeObject(child.get()) : childCtx.defaultObject();
+        final Object obj = child != null ? childCtx.deserializeObject(child) : childCtx.defaultObject();
         final Object witness = handle.compareAndExchangeRelease(this, null, maskNull(obj));
         return witness == null ? obj : unmaskNull(witness);
     }
 
     // Helper split out of codecKey to aid its inlining
-    private Object loadKey(final VarHandle handle) {
-        verify(data instanceof MapEntryNode, "Unsupported value %s", data);
-        verify(context instanceof KeyedListNodeCodecContext, "Unexpected context %s", context);
-        final Object obj = ((KeyedListNodeCodecContext<?>) context).deserialize(((MapEntryNode) data).getIdentifier());
+    private @NonNull Object loadKey(final VarHandle handle) {
+        if (!(data instanceof MapEntryNode mapEntry)) {
+            throw new VerifyException("Unsupported value " + data);
+        }
+        if (!(context instanceof MapCodecContext<?, ?> listContext)) {
+            throw new VerifyException("Unexpected context " + context);
+        }
+
+        final Object obj = listContext.deserialize(mapEntry.name());
         // key is known to be non-null, no need to mask it
         final Object witness = handle.compareAndExchangeRelease(this, null, obj);
         return witness == null ? obj : witness;
@@ -162,8 +143,8 @@ public abstract class CodecDataObject<T extends DataObject> implements DataObjec
 
     // Helper split out of hashCode() to aid its inlining
     private int loadHashCode() {
-        final int result = codecAugmentedHashCode();
-        final Object witness = CACHED_HASH_CODE.compareAndExchangeRelease(this, null, Integer.valueOf(result));
+        final int result = codecHashCode();
+        final Object witness = CACHED_HASH_CODE.compareAndExchangeRelease(this, null, result);
         return witness == null ? result : (Integer) witness;
     }