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.collect.ImmutableList;
12 import java.io.ObjectStreamException;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.util.HashCodeBuilder;
17 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
18 static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.of(), new HashCodeBuilder<>().build());
19 private static final long serialVersionUID = 1L;
20 private final ImmutableList<PathArgument> path;
21 private transient volatile YangInstanceIdentifier parent;
23 private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
25 this.path = Preconditions.checkNotNull(path, "path must not be null.");
28 static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
29 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
33 public boolean isEmpty() {
34 return path.isEmpty();
38 public FixedYangInstanceIdentifier clone() {
40 return (FixedYangInstanceIdentifier) super.clone();
41 } catch (CloneNotSupportedException e) {
42 throw new IllegalStateException("clone() should be supported", e);
47 public YangInstanceIdentifier getParent() {
52 YangInstanceIdentifier ret = parent;
54 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
63 public YangInstanceIdentifier getAncestor(final int depth) {
64 Preconditions.checkArgument(depth >= 0, "Negative depth is not allowed");
65 Preconditions.checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
67 if (depth == path.size()) {
70 if (depth == path.size() - 1) {
71 // Use the parent cache
74 return YangInstanceIdentifier.create(path.subList(0, depth));
78 public List<PathArgument> getPathArguments() {
83 public List<PathArgument> getReversePathArguments() {
84 return path.reverse();
89 List<PathArgument> tryPathArguments() {
95 List<PathArgument> tryReversePathArguments() {
96 return path.reverse();
100 public PathArgument getLastPathArgument() {
101 return path.isEmpty()? null : path.get(path.size() - 1);
106 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
107 if (skipFromRoot == path.size()) {
108 return EMPTY_INSTANCE;
111 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
112 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
113 for (PathArgument a : newPath) {
117 return new FixedYangInstanceIdentifier(newPath, hash.build());
120 private Object readResolve() throws ObjectStreamException {
121 return path.isEmpty() ? EMPTY_INSTANCE : this;
125 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
126 if (other instanceof FixedYangInstanceIdentifier) {
127 return path.equals(((FixedYangInstanceIdentifier) other).path);
129 return super.pathArgumentsEqual(other);
133 public FixedYangInstanceIdentifier toOptimized() {