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, final int hash) {
46 this.parent = Preconditions.checkNotNull(parent);
47 this.pathArgument = Preconditions.checkNotNull(pathArgument);
51 public StackedYangInstanceIdentifier clone() {
53 return (StackedYangInstanceIdentifier) super.clone();
54 } catch (CloneNotSupportedException e) {
55 throw new IllegalStateException("clone() should be supported", e);
60 public YangInstanceIdentifier getParent() {
66 public YangInstanceIdentifier getAncestor(final int depth) {
67 Preconditions.checkArgument(depth >= 0, "Steps cannot be negative");
69 // Calculate how far up our FixedYangInstanceIdentifier ancestor is
71 YangInstanceIdentifier wlk = getParent();
72 while (wlk instanceof StackedYangInstanceIdentifier) {
73 wlk = wlk.getParent();
77 // Guaranteed to come from FixedYangInstanceIdentifier
78 final int fixedDepth = wlk.getPathArguments().size();
79 if (fixedDepth >= depth) {
80 return wlk.getAncestor(depth);
83 // Calculate our depth and check argument
84 final int ourDepth = stackedDepth + fixedDepth;
85 Preconditions.checkArgument(depth <= ourDepth, "Depth %s exceeds maximum depth %s", depth, ourDepth);
87 // Requested depth is covered by the stack, traverse up for specified number of steps
88 final int toWalk = ourDepth - depth;
89 YangInstanceIdentifier result = this;
90 for (int i = 0; i < toWalk; ++i) {
91 result = result.getParent();
98 public boolean isEmpty() {
103 public List<PathArgument> getPathArguments() {
104 StackedPathArguments ret = tryPathArguments();
106 final List<PathArgument> stack = new ArrayList<>();
107 YangInstanceIdentifier current = this;
109 Verify.verify(current instanceof StackedYangInstanceIdentifier);
110 final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) current;
111 stack.add(stacked.getLastPathArgument());
112 current = stacked.getParent();
113 } while (current.tryPathArguments() == null);
115 ret = new StackedPathArguments(current, Lists.reverse(stack));
123 public List<PathArgument> getReversePathArguments() {
124 StackedReversePathArguments ret = tryReversePathArguments();
126 ret = new StackedReversePathArguments(this);
127 reversePathArguments = ret;
133 public PathArgument getLastPathArgument() {
139 StackedPathArguments tryPathArguments() {
140 return pathArguments;
145 StackedReversePathArguments tryReversePathArguments() {
146 return reversePathArguments;
151 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
152 // TODO: can we optimize this one?
153 return YangInstanceIdentifier.create(Iterables.skip(getPathArguments(), skipFromRoot));
157 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
158 if (other instanceof StackedYangInstanceIdentifier) {
159 final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) other;
160 return pathArgument.equals(stacked.pathArgument) && parent.equals(stacked.parent);
162 return super.pathArgumentsEqual(other);
165 private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
166 inputStream.defaultReadObject();
168 final FixedYangInstanceIdentifier p = (FixedYangInstanceIdentifier) inputStream.readObject();
170 PARENT_FIELD.set(this, p);
171 } catch (IllegalArgumentException | IllegalAccessException e) {
172 throw new IOException("Failed to set parent", e);
176 private void writeObject(final ObjectOutputStream outputStream) throws IOException {
177 outputStream.defaultWriteObject();
179 final FixedYangInstanceIdentifier p;
180 if (parent instanceof FixedYangInstanceIdentifier) {
181 p = (FixedYangInstanceIdentifier) parent;
183 p = FixedYangInstanceIdentifier.create(parent.getPathArguments(), parent.hashCode());
185 outputStream.writeObject(p);
189 public YangInstanceIdentifier toOptimized() {
190 return FixedYangInstanceIdentifier.create(getPathArguments());