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.impl.schema.tree;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
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;
23 final class InMemoryDataTreeSnapshotCursor extends AbstractCursor<InMemoryDataTreeSnapshot> {
24 private final Deque<DistinctNodeContainer<?, ?>> stack = new ArrayDeque<>();
26 InMemoryDataTreeSnapshotCursor(final InMemoryDataTreeSnapshot parent, final YangInstanceIdentifier rootPath,
27 final DistinctNodeContainer<?, ?> normalizedNode) {
28 super(parent, rootPath);
29 stack.push(normalizedNode);
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);
37 final NormalizedNode childNode = maybeChildNode.get();
38 checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", child);
39 stack.push((DistinctNodeContainer<?, ?>) childNode);
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);
48 final NormalizedNode childNode = maybeChildNode.get();
49 checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", path);
52 for (PathArgument arg : path) {
55 } catch (Exception e) {
56 for (int i = 0; i < depth; ++i) {
59 throw new IllegalArgumentException(e);
67 public void exit(final int depth) {
68 checkArgument(depth >= 0);
69 checkState(depth < stack.size());
71 for (int i = 0; i < depth; ++i) {
77 public Optional<NormalizedNode> readNode(final PathArgument child) {
78 return NormalizedNodes.findNode(stack.peek(), child);