Add yang-xpath-api
[yangtools.git] / yang / yang-data-jaxen / src / main / java / org / opendaylight / yangtools / yang / data / jaxen / NormalizedNodeContext.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.jaxen;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Function;
13 import com.google.common.collect.ImmutableList;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.jaxen.Context;
17 import org.jaxen.ContextSupport;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 /**
22  * Context wrapper around a {@link NormalizedNode} for use with Jaxen. It tracks the parent node for purposes of
23  * traversing upwards the NormalizedNode tree.
24  */
25 final class NormalizedNodeContext extends Context implements Function<NormalizedNode<?, ?>, NormalizedNodeContext> {
26     private static final long serialVersionUID = 1L;
27     private final NormalizedNodeContext parent;
28     private final NormalizedNode<?, ?> node;
29
30     NormalizedNodeContext(@Nonnull final ContextSupport contextSupport, @Nonnull final NormalizedNode<?, ?> node,
31         @Nullable final NormalizedNodeContext parent) {
32         super(contextSupport);
33         this.node = requireNonNull(node);
34         this.parent = parent;
35
36         setNodeSet(ImmutableList.of(this));
37     }
38
39     @Nonnull NormalizedNode<?, ?> getNode() {
40         return node;
41     }
42
43     @Nullable NormalizedNodeContext getParent() {
44         return parent;
45     }
46
47     @Nonnull YangInstanceIdentifier getPath() {
48         return (parent == null ? YangInstanceIdentifier.EMPTY : parent.getPath()).node(node.getIdentifier());
49     }
50
51     @Override
52     public NormalizedNodeContext apply(final NormalizedNode<?, ?> input) {
53         return new NormalizedNodeContext(getContextSupport(), input, this);
54     }
55 }