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