Optimize DataTreeCandidate.applyToModification()
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / DataTreeCandidateNodes.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
9
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;
15
16 @Beta
17 public final class DataTreeCandidateNodes {
18     private DataTreeCandidateNodes() {
19         throw new UnsupportedOperationException();
20     }
21
22     public static DataTreeCandidateNode fromNormalizedNode(final NormalizedNode<?, ?> node) {
23         return new NormalizedNodeDataTreeCandidateNode(node);
24     }
25
26     public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
27         switch (node.getModificationType()) {
28         case DELETE:
29             cursor.delete(node.getIdentifier());
30             break;
31         case SUBTREE_MODIFIED:
32             cursor.enter(node.getIdentifier());
33             NodeIterator iterator = new NodeIterator(null, node.getChildNodes().iterator());
34             do {
35                 iterator = iterator.next(cursor);
36             } while (iterator != null);
37             break;
38         case UNMODIFIED:
39             // No-op
40             break;
41         case WRITE:
42             cursor.write(node.getIdentifier(), node.getDataAfter().get());
43             break;
44         default:
45             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
46         }
47     }
48
49     private static final class NodeIterator {
50         private final Iterator<DataTreeCandidateNode> iterator;
51         private final NodeIterator parent;
52
53         NodeIterator(final NodeIterator parent, final Iterator<DataTreeCandidateNode> iterator) {
54             this.parent = Preconditions.checkNotNull(parent);
55             this.iterator = Preconditions.checkNotNull(iterator);
56         }
57
58         NodeIterator next(final DataTreeModificationCursor cursor) {
59             while (iterator.hasNext()) {
60                 final DataTreeCandidateNode node = iterator.next();
61                 switch (node.getModificationType()) {
62                 case DELETE:
63                     cursor.delete(node.getIdentifier());
64                     break;
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());
70                     }
71                     break;
72                 case UNMODIFIED:
73                     // No-op
74                     break;
75                 case WRITE:
76                     cursor.write(node.getIdentifier(), node.getDataAfter().get());
77                     break;
78                 default:
79                     throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
80                 }
81             }
82
83             cursor.exit();
84             return parent;
85         }
86     }
87 }