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 org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 public final class DataTreeCandidateNodes {
18 private DataTreeCandidateNodes() {
19 throw new UnsupportedOperationException();
22 public static DataTreeCandidateNode fromNormalizedNode(final NormalizedNode<?, ?> node) {
23 return new NormalizedNodeDataTreeCandidateNode(node);
26 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
27 switch (node.getModificationType()) {
29 cursor.delete(node.getIdentifier());
31 case SUBTREE_MODIFIED:
32 cursor.enter(node.getIdentifier());
33 NodeIterator iterator = new NodeIterator(null, node.getChildNodes().iterator());
35 iterator = iterator.next(cursor);
36 } while (iterator != null);
42 cursor.write(node.getIdentifier(), node.getDataAfter().get());
45 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
49 private static final class NodeIterator {
50 private final Iterator<DataTreeCandidateNode> iterator;
51 private final NodeIterator parent;
53 NodeIterator(final NodeIterator parent, final Iterator<DataTreeCandidateNode> iterator) {
54 this.parent = Preconditions.checkNotNull(parent);
55 this.iterator = Preconditions.checkNotNull(iterator);
58 NodeIterator next(final DataTreeModificationCursor cursor) {
59 while (iterator.hasNext()) {
60 final DataTreeCandidateNode node = iterator.next();
61 switch (node.getModificationType()) {
63 cursor.delete(node.getIdentifier());
65 case SUBTREE_MODIFIED:
66 final Collection<DataTreeCandidateNode> children = node.getChildNodes();
67 if (!children.isEmpty()) {
68 cursor.enter(node.getIdentifier());
69 return new NodeIterator(this, children.iterator());
76 cursor.write(node.getIdentifier(), node.getDataAfter().get());
79 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());