Do not use Optional in returns 04/107704/11
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Sep 2023 00:59:37 +0000 (02:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Oct 2023 09:31:39 +0000 (11:31 +0200)
Optionals are providing type-safety, but we can achieve the same thing
with @Nullable TreeNode. Update call convention to reflect that.

JIRA: YANGTOOLS-1538
Change-Id: I67cbf46d2683c454f5aaece40041bef02d5dab0b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
15 files changed:
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/AbstractDataTreeTip.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/AbstractValidation.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/ChoiceModificationStrategy.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/ContainerModificationStrategy.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/InMemoryDataTreeModification.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/ListModificationStrategy.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MapModificationStrategy.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/ModificationApplyOperation.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/ModifiedNode.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/NodeModification.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/OperationWithModification.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/SchemaAwareApplyOperation.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/ValidatedTreeNode.java

index 5d54527db44a4e84071179c226fabece31669b16..64384421e4d7228f2770a921d30602c949b0c64a 100644 (file)
@@ -42,9 +42,11 @@ abstract class AbstractDataTreeTip implements DataTreeTip {
             return new NoopDataTreeCandidate(YangInstanceIdentifier.of(), root, currentRoot);
         }
 
-        final var newRoot = m.getStrategy().apply(m.getRootModification(), currentRoot, m.getVersion())
-            .orElseThrow(() -> new IllegalStateException(
-                "Apply strategy failed to produce root node for modification " + modification));
+        final var newRoot = m.getStrategy().apply(m.getRootModification(), currentRoot, m.getVersion());
+        if (newRoot == null) {
+            throw new IllegalStateException("Apply strategy failed to produce root node for modification "
+                + modification);
+        }
         return new InMemoryDataTreeCandidate(YangInstanceIdentifier.of(), root, currentRoot, newRoot);
     }
 
index b47df761f2aad766de955ced56393d781a66577d..e0bb0ef8575899ed5c0b804ebb56f64caabc3176 100644 (file)
@@ -13,7 +13,6 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.VerifyException;
 import java.util.Collection;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
@@ -83,7 +82,7 @@ abstract sealed class AbstractNodeContainerModificationStrategy<T extends DataSc
 
     /**
      * Fake TreeNode version used in
-     * {@link #checkTouchApplicable(ModificationPath, NodeModification, Optional, Version)}
+     * {@link #checkTouchApplicable(ModificationPath, NodeModification, TreeNode, Version)}
      * It is okay to use a global constant, as the delegate will ignore it anyway.
      */
     private static final Version FAKE_VERSION = Version.initial();
@@ -213,10 +212,9 @@ abstract sealed class AbstractNodeContainerModificationStrategy<T extends DataSc
         for (var mod : modifications) {
             final var id = mod.getIdentifier();
             final var result = resolveChildOperation(id).apply(mod, meta.childByArg(id), nodeVersion);
-            if (result.isPresent()) {
-                final var tn = result.orElseThrow();
-                meta.putChild(tn);
-                data.addChild(tn.getData());
+            if (result != null) {
+                meta.putChild(result);
+                data.addChild(result.getData());
             } else {
                 meta.removeChild(id);
                 data.removeChild(id);
@@ -288,8 +286,8 @@ abstract sealed class AbstractNodeContainerModificationStrategy<T extends DataSc
                 if (!modification.isEmpty()) {
                     // Version does not matter here as we'll throw it out
                     final var current = apply(modification, modification.original(), Version.initial());
-                    if (current.isPresent()) {
-                        modification.updateValue(LogicalOperation.WRITE, current.orElseThrow().getData());
+                    if (current != null) {
+                        modification.updateValue(LogicalOperation.WRITE, current.getData());
                         mergeChildrenIntoModification(modification, valueChildren, version);
                         return;
                     }
index e69b959a7d324bf1be8391e7aea6ebdc4bf17cc0..8f7216a8be164b8225ccab6ab482719d968e07cf 100644 (file)
@@ -11,8 +11,8 @@ import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
 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;
 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
@@ -61,24 +61,25 @@ abstract sealed class AbstractValidation extends ModificationApplyOperation
     }
 
     @Override
-    final Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-            final Version version) {
+    final TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         var validated = modification.validatedNode(this, currentMeta);
         if (validated != null) {
-            return validated.toOptional();
+            return validated.treeNode();
         }
 
         // This might also mean the delegate is maintaining validation
         if (delegate instanceof AbstractValidation) {
             validated = modification.validatedNode(delegate, currentMeta);
             if (validated != null) {
-                return validated.toOptional();
+                return validated.treeNode();
             }
         }
 
         // Deal with the result moving on us
         final var ret = delegate.apply(modification, currentMeta, version);
-        ret.ifPresent(meta -> enforceOnData(meta.getData()));
+        if (ret != null) {
+            enforceOnData(ret.getData());
+        }
         return ret;
     }
 
@@ -93,7 +94,7 @@ abstract sealed class AbstractValidation extends ModificationApplyOperation
         }
 
         if (delegate instanceof AbstractValidation) {
-            checkApplicable(path, verifyNotNull(modified.validatedNode(delegate, currentMeta)).toOptional());
+            checkApplicable(path, verifyNotNull(modified.validatedNode(delegate, currentMeta)).treeNode());
             return;
         }
 
@@ -111,11 +112,11 @@ abstract sealed class AbstractValidation extends ModificationApplyOperation
         modified.setValidatedNode(this, currentMeta, applied);
     }
 
-    private void checkApplicable(final ModificationPath path, final Optional<? extends TreeNode> applied)
+    private void checkApplicable(final ModificationPath path, final @Nullable TreeNode applied)
             throws DataValidationFailedException {
-        if (applied.isPresent()) {
+        if (applied != null) {
             // We only enforce min/max on present data and rely on MandatoryLeafEnforcer to take care of the empty case
-            enforceOnData(path, applied.orElseThrow().getData());
+            enforceOnData(path, applied.getData());
         }
     }
 
index fa16bbd8e9d8da1aa474ebc12f9959134d568ac6..4cfa5dd2bacf3d990611962007399a2bf15bef41 100644 (file)
@@ -7,9 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.data.tree.impl;
 
-import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
 
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -28,7 +27,7 @@ final class AutomaticLifecycleMixin {
      */
     @FunctionalInterface
     interface Apply {
-        Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
+        @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
     }
 
     /**
@@ -45,16 +44,16 @@ final class AutomaticLifecycleMixin {
         // Hidden on purpose
     }
 
-    static Optional<? extends TreeNode> apply(final Apply delegate, final ApplyWrite writeDelegate,
+    static @Nullable TreeNode apply(final Apply delegate, final ApplyWrite writeDelegate,
             final NormalizedNode emptyNode, final ModifiedNode modification, final @Nullable TreeNode currentMeta,
             final Version version) {
-        final Optional<? extends TreeNode> ret;
+        final @Nullable TreeNode ret;
         if (modification.getOperation() == LogicalOperation.DELETE) {
             if (modification.isEmpty()) {
                 return delegate.apply(modification, currentMeta, version);
             }
             // Delete with children, implies it really is an empty write
-            ret = Optional.of(writeDelegate.applyWrite(modification, emptyNode, currentMeta, version));
+            ret = verifyNotNull(writeDelegate.applyWrite(modification, emptyNode, currentMeta, version));
         } else if (modification.getOperation() == LogicalOperation.TOUCH && currentMeta == null) {
             ret = applyTouch(delegate, emptyNode, modification, null, version);
         } else {
@@ -62,10 +61,10 @@ final class AutomaticLifecycleMixin {
             ret = delegate.apply(modification, currentMeta, version);
         }
 
-        return ret.isPresent() ? disappearResult(modification, ret.orElseThrow(), currentMeta) : ret;
+        return ret == null ? null : disappearResult(modification, ret, currentMeta);
     }
 
-    private static Optional<? extends TreeNode> applyTouch(final Apply delegate, final NormalizedNode emptyNode,
+    private static @Nullable TreeNode applyTouch(final Apply delegate, final NormalizedNode emptyNode,
             final ModifiedNode modification, final @Nullable TreeNode currentMeta, final Version version) {
         // Container is not present, let's take care of the 'magically appear' part of our job
         final var ret = delegate.apply(modification, fakeMeta(emptyNode, version), version);
@@ -77,11 +76,15 @@ final class AutomaticLifecycleMixin {
         return ret;
     }
 
-    private static Optional<? extends TreeNode> disappearResult(final ModifiedNode modification, final TreeNode result,
+    private static @Nullable TreeNode disappearResult(final ModifiedNode modification, final @NonNull TreeNode result,
             final @Nullable TreeNode currentMeta) {
         // Check if the result is in fact empty before pulling any tricks
-        if (!isEmpty(result)) {
-            return Optional.of(result);
+        final var data = result.getData();
+        if (!(data instanceof NormalizedNodeContainer<?> container)) {
+            throw new IllegalStateException("Unhandled data " + data);
+        }
+        if (!container.isEmpty()) {
+            return result;
         }
 
         // We are pulling the 'disappear' trick, but what we report can be three different things
@@ -97,16 +100,10 @@ final class AutomaticLifecycleMixin {
             finalType = ModificationType.DISAPPEARED;
         }
         modification.resolveModificationType(finalType);
-        return Optional.empty();
+        return null;
     }
 
     private static @NonNull TreeNode fakeMeta(final NormalizedNode emptyNode, final Version version) {
         return TreeNode.of(emptyNode, version);
     }
-
-    private static boolean isEmpty(final TreeNode treeNode) {
-        final NormalizedNode data = treeNode.getData();
-        checkState(data instanceof NormalizedNodeContainer, "Unhandled data %s", data);
-        return ((NormalizedNodeContainer<?>) data).size() == 0;
-    }
 }
index 9420a6527011026a0cfb0011ad365920271b1e8a..32706aaf389041ee0a92684fa76e606bb12fde39 100644 (file)
@@ -14,7 +14,6 @@ import static com.google.common.base.Verify.verifyNotNull;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import java.util.HashMap;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -76,8 +75,7 @@ final class ChoiceModificationStrategy extends Visible<ChoiceSchemaNode> {
     }
 
     @Override
-    Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-            final Version version) {
+    TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
             version);
     }
index b6bc51b70bb63a2a904b878cfe7234b2d627806c..b521c29f2d6a9723ff1bd36f4da16ddd41d12743 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.yangtools.yang.data.tree.impl;
 
 import static java.util.Objects.requireNonNull;
 
-import java.util.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
@@ -84,8 +83,7 @@ sealed class ContainerModificationStrategy extends DataNodeContainerModification
         }
 
         @Override
-        Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-                final Version version) {
+        TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
             return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
                 version);
         }
index 56f426fd4a92dbd07ce2a476aeda90ca330769ec..5d1012f5c80de06356ec7cd24e66990545a2f187 100644 (file)
@@ -14,6 +14,7 @@ import static java.util.Objects.requireNonNull;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -126,20 +127,14 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements
         final var terminalPath = terminal.getKey();
 
         final var result = resolveSnapshot(terminalPath, terminal.getValue());
-        if (result.isPresent()) {
-            final var data = result.orElseThrow().getData();
-            return NormalizedNodes.findNode(terminalPath, data, path);
-        }
-
-        return Optional.empty();
+        return result == null ? Optional.empty() : NormalizedNodes.findNode(terminalPath, result.getData(), path);
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
-    private Optional<? extends TreeNode> resolveSnapshot(final YangInstanceIdentifier path,
-            final ModifiedNode modification) {
+    private @Nullable TreeNode resolveSnapshot(final YangInstanceIdentifier path, final ModifiedNode modification) {
         final var potentialSnapshot = modification.getSnapshot();
         if (potentialSnapshot != null) {
-            return potentialSnapshot;
+            return potentialSnapshot.orElse(null);
         }
 
         try {
@@ -213,10 +208,11 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements
          * have same version each time this method is called.
          */
         final var originalSnapshotRoot = snapshot.getRootNode();
-        return new InMemoryDataTreeSnapshot(snapshot.getEffectiveModelContext(),
-            getStrategy().apply(rootNode, originalSnapshotRoot, version)
-                .orElseThrow(() -> new IllegalStateException(
-                    "Data tree root is not present, possibly removed by previous modification")), strategyTree)
+        final var newRoot = getStrategy().apply(rootNode, originalSnapshotRoot, version);
+        if (newRoot == null) {
+            throw new IllegalStateException("Data tree root is not present, possibly removed by previous modification");
+        }
+        return new InMemoryDataTreeSnapshot(snapshot.getEffectiveModelContext(), newRoot, strategyTree)
             .newModification();
     }
 
index 8b3563e116d256b08fb1a64f3e2e655c06a39a47..f6d1e03862b05e003500ddbb62d9b598e86e4bd6 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.yangtools.yang.data.tree.impl;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -44,8 +43,7 @@ final class ListModificationStrategy extends SchemaAwareApplyOperation<ListSchem
     }
 
     @Override
-    Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-            final Version version) {
+    TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
             version);
     }
@@ -108,10 +106,9 @@ final class ListModificationStrategy extends SchemaAwareApplyOperation<ListSchem
             final var cm = meta.childByArg(id);
 
             final var result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
-            if (result.isPresent()) {
-                final TreeNode tn = result.orElseThrow();
-                meta.putChild(tn);
-                data.addChild(tn.getData());
+            if (result != null) {
+                meta.putChild(result);
+                data.addChild(result.getData());
             } else {
                 meta.removeChild(id);
                 data.removeChild(id);
index c894bd2814bb6707bc1fcd9d4b51e64a069ade28..71e009f9666b6b512895738c7796c10f5e5ab420 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.yangtools.yang.data.tree.impl;
 
 import static java.util.Objects.requireNonNull;
 
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -61,8 +60,7 @@ final class MapModificationStrategy extends Invisible<ListSchemaNode> {
     }
 
     @Override
-    Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-            final Version version) {
+    TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         return AutomaticLifecycleMixin.apply(super::apply, this::applyWrite, emptyNode, modification, currentMeta,
             version);
     }
index 3fcdccd1ae8bfc2c730bc4c54d7cebc0e5f99fd8..e35666a1de1212f6e64e6d2f11ea1805d9725e2a 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.yangtools.yang.data.tree.impl;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -51,12 +50,11 @@ abstract sealed class ModificationApplyOperation implements StoreTreeNode<Modifi
      * @param modification NodeModification to be applied
      * @param currentMeta Store Metadata Node on which NodeModification should be applied
      * @param version New subtree version of parent node
-     * @return new {@link TreeNode} if operation resulted in updating node, {@link Optional#empty()} if
-     *         {@link ModifiedNode} resulted in deletion of this node.
+     * @return new {@link TreeNode} if operation resulted in updating node, {@code null} if {@link ModifiedNode}
+     *         resulted in deletion of this node.
      * @throws IllegalArgumentException If it is not possible to apply Operation on provided Metadata node
      */
-    abstract Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta,
-            Version version);
+    abstract @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
 
     /**
      * Checks if provided node modification could be applied to current metadata node.
index bad329943b6dc6d6322561826c7bb5847f2bac67..2f2152d056d2f4325ff0f50c2d4f2148df9396c1 100644 (file)
@@ -251,7 +251,8 @@ final class ModifiedNode extends NodeModification implements StoreTreeNode<Modif
             case WRITE -> {
                 // A WRITE can collapse all of its children
                 if (!children.isEmpty()) {
-                    value = schema.apply(this, original(), version).map(TreeNode::getData).orElse(null);
+                    final var applied = schema.apply(this, original(), version);
+                    value = applied != null ? applied.getData() : null;
                     children.clear();
                 }
 
@@ -276,8 +277,8 @@ final class ModifiedNode extends NodeModification implements StoreTreeNode<Modif
         return snapshotCache;
     }
 
-    Optional<TreeNode> setSnapshot(final Optional<TreeNode> snapshot) {
-        snapshotCache = requireNonNull(snapshot);
+    @Nullable TreeNode setSnapshot(final @Nullable TreeNode snapshot) {
+        snapshotCache = Optional.ofNullable(snapshot);
         return snapshot;
     }
 
@@ -332,7 +333,7 @@ final class ModifiedNode extends NodeModification implements StoreTreeNode<Modif
     }
 
     void setValidatedNode(final ModificationApplyOperation op, final @Nullable TreeNode currentMeta,
-            final Optional<? extends TreeNode> node) {
+            final @Nullable TreeNode node) {
         validatedOp = requireNonNull(op);
         validatedCurrent = currentMeta;
         validatedNode = new ValidatedTreeNode(node);
index 5d24b2a2e57b756c996ed843c8b11961ba18db1d..b7c176cfb8c8a1760449670da5300943a4261758 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.yangtools.yang.data.tree.impl;
 
 import java.util.Collection;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Identifiable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -26,16 +25,6 @@ abstract sealed class NodeModification implements Identifiable<PathArgument> per
      */
     abstract LogicalOperation getOperation();
 
-    /**
-     * Get the original tree node to which the modification is to be applied.
-     *
-     * @return The original node, or {@link Optional#absent()} if the node is a new node.
-     */
-    // FIXME: we should not need this method
-    final Optional<? extends TreeNode> getOriginal() {
-        return Optional.ofNullable(original());
-    }
-
     /**
      * Get the original tree node to which the modification is to be applied.
      *
index 64b2a891d806cfa77d59eea9f7fa8007a5b049be..29227d4106378b339f1e3aae3c701a6e09b94e07 100644 (file)
@@ -59,18 +59,19 @@ final class OperationWithModification {
     Optional<NormalizedNode> read(final PathArgument child, final Version version) {
         final ModifiedNode childNode = modification.childByArg(child);
         if (childNode != null) {
-            Optional<? extends TreeNode> snapshot = childNode.getSnapshot();
+            var snapshot = childNode.getSnapshot();
             if (snapshot == null) {
                 // Snapshot is not present, force instantiation
-                snapshot = applyOperation.getChildByArg(child).apply(childNode, childNode.original(), version);
+                snapshot = Optional.ofNullable(
+                    applyOperation.getChildByArg(child).apply(childNode, childNode.original(), version));
             }
 
             return snapshot.map(TreeNode::getData);
         }
 
-        Optional<? extends TreeNode> snapshot = modification.getSnapshot();
+        var snapshot = modification.getSnapshot();
         if (snapshot == null) {
-            snapshot = apply(modification.original(), version);
+            snapshot = Optional.ofNullable(apply(modification.original(), version));
         }
 
         if (snapshot.isPresent()) {
@@ -88,7 +89,7 @@ final class OperationWithModification {
         return applyOperation;
     }
 
-    public Optional<? extends TreeNode> apply(final @Nullable TreeNode data, final Version version) {
+    public @Nullable TreeNode apply(final @Nullable TreeNode data, final Version version) {
         return applyOperation.apply(modification, data, version);
     }
 
index 79ae7310882a39a79f976882f77d8735db5b78bc..741d6fd3a61374203c31a602d6cd7ae3a557ef40 100644 (file)
@@ -11,7 +11,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Verify.verifyNotNull;
 
 import java.util.List;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -192,20 +191,19 @@ abstract sealed class SchemaAwareApplyOperation<T extends DataSchemaNode> extend
     }
 
     @Override
-    Optional<? extends TreeNode> apply(final ModifiedNode modification, final TreeNode currentMeta,
-            final Version version) {
+    TreeNode apply(final ModifiedNode modification, final TreeNode currentMeta, final Version version) {
         return switch (modification.getOperation()) {
             case DELETE -> {
                 // Deletion of a non-existing node is a no-op, report it as such
                 modification.resolveModificationType(currentMeta != null ? ModificationType.DELETE
                         : ModificationType.UNMODIFIED);
-                yield modification.setSnapshot(Optional.empty());
+                yield modification.setSnapshot(null);
             }
             case TOUCH -> {
                 if (currentMeta == null) {
                     throw new IllegalArgumentException("Metadata not available for modification " + modification);
                 }
-                yield modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta, version)));
+                yield modification.setSnapshot(applyTouch(modification, currentMeta, version));
             }
             case MERGE -> {
                 final TreeNode result;
@@ -221,16 +219,16 @@ abstract sealed class SchemaAwareApplyOperation<T extends DataSchemaNode> extend
                     result = applyMerge(modification, currentMeta, version);
                 }
 
-                yield modification.setSnapshot(Optional.of(result));
+                yield modification.setSnapshot(result);
             }
             case WRITE -> {
                 modification.resolveModificationType(ModificationType.WRITE);
-                yield modification.setSnapshot(Optional.of(applyWrite(modification,
-                    verifyNotNull(modification.getWrittenValue()), currentMeta, version)));
+                yield modification.setSnapshot(applyWrite(modification, verifyNotNull(modification.getWrittenValue()),
+                    currentMeta, version));
             }
             case NONE -> {
                 modification.resolveModificationType(ModificationType.UNMODIFIED);
-                yield Optional.ofNullable(currentMeta);
+                yield currentMeta;
             }
         };
     }
index 3b942d9312670108fc2ead484510a421aad91b98..22bb4e677d24ac591010ad9e6c3ca6869dcf8816 100644 (file)
@@ -7,18 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.data.tree.impl;
 
-import java.util.Optional;
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
 
-@SuppressWarnings("null")
 record ValidatedTreeNode(@Nullable TreeNode treeNode) {
-    ValidatedTreeNode(final Optional<? extends TreeNode> optional) {
-        this(optional.orElse(null));
-    }
-
-    @NonNull Optional<TreeNode> toOptional() {
-        return Optional.ofNullable(treeNode);
-    }
+    // Nothing else
 }