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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
20 final class InMemoryDataTreeSnapshotCursor extends AbstractCursor<InMemoryDataTreeSnapshot> {
21 private final Deque<NormalizedNodeContainer<?, ?, ?>> stack = new ArrayDeque<>();
23 InMemoryDataTreeSnapshotCursor(final InMemoryDataTreeSnapshot parent, final YangInstanceIdentifier rootPath, final NormalizedNodeContainer<?, ?, ?> normalizedNode) {
24 super(parent, rootPath);
25 stack.push(normalizedNode);
29 public void enter(final PathArgument child) {
30 final Optional<NormalizedNode<?, ?>> maybeChildNode = NormalizedNodes.getDirectChild(stack.peek(), child);
31 Preconditions.checkArgument(maybeChildNode.isPresent(), "Child %s not found", child);
33 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
34 Preconditions.checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", child);
35 stack.push((NormalizedNodeContainer<?, ?, ?>) childNode);
39 public void enter(final Iterable<PathArgument> path) {
40 final Optional<NormalizedNode<?, ?>> maybeChildNode = NormalizedNodes.findNode(stack.peek(), path);
41 Preconditions.checkArgument(maybeChildNode.isPresent(), "Child %s not found", path);
43 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
44 Preconditions.checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", path);
47 for (PathArgument arg : path) {
50 } catch (Exception e) {
51 for (int i = 0; i < depth; ++i) {
54 throw new IllegalArgumentException(e);
62 public void exit(final int depth) {
63 Preconditions.checkArgument(depth >= 0);
64 Preconditions.checkState(depth < stack.size());
66 for (int i = 0; i < depth; ++i) {
72 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
73 return NormalizedNodes.findNode(stack.peek(), child);