Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeSnapshotCursor.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.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import java.util.Optional;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
21
22 final class InMemoryDataTreeSnapshotCursor extends AbstractCursor<InMemoryDataTreeSnapshot> {
23     private final Deque<NormalizedNodeContainer<?, ?, ?>> stack = new ArrayDeque<>();
24
25     InMemoryDataTreeSnapshotCursor(final InMemoryDataTreeSnapshot parent, final YangInstanceIdentifier rootPath,
26             final NormalizedNodeContainer<?, ?, ?> normalizedNode) {
27         super(parent, rootPath);
28         stack.push(normalizedNode);
29     }
30
31     @Override
32     public void enter(final PathArgument child) {
33         final Optional<NormalizedNode<?, ?>> maybeChildNode = NormalizedNodes.getDirectChild(stack.peek(), child);
34         checkArgument(maybeChildNode.isPresent(), "Child %s not found", child);
35
36         final NormalizedNode<?, ?> childNode = maybeChildNode.get();
37         checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", child);
38         stack.push((NormalizedNodeContainer<?, ?, ?>) childNode);
39     }
40
41     @Override
42     @SuppressWarnings("checkstyle:illegalCatch")
43     public void enter(final Iterable<PathArgument> path) {
44         final Optional<NormalizedNode<?, ?>> maybeChildNode = NormalizedNodes.findNode(stack.peek(), path);
45         checkArgument(maybeChildNode.isPresent(), "Child %s not found", path);
46
47         final NormalizedNode<?, ?> childNode = maybeChildNode.get();
48         checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", path);
49
50         int depth = 0;
51         for (PathArgument arg : path) {
52             try {
53                 enter(arg);
54             } catch (Exception e) {
55                 for (int i = 0; i < depth; ++i) {
56                     stack.pop();
57                 }
58                 throw new IllegalArgumentException(e);
59             }
60
61             ++depth;
62         }
63     }
64
65     @Override
66     public void exit(final int depth) {
67         checkArgument(depth >= 0);
68         checkState(depth < stack.size());
69
70         for (int i = 0; i < depth; ++i) {
71             stack.pop();
72         }
73     }
74
75     @Override
76     public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
77         return NormalizedNodes.findNode(stack.peek(), child);
78     }
79 }