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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.collect.ImmutableList;
15 import java.io.ObjectStreamException;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.util.HashCodeBuilder;
20 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
21 static final @NonNull FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(
23 private static final long serialVersionUID = 1L;
25 private final ImmutableList<PathArgument> path;
26 private transient volatile YangInstanceIdentifier parent;
28 FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path) {
29 this.path = requireNonNull(path, "path must not be null.");
32 static @NonNull FixedYangInstanceIdentifier of(final ImmutableList<PathArgument> path) {
33 return path.isEmpty() ? EMPTY_INSTANCE : new FixedYangInstanceIdentifier(path);
36 static @NonNull FixedYangInstanceIdentifier of(final List<PathArgument> path) {
37 return path.isEmpty() ? EMPTY_INSTANCE : new FixedYangInstanceIdentifier(ImmutableList.copyOf(path));
41 public boolean isEmpty() {
42 return path.isEmpty();
46 public FixedYangInstanceIdentifier clone() {
48 return (FixedYangInstanceIdentifier) super.clone();
49 } catch (CloneNotSupportedException e) {
50 throw new IllegalStateException("clone() should be supported", e);
55 public YangInstanceIdentifier getParent() {
60 YangInstanceIdentifier ret = parent;
62 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
70 public YangInstanceIdentifier coerceParent() {
71 return verifyNotNull(getParent(), "Empty instance identifier does not have a parent");
75 public YangInstanceIdentifier getAncestor(final int depth) {
76 checkArgument(depth >= 0, "Negative depth is not allowed");
77 checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
79 if (depth == path.size()) {
82 if (depth == path.size() - 1) {
83 // Use the parent cache
84 return verifyNotNull(getParent());
86 return YangInstanceIdentifier.create(path.subList(0, depth));
90 public List<PathArgument> getPathArguments() {
95 public List<PathArgument> getReversePathArguments() {
96 return path.reverse();
100 @NonNull List<PathArgument> tryPathArguments() {
105 @NonNull List<PathArgument> tryReversePathArguments() {
106 return path.reverse();
110 public PathArgument getLastPathArgument() {
111 return path.isEmpty() ? null : path.get(path.size() - 1);
115 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
116 return skipFromRoot == path.size() ? EMPTY_INSTANCE
117 : new FixedYangInstanceIdentifier(path.subList(skipFromRoot, path.size()));
120 private Object readResolve() throws ObjectStreamException {
121 return path.isEmpty() ? EMPTY_INSTANCE : this;
125 int computeHashCode() {
127 for (PathArgument arg : path) {
128 ret = HashCodeBuilder.nextHashCode(ret, arg);
134 public FixedYangInstanceIdentifier toOptimized() {