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(),
19 new HashCodeBuilder<>().build());
20 private static final long serialVersionUID = 1L;
22 private final ImmutableList<PathArgument> path;
23 private transient volatile YangInstanceIdentifier parent;
25 private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
27 this.path = Preconditions.checkNotNull(path, "path must not be null.");
30 static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
31 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
35 public boolean isEmpty() {
36 return path.isEmpty();
40 public FixedYangInstanceIdentifier clone() {
42 return (FixedYangInstanceIdentifier) super.clone();
43 } catch (CloneNotSupportedException e) {
44 throw new IllegalStateException("clone() should be supported", e);
49 public YangInstanceIdentifier getParent() {
54 YangInstanceIdentifier ret = parent;
56 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
65 public YangInstanceIdentifier getAncestor(final int depth) {
66 Preconditions.checkArgument(depth >= 0, "Negative depth is not allowed");
67 Preconditions.checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
69 if (depth == path.size()) {
72 if (depth == path.size() - 1) {
73 // Use the parent cache
76 return YangInstanceIdentifier.create(path.subList(0, depth));
80 public List<PathArgument> getPathArguments() {
85 public List<PathArgument> getReversePathArguments() {
86 return path.reverse();
91 List<PathArgument> tryPathArguments() {
97 List<PathArgument> tryReversePathArguments() {
98 return path.reverse();
102 public PathArgument getLastPathArgument() {
103 return path.isEmpty() ? null : path.get(path.size() - 1);
108 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
109 if (skipFromRoot == path.size()) {
110 return EMPTY_INSTANCE;
113 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
114 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
115 for (PathArgument a : newPath) {
119 return new FixedYangInstanceIdentifier(newPath, hash.build());
122 private Object readResolve() throws ObjectStreamException {
123 return path.isEmpty() ? EMPTY_INSTANCE : this;
127 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
128 if (other instanceof FixedYangInstanceIdentifier) {
129 return path.equals(((FixedYangInstanceIdentifier) other).path);
131 return super.pathArgumentsEqual(other);
135 public FixedYangInstanceIdentifier toOptimized() {