*/
package org.opendaylight.yangtools.yang.data.api.schema.tree;
+import static com.google.common.base.Verify.verifyNotNull;
+
+import com.google.common.base.VerifyException;
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;
/**
/**
* Returns a direct child of the node.
*
- * @param child Identifier of child
- * @return Optional with node if the child is existing, {@link Optional#empty()} otherwise.
+ * @param arg Identifier of child
+ * @return A node if the child is existing, {@code null} otherwise.
+ * @throws NullPointerException when {@code child} is null
+ */
+ @Nullable C childByArg(PathArgument arg);
+
+ /**
+ * Returns a direct child of the node.
+ *
+ * @param arg Identifier of child
+ * @return A child node
+ * @throws NullPointerException when {@code child} is null
+ * @throws VerifyException if the child does not exist
+ */
+ default @NonNull C getChildByArg(final PathArgument arg) {
+ return verifyNotNull(childByArg(arg), "Child %s does not exist");
+ }
+
+ /**
+ * Returns a direct child of the node.
+ *
+ * @param arg Identifier of child
+ * @return Optional with node if the child exists, {@link Optional#empty()} otherwise.
* @throws NullPointerException when {@code child} is null
*/
- @NonNull Optional<? extends C> getChild(PathArgument child);
+ @NonNull default Optional<C> findChildByArg(final PathArgument arg) {
+ return Optional.ofNullable(childByArg(arg));
+ }
}
*/
public static <T extends StoreTreeNode<T>> Optional<? extends T> findNode(final T tree,
final YangInstanceIdentifier path) {
- Optional<? extends T> current = Optional.of(tree);
+ T current = tree;
Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
- while (current.isPresent() && pathIter.hasNext()) {
- current = current.get().getChild(pathIter.next());
+ while (current != null && pathIter.hasNext()) {
+ current = current.childByArg(pathIter.next());
}
- return current;
+ return Optional.ofNullable(current);
}
public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
int depth = 1;
for (PathArgument pathArg : path.getPathArguments()) {
- Optional<? extends T> potential = current.getChild(pathArg);
- if (potential.isEmpty()) {
+ current = current.childByArg(pathArg);
+ if (current == null) {
throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
path.getAncestor(depth)));
}
- current = potential.get();
++depth;
}
return current;
public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree,
final YangInstanceIdentifier path, final Predicate<T> predicate) {
- Optional<? extends T> parent = Optional.of(tree);
- Optional<? extends T> current = Optional.of(tree);
+ T parent = tree;
+ T current = tree;
int nesting = 0;
Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
- while (current.isPresent() && pathIter.hasNext() && !predicate.test(current.get())) {
+ while (current != null && pathIter.hasNext() && !predicate.test(current)) {
parent = current;
- current = current.get().getChild(pathIter.next());
+ current = current.childByArg(pathIter.next());
nesting++;
}
- if (current.isPresent()) {
+ if (current != null) {
final YangInstanceIdentifier currentPath = path.getAncestor(nesting);
- return new SimpleImmutableEntry<>(currentPath, current.get());
+ return new SimpleImmutableEntry<>(currentPath, current);
}
/*
* present. At any rate we verify state just to be on the safe side.
*/
verify(nesting > 0);
- return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get());
+ return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent);
}
public static <T extends StoreTreeNode<T>> Optional<? extends T> getChild(final Optional<T> parent,
final PathArgument child) {
if (parent.isPresent()) {
- return parent.get().getChild(child);
+ return parent.get().findChildByArg(child);
}
return Optional.empty();
}
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
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;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();
}
- protected final Optional<TreeNode> getChildFromData(final PathArgument childId) {
+ protected final @Nullable TreeNode getChildFromData(final PathArgument childId) {
// We do not cache the instantiated node as it is dirt cheap
- return Optional.ofNullable(getChildFromData(castData(), childId, getVersion()));
+ return getChildFromData(castData(), childId, getVersion());
}
static TreeNode getChildFromData(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data,
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.Collections2;
import java.util.Map;
-import java.util.Optional;
import org.opendaylight.yangtools.util.MapAdaptor;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
}
@Override
- public Optional<TreeNode> getChild(final PathArgument childId) {
+ public TreeNode childByArg(final PathArgument arg) {
final TreeNode modified;
- return (modified = getModifiedChild(childId)) == null ? getChildFromData(childId) : Optional.of(modified);
+ return (modified = getModifiedChild(arg)) == null ? getChildFromData(arg) : modified;
}
@Override
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
import java.util.Map;
-import java.util.Optional;
import org.opendaylight.yangtools.util.MapAdaptor;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
}
@Override
- public Optional<TreeNode> getChild(final PathArgument childId) {
- final TreeNode modified = getModifiedChild(childId);
- if (modified != null) {
- return Optional.of(modified);
- }
-
- return Optional.ofNullable(AbstractContainerNode.getChildFromData(getData(), childId, getVersion()));
+ public TreeNode childByArg(final PathArgument arg) {
+ final TreeNode modified = getModifiedChild(arg);
+ return modified != null ? modified : AbstractContainerNode.getChildFromData(getData(), arg, getVersion());
}
}
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
import java.util.Map;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
}
@Override
- public Optional<TreeNode> getChild(final PathArgument childId) {
- return Optional.ofNullable(getModifiedChild(childId));
+ public TreeNode childByArg(final PathArgument arg) {
+ return getModifiedChild(arg);
}
@Override
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
import java.util.Map;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
final class MaterializedMutableContainerNode extends AbstractMutableContainerNode {
}
@Override
- public Optional<TreeNode> getChild(final PathArgument child) {
- return Optional.ofNullable(getModifiedChild(child));
+ public TreeNode childByArg(final PathArgument arg) {
+ return getModifiedChild(arg);
}
}
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
}
@Override
- public Optional<TreeNode> getChild(final PathArgument child) {
- return getChildFromData(child);
+ public TreeNode childByArg(final PathArgument arg) {
+ return getChildFromData(arg);
}
@Override
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.slf4j.Logger;
}
@Override
- public Optional<TreeNode> getChild(final PathArgument childId) {
- LOG.warn("Attempted to access child {} of value-node {}", childId, this);
- return Optional.empty();
+ public TreeNode childByArg(final PathArgument arg) {
+ LOG.warn("Attempted to access child {} of value-node {}", arg, this);
+ return null;
}
@Override
}
private static TreeNode childMeta(final TreeNode parent, final PathArgument id) {
- return parent == null ? null : parent.getChild(id).orElse(null);
+ return parent == null ? null : parent.childByArg(id);
}
private static boolean canHaveChildren(final @Nullable TreeNode oldMeta, final @Nullable TreeNode newMeta) {
case APPEARED:
case DISAPPEARED:
case SUBTREE_MODIFIED:
- return mod.getChild(identifier).map(this::childNode);
+ final ModifiedNode child = mod.childByArg(identifier);
+ return child == null ? Optional.empty() : Optional.of(childNode(child));
case UNMODIFIED:
if (!canHaveChildren(oldMeta, newMeta)) {
return Optional.empty();
return entryStrategy.getSchema();
}
- final Optional<ModificationApplyOperation> entryStrategy() {
- return Optional.of(entryStrategy);
+ final @NonNull ModificationApplyOperation entryStrategy() {
+ return entryStrategy;
}
@Override
if (verifyChildrenStructure) {
final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) writtenValue;
for (final NormalizedNode<?, ?> child : container.getValue()) {
- final Optional<ModificationApplyOperation> childOp = getChild(child.getIdentifier());
- if (childOp.isPresent()) {
- childOp.get().fullVerifyStructure(child);
- } else {
+ final ModificationApplyOperation childOp = childByArg(child.getIdentifier());
+ if (childOp == null) {
throw new SchemaValidationFailedException(String.format(
- "Node %s is not a valid child of %s according to the schema.",
- child.getIdentifier(), container.getIdentifier()));
+ "Node %s is not a valid child of %s according to the schema.",
+ child.getIdentifier(), container.getIdentifier()));
}
+ childOp.fullVerifyStructure(child);
}
optionalVerifyValueChildren(writtenValue);
protected final void recursivelyVerifyStructure(final NormalizedNode<?, ?> value) {
final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) value;
for (final NormalizedNode<?, ?> child : container.getValue()) {
- final Optional<ModificationApplyOperation> childOp = getChild(child.getIdentifier());
- if (!childOp.isPresent()) {
+ final ModificationApplyOperation childOp = childByArg(child.getIdentifier());
+ if (childOp == null) {
throw new SchemaValidationFailedException(
String.format("Node %s is not a valid child of %s according to the schema.",
child.getIdentifier(), container.getIdentifier()));
}
- childOp.get().recursivelyVerifyStructure(child);
+ childOp.recursivelyVerifyStructure(child);
}
}
for (final ModifiedNode mod : modifications) {
final PathArgument id = mod.getIdentifier();
- final Optional<? extends TreeNode> cm = meta.getChild(id);
+ final Optional<? extends TreeNode> cm = meta.findChildByArg(id);
final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
if (result.isPresent()) {
final TreeNode current, final Version version) throws DataValidationFailedException {
for (final NodeModification childMod : modification.getChildren()) {
final PathArgument childId = childMod.getIdentifier();
- final Optional<? extends TreeNode> childMeta = current.getChild(childId);
+ final Optional<? extends TreeNode> childMeta = current.findChildByArg(childId);
path.push(childId);
try {
import java.util.Collection;
import java.util.Iterator;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
abstract class AbstractReadyIterator {
// through it via re-entering this method on the child iterator.
while (children.hasNext()) {
final ModifiedNode child = children.next();
- final Optional<ModificationApplyOperation> childOperation = op.getChild(child.getIdentifier());
- checkState(childOperation.isPresent(), "Schema for child %s is not present.", child.getIdentifier());
+ final ModificationApplyOperation childOp = op.childByArg(child.getIdentifier());
+ checkState(childOp != null, "Schema for child %s is not present.", child.getIdentifier());
final Collection<ModifiedNode> grandChildren = child.getChildren();
- final ModificationApplyOperation childOp = childOperation.get();
if (grandChildren.isEmpty()) {
// The child is empty, seal it
}
@Override
- public final Optional<ModificationApplyOperation> getChild(final PathArgument child) {
- return delegate.getChild(child);
+ public final ModificationApplyOperation childByArg(final PathArgument arg) {
+ return delegate.childByArg(arg);
}
@Override
}
@Override
- public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
- return Optional.ofNullable(childNodes.get(child));
+ public ModificationApplyOperation childByArg(final PathArgument arg) {
+ return childNodes.get(arg);
}
@Override
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
}
@Override
- public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
+ public final ModificationApplyOperation childByArg(final PathArgument arg) {
final ImmutableMap<PathArgument, ModificationApplyOperation> local = children;
- final ModificationApplyOperation existing = local.get(identifier);
+ final ModificationApplyOperation existing = local.get(arg);
if (existing != null) {
- return Optional.of(existing);
+ return existing;
}
- final ModificationApplyOperation childOperation = resolveChild(identifier);
- return childOperation != null ? appendChild(local, identifier, childOperation) : Optional.empty();
+ final ModificationApplyOperation childOperation = resolveChild(arg);
+ return childOperation != null ? appendChild(local, arg, childOperation) : null;
}
private ModificationApplyOperation resolveChild(final PathArgument identifier) {
}
}
- private Optional<ModificationApplyOperation> appendChild(
+ private @Nullable ModificationApplyOperation appendChild(
final ImmutableMap<PathArgument, ModificationApplyOperation> initial, final PathArgument identifier,
final ModificationApplyOperation computed) {
// Attempt to install the updated map
if (UPDATER.compareAndSet(this, previous, updated)) {
- return Optional.of(computed);
+ return computed;
}
// We have raced, acquire a new snapshot, recheck presence and retry if needed
previous = children;
final ModificationApplyOperation raced = previous.get(identifier);
if (raced != null) {
- return Optional.of(raced);
+ return raced;
}
}
}
int depth = 1;
for (final PathArgument pathArg : path.getPathArguments()) {
- final Optional<ModificationApplyOperation> potential = operation.getChild(pathArg);
- if (!potential.isPresent()) {
+ operation = operation.childByArg(pathArg);
+ if (operation == null) {
throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.",
path.getAncestor(depth)));
}
- operation = potential.get();
++depth;
modification = modification.modifyChild(pathArg, operation, version);
getParent().upgradeIfPossible();
final OperationWithModification op = stack.peek();
- final Optional<ModificationApplyOperation> potential = op.getApplyOperation().getChild(child);
- if (potential.isPresent()) {
- final ModificationApplyOperation operation = potential.get();
+ final ModificationApplyOperation operation = op.getApplyOperation().childByArg(child);
+ if (operation != null) {
final ModifiedNode modification = op.getModification().modifyChild(child, operation,
getParent().getVersion());
*/
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
-import java.util.Optional;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
}
@Override
- public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
- return identifier instanceof NodeWithValue ? entryStrategy() : Optional.empty();
+ public ModificationApplyOperation childByArg(final PathArgument arg) {
+ return arg instanceof NodeWithValue ? entryStrategy() : null;
}
}
\ No newline at end of file
for (final ModifiedNode mod : modifications) {
final PathArgument id = mod.getIdentifier();
- final Optional<? extends TreeNode> cm = meta.getChild(id);
+ final Optional<? extends TreeNode> cm = meta.findChildByArg(id);
final Optional<? extends TreeNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
if (result.isPresent()) {
}
@Override
- public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
- return child instanceof NodeIdentifier ? Optional.of(entryStrategy) : Optional.empty();
+ public ModificationApplyOperation childByArg(final PathArgument arg) {
+ return arg instanceof NodeIdentifier ? entryStrategy : null;
}
@Override
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
}
@Override
- public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
- return identifier instanceof NodeIdentifierWithPredicates ? entryStrategy() : Optional.empty();
+ public ModificationApplyOperation childByArg(final PathArgument arg) {
+ return arg instanceof NodeIdentifierWithPredicates ? entryStrategy() : null;
}
@Override
* <b>Implementation notes</b>
* <ul>
* <li>Implementations MUST expose all nested suboperations which operates on child nodes expose via
- * {@link #getChild(PathArgument)} method.</li>
+ * {@link #findChildByArg(PathArgument)} method.</li>
* <li>Same suboperations SHOULD be used when invoked via {@link #apply(ModifiedNode, Optional, Version)},
* if applicable.</li>
* <li>There are exactly two base implementations:
abstract void mergeIntoModifiedNode(ModifiedNode modification, NormalizedNode<?, ?> value, Version version);
/**
- * Returns a suboperation for specified tree node.
+ * {@inheritDoc}
*
- * @return Reference to suboperation for specified tree node, {@link Optional#empty()}
- * if suboperation is not supported for specified tree node.
+ * @return Reference to suboperation for specified tree node, {@code null} if suboperation is not supported for
+ * specified tree node.
*/
@Override
- public abstract Optional<ModificationApplyOperation> getChild(PathArgument child);
+ public abstract ModificationApplyOperation childByArg(PathArgument arg);
abstract void recursivelyVerifyStructure(NormalizedNode<?, ?> value);
* @return Child modification if direct child or it's subtree was modified.
*/
@Override
- public Optional<ModifiedNode> getChild(final PathArgument child) {
- return Optional.ofNullable(children.get(child));
+ public ModifiedNode childByArg(final PathArgument arg) {
+ return children.get(arg);
}
private Optional<? extends TreeNode> metadataFromSnapshot(final @NonNull PathArgument child) {
- return original.isPresent() ? original.get().getChild(child) : Optional.empty();
+ return original.isPresent() ? original.get().findChildByArg(child) : Optional.empty();
}
private Optional<? extends TreeNode> metadataFromData(final @NonNull PathArgument child, final Version modVersion) {
writtenOriginal = TreeNodeFactory.createTreeNode(value, modVersion);
}
- return writtenOriginal.getChild(child);
+ return writtenOriginal.findChildByArg(child);
}
/**
* view, one will we instantiated with specified version.
*/
Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
- final Optional<ModifiedNode> maybeChild = modification.getChild(child);
- if (maybeChild.isPresent()) {
- final ModifiedNode childNode = maybeChild.get();
-
+ final ModifiedNode childNode = modification.childByArg(child);
+ if (childNode != null) {
Optional<? extends TreeNode> snapshot = childNode.getSnapshot();
if (snapshot == null) {
// Snapshot is not present, force instantiation
- snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
+ snapshot = applyOperation.getChildByArg(child).apply(childNode, childNode.getOriginal(), version);
}
return snapshot.map(TreeNode::getData);
}
if (snapshot.isPresent()) {
- return snapshot.get().getChild(child).map(TreeNode::getData);
+ return snapshot.get().findChildByArg(child).map(TreeNode::getData);
}
return Optional.empty();
"Node children was modified by other transaction");
}
- protected final ModificationApplyOperation resolveChildOperation(final PathArgument child) {
- final Optional<ModificationApplyOperation> potential = getChild(child);
- checkArgument(potential.isPresent(), "Operation for child %s is not defined.", child);
- return potential.get();
+ protected final @NonNull ModificationApplyOperation resolveChildOperation(final PathArgument child) {
+ final ModificationApplyOperation potential = childByArg(child);
+ checkArgument(potential != null, "Operation for child %s is not defined.", child);
+ return potential;
}
@Override
}
@Override
- public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
+ public ModificationApplyOperation childByArg(final PathArgument arg) {
throw new UnsupportedOperationException("Node " + schema + " is leaf type node. Child nodes not allowed");
}