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 javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.util.HashCodeBuilder;
19 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
20 static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.of(),
21 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 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));
67 public YangInstanceIdentifier getAncestor(final int depth) {
68 checkArgument(depth >= 0, "Negative depth is not allowed");
69 checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
71 if (depth == path.size()) {
74 if (depth == path.size() - 1) {
75 // Use the parent cache
78 return YangInstanceIdentifier.create(path.subList(0, depth));
82 public List<PathArgument> getPathArguments() {
87 public List<PathArgument> getReversePathArguments() {
88 return path.reverse();
93 List<PathArgument> tryPathArguments() {
99 List<PathArgument> tryReversePathArguments() {
100 return path.reverse();
104 public PathArgument getLastPathArgument() {
105 return path.isEmpty() ? null : path.get(path.size() - 1);
110 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
111 if (skipFromRoot == path.size()) {
112 return EMPTY_INSTANCE;
115 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
116 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
117 for (PathArgument a : newPath) {
121 return new FixedYangInstanceIdentifier(newPath, hash.build());
124 private Object readResolve() throws ObjectStreamException {
125 return path.isEmpty() ? EMPTY_INSTANCE : this;
129 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
130 if (other instanceof FixedYangInstanceIdentifier) {
131 return path.equals(((FixedYangInstanceIdentifier) other).path);
133 return super.pathArgumentsEqual(other);
137 public FixedYangInstanceIdentifier toOptimized() {