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 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;
21 final class InMemoryDataTreeSnapshotCursor extends AbstractCursor<InMemoryDataTreeSnapshot> {
22 private final Deque<NormalizedNodeContainer<?, ?, ?>> stack = new ArrayDeque<>();
24 InMemoryDataTreeSnapshotCursor(final InMemoryDataTreeSnapshot parent, final YangInstanceIdentifier rootPath,
25 final NormalizedNodeContainer<?, ?, ?> normalizedNode) {
26 super(parent, rootPath);
27 stack.push(normalizedNode);
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);
35 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
36 Preconditions.checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", child);
37 stack.push((NormalizedNodeContainer<?, ?, ?>) childNode);
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);
46 final NormalizedNode<?, ?> childNode = maybeChildNode.get();
47 Preconditions.checkArgument(childNode instanceof NormalizedNodeContainer, "Child %s is not a container", path);
50 for (PathArgument arg : path) {
53 } catch (Exception e) {
54 for (int i = 0; i < depth; ++i) {
57 throw new IllegalArgumentException(e);
65 public void exit(final int depth) {
66 Preconditions.checkArgument(depth >= 0);
67 Preconditions.checkState(depth < stack.size());
69 for (int i = 0; i < depth; ++i) {
75 public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
76 return NormalizedNodes.findNode(stack.peek(), child);