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(
22 ImmutableList.of(), new HashCodeBuilder<>().build());
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, final int hash) {
30 this.path = requireNonNull(path, "path must not be null.");
33 static @NonNull FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
34 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
38 public boolean isEmpty() {
39 return path.isEmpty();
43 public FixedYangInstanceIdentifier clone() {
45 return (FixedYangInstanceIdentifier) super.clone();
46 } catch (CloneNotSupportedException e) {
47 throw new IllegalStateException("clone() should be supported", e);
52 public YangInstanceIdentifier getParent() {
57 YangInstanceIdentifier ret = parent;
59 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
67 public YangInstanceIdentifier coerceParent() {
68 return verifyNotNull(getParent(), "Empty instance identifier does not have a parent");
72 public YangInstanceIdentifier getAncestor(final int depth) {
73 checkArgument(depth >= 0, "Negative depth is not allowed");
74 checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
76 if (depth == path.size()) {
79 if (depth == path.size() - 1) {
80 // Use the parent cache
83 return YangInstanceIdentifier.create(path.subList(0, depth));
87 public List<PathArgument> getPathArguments() {
92 public List<PathArgument> getReversePathArguments() {
93 return path.reverse();
97 @NonNull List<PathArgument> tryPathArguments() {
102 @NonNull List<PathArgument> tryReversePathArguments() {
103 return path.reverse();
107 public PathArgument getLastPathArgument() {
108 return path.isEmpty() ? null : path.get(path.size() - 1);
112 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
113 if (skipFromRoot == path.size()) {
114 return EMPTY_INSTANCE;
117 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
118 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
119 for (PathArgument a : newPath) {
123 return new FixedYangInstanceIdentifier(newPath, hash.build());
126 private Object readResolve() throws ObjectStreamException {
127 return path.isEmpty() ? EMPTY_INSTANCE : this;
131 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
132 if (other instanceof FixedYangInstanceIdentifier) {
133 return path.equals(((FixedYangInstanceIdentifier) other).path);
135 return super.pathArgumentsEqual(other);
139 public FixedYangInstanceIdentifier toOptimized() {