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.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
22 final class InMemoryDataTreeSnapshotCursor extends AbstractCursor<InMemoryDataTreeSnapshot> {
23 private final Deque<NormalizedNodeContainer<?, ?, ?>> stack = new ArrayDeque<>();
25 InMemoryDataTreeSnapshotCursor(final InMemoryDataTreeSnapshot parent, final YangInstanceIdentifier rootPath,
26 final NormalizedNodeContainer<?, ?, ?> normalizedNode) {
27 super(parent, rootPath);
28 stack.push(normalizedNode);
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);
36 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
37 checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", child);
38 stack.push((NormalizedNodeContainer<?, ?, ?>) childNode);
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);
47 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
48 checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", path);
51 for (PathArgument arg : path) {
54 } catch (Exception e) {
55 for (int i = 0; i < depth; ++i) {
58 throw new IllegalArgumentException(e);
66 public void exit(final int depth) {
67 checkArgument(depth >= 0);
68 checkState(depth < stack.size());
70 for (int i = 0; i < depth; ++i) {
76 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
77 return NormalizedNodes.findNode(stack.peek(), child);