Use Optional.isEmpty() 95/107695/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Sep 2023 20:46:15 +0000 (22:46 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Sep 2023 21:46:06 +0000 (23:46 +0200)
We have a number of calls to '!Optional.isPresent()'. Java 11 provides a
convenience Optional.isEmpty() for this case, so use that instead.

Change-Id: I519671f470d804e62bae07ec4c5a71aaa08a29f8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamNormalizedNodeStreamWriter.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/AbstractNodeContainerModificationStrategy.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/AutomaticLifecycleMixin.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MandatoryLeafEnforcer.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/SchemaAwareApplyOperation.java
data/yang-data-tree-ri/src/test/java/org/opendaylight/yangtools/yang/data/tree/impl/Bug4454Test.java
parser/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/uses/UsesStatementSupport.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/AugmentTest.java

index 6168b96abb560c6860217829234beaea645563b1..4ac9865aec4aa11ae3bd2a834e8d859bb30bb71a 100644 (file)
@@ -8,7 +8,6 @@
  */
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
-import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
@@ -65,9 +64,13 @@ final class SchemaAwareXMLStreamNormalizedNodeStreamWriter
                 qname.getModule());
         }
 
-        checkArgument(!qname.getRevision().isPresent(), "Failed to find bound annotation %s", qname);
-        checkArgument(value instanceof String, "Invalid non-string value %s for unbound annotation %s", value, qname);
-        return (String) value;
+        if (qname.getRevision().isPresent()) {
+            throw new IllegalArgumentException("Failed to find bound annotation " + qname);
+        }
+        if (value instanceof String str) {
+            return str;
+        }
+        throw new IllegalArgumentException("Invalid non-string value " + value + " for unbound annotation " + qname);
     }
 
     @Override
index 5a43bfdd28e996eaf24ffd8ce8626b24c0fcb9f5..3c5879228f831efbc127142a4f7bb38c1ba7d878 100644 (file)
@@ -354,13 +354,13 @@ abstract sealed class AbstractNodeContainerModificationStrategy<T extends DataSc
     protected final void checkTouchApplicable(final ModificationPath path, final NodeModification modification,
             final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
         final TreeNode currentNode;
-        if (!current.isPresent()) {
+        if (current.isEmpty()) {
             currentNode = defaultTreeNode();
             if (currentNode == null) {
-                if (!modification.getOriginal().isPresent()) {
+                if (modification.getOriginal().isEmpty()) {
                     final YangInstanceIdentifier id = path.toInstanceIdentifier();
                     throw new ModifiedNodeDoesNotExistException(id,
-                        String.format("Node %s does not exist. Cannot apply modification to its children.", id));
+                        "Node " + id + " does not exist. Cannot apply modification to its children.");
                 }
 
                 throw new ConflictingModificationAppliedException(path.toInstanceIdentifier(),
index 1eb0c1f4be54ee49dee4d4d664562bd9b8cba76f..89467acdbb69ae6e5ff9fe5f83994f9749584912 100644 (file)
@@ -54,7 +54,7 @@ final class AutomaticLifecycleMixin {
             }
             // Delete with children, implies it really is an empty write
             ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, storeMeta, version));
-        } else if (modification.getOperation() == LogicalOperation.TOUCH && !storeMeta.isPresent()) {
+        } else if (modification.getOperation() == LogicalOperation.TOUCH && storeMeta.isEmpty()) {
             ret = applyTouch(delegate, emptyNode, modification, storeMeta, version);
         } else {
             // No special handling required here, run normal apply operation
@@ -85,7 +85,7 @@ final class AutomaticLifecycleMixin {
 
         // We are pulling the 'disappear' trick, but what we report can be three different things
         final ModificationType finalType;
-        if (!storeMeta.isPresent()) {
+        if (storeMeta.isEmpty()) {
             // ... there was nothing in the datastore, no change
             finalType = ModificationType.UNMODIFIED;
         } else if (modification.getModificationType() == ModificationType.WRITE) {
index 5bd626a83ca48dcf117024eb79ed689c5b1f9cd4..421291f718e4108a9a831128ef6f0900dd5bbc5e 100644 (file)
@@ -85,9 +85,9 @@ final class MandatoryLeafEnforcer implements Immutable {
 
     void enforceOnData(final NormalizedNode data) {
         for (var path : mandatoryNodes) {
-            if (!NormalizedNodes.findNode(data, path).isPresent()) {
-                throw new IllegalArgumentException(String.format("Node %s is missing mandatory descendant %s",
-                    data.name(), YangInstanceIdentifier.of(path)));
+            if (NormalizedNodes.findNode(data, path).isEmpty()) {
+                throw new IllegalArgumentException("Node " + data.name() + " is missing mandatory descendant "
+                    + YangInstanceIdentifier.of(path));
             }
         }
     }
index 9f97c637b7dd14546b062c0e5693f1b31d5b4db7..e884f300c2e67935894810328af1bcb07bc81e5b 100644 (file)
@@ -176,19 +176,19 @@ abstract sealed class SchemaAwareApplyOperation<T extends DataSchemaNode> extend
      */
     private static void checkWriteApplicable(final ModificationPath path, final NodeModification modification,
             final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
-        final Optional<? extends TreeNode> original = modification.getOriginal();
+        final var original = modification.getOriginal();
         if (original.isPresent() && current.isPresent()) {
             checkNotConflicting(path, original.orElseThrow(), current.orElseThrow());
         } else {
-            checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
-            checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
+            checkConflicting(path, original.isEmpty(), "Node was deleted by other transaction.");
+            checkConflicting(path, current.isEmpty(), "Node was created by other transaction.");
         }
     }
 
     private static void checkDeleteApplicable(final NodeModification modification,
             final Optional<? extends TreeNode> current) {
         // Delete is always applicable, we do not expose it to subclasses
-        if (!current.isPresent()) {
+        if (current.isEmpty()) {
             LOG.trace("Delete operation turned to no-op on missing node {}", modification);
         }
     }
@@ -211,7 +211,7 @@ abstract sealed class SchemaAwareApplyOperation<T extends DataSchemaNode> extend
             case MERGE -> {
                 final TreeNode result;
 
-                if (!currentMeta.isPresent()) {
+                if (currentMeta.isEmpty()) {
                     // This is a slight optimization: a merge on a non-existing node equals to a write. Written data
                     // structure is usually verified when the transaction is sealed. To preserve correctness, we have
                     // to run that validation here.
index cb01da433fa3ad02210ea227f02d9e2c3c1d0295..a0f15f361bc536e144fd37a0b4047d795906eb84 100644 (file)
@@ -232,7 +232,7 @@ public class Bug4454Test {
 
         DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
         Optional<NormalizedNode> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
-        assertTrue(!minMaxListRead.isPresent());
+        assertFalse(minMaxListRead.isPresent());
 
         modificationTree1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
         modificationTree1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
index 49291fc87bcd2e0064d6700beddc3be7649f3848..217b9f084d28a0a530afd807fca18caaadfc17c5 100644 (file)
@@ -257,7 +257,7 @@ public final class UsesStatementSupport
         // FIXME: this really should be handled via separate inference, i.e. we first instantiate the template and when
         //        it appears, this refine will trigger on it. This reinforces the FIXME below.
         final var optRefineTargetCtx = ParserNamespaces.findSchemaTreeStatement(usesParentCtx, refineDescendant);
-        InferenceException.throwIf(!optRefineTargetCtx.isPresent(), refineStmtCtx, "Refine target node %s not found.",
+        InferenceException.throwIf(optRefineTargetCtx.isEmpty(), refineStmtCtx, "Refine target node %s not found.",
             refineDescendant);
 
         // FIXME: This communicates the looked-up target node to RefineStatementSupport.buildEffective(). We should do
index a6b08029b3ec0923e51c2c0a7133a443e960f7a3..47b7a8e8cdb1e3b92f042bd42fab792437f57a88 100644 (file)
@@ -109,7 +109,7 @@ class AugmentTest extends AbstractYangTest {
         AugmentationSchemaNode augment2 = null;
         AugmentationSchemaNode augment3 = null;
         for (final AugmentationSchemaNode as : augmentations) {
-            if (!as.getWhenCondition().isPresent()) {
+            if (as.getWhenCondition().isEmpty()) {
                 augment3 = as;
             } else if ("br:ifType='ds0'".equals(as.getWhenCondition().orElseThrow().toString())) {
                 augment1 = as;