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);
}
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;
/**
* 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();
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);
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;
}
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;
}
@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;
}
}
if (delegate instanceof AbstractValidation) {
- checkApplicable(path, verifyNotNull(modified.validatedNode(delegate, currentMeta)).toOptional());
+ checkApplicable(path, verifyNotNull(modified.validatedNode(delegate, currentMeta)).treeNode());
return;
}
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());
}
}
*/
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;
*/
@FunctionalInterface
interface Apply {
- Optional<? extends TreeNode> apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
+ @Nullable TreeNode apply(ModifiedNode modification, @Nullable TreeNode currentMeta, Version version);
}
/**
// 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 {
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);
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
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;
- }
}
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;
}
@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);
}
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;
}
@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);
}
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;
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 {
* 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();
}
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;
}
@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);
}
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);
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;
}
@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);
}
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;
* @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.
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();
}
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;
}
}
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);
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;
*/
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.
*
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()) {
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);
}
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;
}
@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;
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;
}
};
}
*/
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
}