*/
package org.opendaylight.yangtools.yang.data.tree.spi;
+import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
-import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
* @return Collection of changes
*/
public static @NonNull Collection<DataTreeCandidateNode> containerDelta(
- final @Nullable DistinctNodeContainer<PathArgument, NormalizedNode> oldData,
- final @Nullable DistinctNodeContainer<PathArgument, NormalizedNode> newData) {
+ final @Nullable DistinctNodeContainer<?, ?> oldData,
+ final @Nullable DistinctNodeContainer<?, ?> newData) {
if (newData == null) {
return oldData == null ? ImmutableList.of()
: Collections2.transform(oldData.body(), DataTreeCandidateNodes::deleteNode);
* in old data. Based on that we construct replaced/written nodes. We then proceed to
* iterate over old data and looking up each child in new data.
*/
- final Collection<DataTreeCandidateNode> result = new ArrayList<>();
- for (NormalizedNode child : newData.body()) {
+
+ final var result = new ArrayList<DataTreeCandidateNode>();
+ @SuppressWarnings("unchecked")
+ final var oldCast = (DistinctNodeContainer<PathArgument, ?>) oldData;
+ for (var child : newData.body()) {
final DataTreeCandidateNode node;
- final NormalizedNode oldChild = oldData.childByArg(child.getIdentifier());
+ final NormalizedNode oldChild = oldCast.childByArg(child.name());
if (oldChild != null) {
// This does not find children which have not in fact been modified, as doing that
// reliably would require us running a full equals() on the two nodes.
}
// Process removals next, looking into new data to see if we processed it
- for (NormalizedNode child : oldData.body()) {
- if (newData.childByArg(child.getIdentifier()) == null) {
+ @SuppressWarnings("unchecked")
+ final var newCast = (DistinctNodeContainer<PathArgument, ?>) newData;
+ for (var child : oldData.body()) {
+ if (newCast.childByArg(child.name()) == null) {
result.add(deleteNode(child));
}
}
* @param newData New data container, may be null
* @return A {@link DataTreeCandidateNode} describing the change, or empty if the node is not present
*/
- public static @NonNull Optional<DataTreeCandidateNode> containerDelta(
+ public static @Nullable DataTreeCandidateNode containerDelta(
final @Nullable DistinctNodeContainer<PathArgument, NormalizedNode> oldData,
final @Nullable DistinctNodeContainer<PathArgument, NormalizedNode> newData,
final @NonNull PathArgument child) {
final NormalizedNode newChild = getChild(newData, child);
final NormalizedNode oldChild = getChild(oldData, child);
if (oldChild != null) {
- return Optional.of(newChild != null ? replaceNode(oldChild, newChild) : deleteNode(oldChild));
+ return newChild != null ? replaceNode(oldChild, newChild) : deleteNode(oldChild);
} else if (newChild != null) {
- return Optional.of(DataTreeCandidateNodes.writeNode(newChild));
+ return DataTreeCandidateNodes.writeNode(newChild);
} else {
- return Optional.empty();
+ return null;
}
}
* @param node candidate tree to apply
*/
public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
- switch (node.getModificationType()) {
+ switch (node.modificationType()) {
case DELETE:
- cursor.delete(node.getIdentifier());
+ cursor.delete(node.name());
break;
case SUBTREE_MODIFIED:
- cursor.enter(node.getIdentifier());
- AbstractNodeIterator iterator = new ExitingNodeIterator(null, node.getChildNodes().iterator());
+ cursor.enter(node.name());
+ AbstractNodeIterator iterator = new ExitingNodeIterator(null, node.childNodes().iterator());
do {
iterator = iterator.next(cursor);
} while (iterator != null);
// No-op
break;
case WRITE:
- cursor.write(node.getIdentifier(), node.getDataAfter().orElseThrow());
+ cursor.write(node.name(), verifyNotNull(node.dataAfter()));
break;
default:
- throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
+ throw new IllegalArgumentException("Unsupported modification " + node.modificationType());
}
}
*/
public static void applyRootedNodeToCursor(final DataTreeModificationCursor cursor,
final YangInstanceIdentifier rootPath, final DataTreeCandidateNode node) {
- switch (node.getModificationType()) {
+ switch (node.modificationType()) {
case DELETE:
cursor.delete(rootPath.getLastPathArgument());
break;
case SUBTREE_MODIFIED:
cursor.enter(rootPath.getLastPathArgument());
- AbstractNodeIterator iterator = new ExitingNodeIterator(null, node.getChildNodes().iterator());
+ AbstractNodeIterator iterator = new ExitingNodeIterator(null, node.childNodes().iterator());
do {
iterator = iterator.next(cursor);
} while (iterator != null);
// No-op
break;
case WRITE:
- cursor.write(rootPath.getLastPathArgument(), node.getDataAfter().orElseThrow());
+ cursor.write(rootPath.getLastPathArgument(), verifyNotNull(node.dataAfter()));
break;
default:
- throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
+ throw new IllegalArgumentException("Unsupported modification " + node.modificationType());
}
}
public static void applyRootToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
- switch (node.getModificationType()) {
+ switch (node.modificationType()) {
case DELETE:
throw new IllegalArgumentException("Can not delete root.");
case WRITE:
case SUBTREE_MODIFIED:
- AbstractNodeIterator iterator = new RootNonExitingIterator(node.getChildNodes().iterator());
+ AbstractNodeIterator iterator = new RootNonExitingIterator(node.childNodes().iterator());
do {
iterator = iterator.next(cursor);
} while (iterator != null);
// No-op
break;
default:
- throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
+ throw new IllegalArgumentException("Unsupported modification " + node.modificationType());
}
}
final AbstractNodeIterator next(final DataTreeModificationCursor cursor) {
while (iterator.hasNext()) {
final DataTreeCandidateNode node = iterator.next();
- switch (node.getModificationType()) {
+ switch (node.modificationType()) {
case DELETE:
- cursor.delete(node.getIdentifier());
+ cursor.delete(node.name());
break;
case APPEARED:
case DISAPPEARED:
case SUBTREE_MODIFIED:
- final Collection<DataTreeCandidateNode> children = node.getChildNodes();
+ final var children = node.childNodes();
if (!children.isEmpty()) {
- cursor.enter(node.getIdentifier());
+ cursor.enter(node.name());
return new ExitingNodeIterator(this, children.iterator());
}
break;
// No-op
break;
case WRITE:
- cursor.write(node.getIdentifier(), node.getDataAfter().orElseThrow());
+ cursor.write(node.name(), verifyNotNull(node.dataAfter()));
break;
default:
- throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
+ throw new IllegalArgumentException("Unsupported modification " + node.modificationType());
}
}
exitNode(cursor);