2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 public final class DataTreeCandidateNodes {
20 private DataTreeCandidateNodes() {
21 throw new UnsupportedOperationException();
24 public static DataTreeCandidateNode fromNormalizedNode(final NormalizedNode<?, ?> node) {
25 return new NormalizedNodeDataTreeCandidateNode(node);
28 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
29 switch (node.getModificationType()) {
31 cursor.delete(node.getIdentifier());
33 case SUBTREE_MODIFIED:
34 cursor.enter(node.getIdentifier());
35 AbstractNodeIterator iterator = new ExitingNodeIterator(null, node.getChildNodes().iterator());
37 iterator = iterator.next(cursor);
38 } while (iterator != null);
44 cursor.write(node.getIdentifier(), node.getDataAfter().get());
47 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
51 public static void applyRootToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
52 switch (node.getModificationType()) {
54 throw new IllegalArgumentException("Can not delete root.");
56 case SUBTREE_MODIFIED:
57 AbstractNodeIterator iterator = new RootNonExitingIterator(node.getChildNodes().iterator());
59 iterator = iterator.next(cursor);
60 } while (iterator != null);
66 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
70 private static abstract class AbstractNodeIterator {
71 private final Iterator<DataTreeCandidateNode> iterator;
73 AbstractNodeIterator(final Iterator<DataTreeCandidateNode> iterator) {
74 this.iterator = Preconditions.checkNotNull(iterator);
77 AbstractNodeIterator next(final DataTreeModificationCursor cursor) {
78 while (iterator.hasNext()) {
79 final DataTreeCandidateNode node = iterator.next();
80 switch (node.getModificationType()) {
82 cursor.delete(node.getIdentifier());
84 case SUBTREE_MODIFIED:
85 final Collection<DataTreeCandidateNode> children = node.getChildNodes();
86 if (!children.isEmpty()) {
87 cursor.enter(node.getIdentifier());
88 return new ExitingNodeIterator(this, children.iterator());
95 cursor.write(node.getIdentifier(), node.getDataAfter().get());
98 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
105 protected abstract @Nullable AbstractNodeIterator getParent();
107 protected abstract void exitNode(DataTreeModificationCursor cursor);
110 private static final class RootNonExitingIterator extends AbstractNodeIterator {
112 protected RootNonExitingIterator(@Nonnull final Iterator<DataTreeCandidateNode> iterator) {
117 protected void exitNode(final DataTreeModificationCursor cursor) {
122 protected AbstractNodeIterator getParent() {
128 private static final class ExitingNodeIterator extends AbstractNodeIterator {
130 private final AbstractNodeIterator parent;
132 public ExitingNodeIterator(@Nullable final AbstractNodeIterator parent,
133 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
135 this.parent = parent;
139 protected AbstractNodeIterator getParent() {
144 protected final void exitNode(final DataTreeModificationCursor cursor) {