Improve AbstractImmutableNormalizedNodeBuilder 66/104866/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 13 Mar 2023 22:48:19 +0000 (23:48 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 15 Mar 2023 13:15:33 +0000 (13:15 +0000)
We are accessing fields twice, which elicits an Eclipse warning about
nullability. While this cannot really happen, as the fields cannot
become null once they have been non-null, we can improve the code by
essentially providing our alternative to checkState().

Change-Id: I9263cba1a2e9b46377556de423227ee02c003982
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/AbstractImmutableNormalizedNodeBuilder.java

index c392470fd0102f20693048af4875931a143bda69..c256947eaef228bd6dbf23e1a8aa3644d98645a6 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
-import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -21,13 +21,11 @@ abstract class AbstractImmutableNormalizedNodeBuilder<I extends PathArgument, V,
     private @Nullable V value = null;
 
     protected final I getNodeIdentifier() {
-        checkState(nodeIdentifier != null, "Identifier has not been set");
-        return nodeIdentifier;
+        return checkSet(nodeIdentifier, "Identifier has not been set");
     }
 
     protected final V getValue() {
-        checkState(value != null, "Value has not been set");
-        return value;
+        return checkSet(value, "Value has not been set");
     }
 
     @Override
@@ -41,4 +39,11 @@ abstract class AbstractImmutableNormalizedNodeBuilder<I extends PathArgument, V,
         this.nodeIdentifier = requireNonNull(withNodeIdentifier);
         return this;
     }
+
+    private static <T> @NonNull T checkSet(final @Nullable T obj, final String message) {
+        if (obj == null) {
+            throw new IllegalStateException(message);
+        }
+        return obj;
+    }
 }