Use instanceof patterns in CodecDataObject
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 4 Jun 2023 20:13:50 +0000 (22:13 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Wed, 19 Jun 2024 00:41:45 +0000 (10:41 +1000)
Throw explicit VerifyExceptions instead of verify(... instanceof ...),
so we make it obvious what is going on.

Change-Id: I2fef6be328eb99b2f7c5e6082ad637be0374a17f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObject.java

index 1aa856a3dea4cc3b5e49035d79cfdab31c39215e..5e7c5df0ca8b3dd609b1bb9e4f1625e128a39fda 100644 (file)
@@ -7,9 +7,9 @@
  */
 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.VerifyException;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
 import org.eclipse.jdt.annotation.NonNull;
@@ -90,8 +90,10 @@ public abstract class CodecDataObject<T extends DataObject> implements DataObjec
 
     private @NonNull Object emptyObject(final @NonNull Class<? extends DataObject> bindingClass) {
         final var childContext = context.streamChild(bindingClass);
-        verify(childContext instanceof NonPresenceContainerNodeCodecContext, "Unexpected context %s", childContext);
-        return ((NonPresenceContainerNodeCodecContext<?>) childContext).emptyObject();
+        if (childContext instanceof NonPresenceContainerNodeCodecContext<?> nonPresence) {
+            return nonPresence.emptyObject();
+        }
+        throw new VerifyException("Unexpected context " + childContext);
     }
 
     protected final @NonNull Object codecKey(final VarHandle handle) {
@@ -124,10 +126,14 @@ public abstract class CodecDataObject<T extends DataObject> implements DataObjec
 
     // Helper split out of codecKey to aid its inlining
     private @NonNull 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).name());
+        if (!(data instanceof MapEntryNode mapEntry)) {
+            throw new VerifyException("Unsupported value " + data);
+        }
+        if (!(context instanceof KeyedListNodeCodecContext<?, ?> 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;