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 java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableList;
14 import java.io.ObjectStreamException;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.HashCodeBuilder;
19 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
20 static final @NonNull FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(
21 ImmutableList.of(), new HashCodeBuilder<>().build());
22 private static final long serialVersionUID = 1L;
24 private final ImmutableList<PathArgument> path;
25 private transient volatile YangInstanceIdentifier parent;
27 private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
29 this.path = requireNonNull(path, "path must not be null.");
32 static @NonNull FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
33 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
37 public boolean isEmpty() {
38 return path.isEmpty();
42 public FixedYangInstanceIdentifier clone() {
44 return (FixedYangInstanceIdentifier) super.clone();
45 } catch (CloneNotSupportedException e) {
46 throw new IllegalStateException("clone() should be supported", e);
51 public YangInstanceIdentifier getParent() {
56 YangInstanceIdentifier ret = parent;
58 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
66 public YangInstanceIdentifier getAncestor(final int depth) {
67 checkArgument(depth >= 0, "Negative depth is not allowed");
68 checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
70 if (depth == path.size()) {
73 if (depth == path.size() - 1) {
74 // Use the parent cache
77 return YangInstanceIdentifier.create(path.subList(0, depth));
81 public List<PathArgument> getPathArguments() {
86 public List<PathArgument> getReversePathArguments() {
87 return path.reverse();
91 @NonNull List<PathArgument> tryPathArguments() {
96 @NonNull List<PathArgument> tryReversePathArguments() {
97 return path.reverse();
101 public PathArgument getLastPathArgument() {
102 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() {