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