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.api;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.Iterables;
13 import com.google.common.collect.Lists;
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.io.ObjectOutputStream;
17 import java.lang.reflect.Field;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.annotation.Nonnull;
22 final class StackedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
23 private static final long serialVersionUID = 1L;
24 private static final Field PARENT_FIELD;
29 f = StackedYangInstanceIdentifier.class.getDeclaredField("parent");
30 } catch (NoSuchFieldException | SecurityException e) {
31 throw new ExceptionInInitializerError(e);
33 f.setAccessible(true);
38 private final YangInstanceIdentifier parent;
39 private final PathArgument pathArgument;
41 private transient volatile StackedPathArguments pathArguments;
42 private transient volatile StackedReversePathArguments reversePathArguments;
44 StackedYangInstanceIdentifier(final YangInstanceIdentifier parent, final PathArgument pathArgument,
47 this.parent = Preconditions.checkNotNull(parent);
48 this.pathArgument = Preconditions.checkNotNull(pathArgument);
52 public StackedYangInstanceIdentifier clone() {
54 return (StackedYangInstanceIdentifier) super.clone();
55 } catch (CloneNotSupportedException e) {
56 throw new IllegalStateException("clone() should be supported", e);
61 public YangInstanceIdentifier getParent() {
67 public YangInstanceIdentifier getAncestor(final int depth) {
68 Preconditions.checkArgument(depth >= 0, "Steps cannot be negative");
70 // Calculate how far up our FixedYangInstanceIdentifier ancestor is
72 YangInstanceIdentifier wlk = getParent();
73 while (wlk instanceof StackedYangInstanceIdentifier) {
74 wlk = wlk.getParent();
78 // Guaranteed to come from FixedYangInstanceIdentifier
79 final int fixedDepth = wlk.getPathArguments().size();
80 if (fixedDepth >= depth) {
81 return wlk.getAncestor(depth);
84 // Calculate our depth and check argument
85 final int ourDepth = stackedDepth + fixedDepth;
86 Preconditions.checkArgument(depth <= ourDepth, "Depth %s exceeds maximum depth %s", depth, ourDepth);
88 // Requested depth is covered by the stack, traverse up for specified number of steps
89 final int toWalk = ourDepth - depth;
90 YangInstanceIdentifier result = this;
91 for (int i = 0; i < toWalk; ++i) {
92 result = result.getParent();
99 public boolean isEmpty() {
104 public List<PathArgument> getPathArguments() {
105 StackedPathArguments ret = tryPathArguments();
107 final List<PathArgument> stack = new ArrayList<>();
108 YangInstanceIdentifier current = this;
110 Verify.verify(current instanceof StackedYangInstanceIdentifier);
111 final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) current;
112 stack.add(stacked.getLastPathArgument());
113 current = stacked.getParent();
114 } while (current.tryPathArguments() == null);
116 ret = new StackedPathArguments(current, Lists.reverse(stack));
124 public List<PathArgument> getReversePathArguments() {
125 StackedReversePathArguments ret = tryReversePathArguments();
127 ret = new StackedReversePathArguments(this);
128 reversePathArguments = ret;
134 public PathArgument getLastPathArgument() {
140 StackedPathArguments tryPathArguments() {
141 return pathArguments;
146 StackedReversePathArguments tryReversePathArguments() {
147 return reversePathArguments;
152 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
153 // TODO: can we optimize this one?
154 return YangInstanceIdentifier.create(Iterables.skip(getPathArguments(), skipFromRoot));
158 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
159 if (other instanceof StackedYangInstanceIdentifier) {
160 final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) other;
161 return pathArgument.equals(stacked.pathArgument) && parent.equals(stacked.parent);
163 return super.pathArgumentsEqual(other);
166 private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
167 inputStream.defaultReadObject();
169 final FixedYangInstanceIdentifier p = (FixedYangInstanceIdentifier) inputStream.readObject();
171 PARENT_FIELD.set(this, p);
172 } catch (IllegalArgumentException | IllegalAccessException e) {
173 throw new IOException("Failed to set parent", e);
177 private void writeObject(final ObjectOutputStream outputStream) throws IOException {
178 outputStream.defaultWriteObject();
180 final FixedYangInstanceIdentifier p;
181 if (parent instanceof FixedYangInstanceIdentifier) {
182 p = (FixedYangInstanceIdentifier) parent;
184 p = FixedYangInstanceIdentifier.create(parent.getPathArguments(), parent.hashCode());
186 outputStream.writeObject(p);
190 public YangInstanceIdentifier toOptimized() {
191 return FixedYangInstanceIdentifier.create(getPathArguments());