2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.data.api;
9 import com.google.common.base.Preconditions;
10 import com.google.common.collect.ImmutableList;
11 import java.io.ObjectStreamException;
12 import java.util.List;
13 import org.opendaylight.yangtools.util.HashCodeBuilder;
15 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier {
16 static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
17 private static final long serialVersionUID = 1L;
18 private final ImmutableList<PathArgument> path;
19 private transient volatile YangInstanceIdentifier parent;
21 private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
23 this.path = Preconditions.checkNotNull(path, "path must not be null.");
26 static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
27 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
31 public boolean isEmpty() {
32 return path.isEmpty();
36 public YangInstanceIdentifier getParent() {
41 YangInstanceIdentifier ret = parent;
43 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
51 public List<PathArgument> getPathArguments() {
56 public List<PathArgument> getReversePathArguments() {
57 return path.reverse();
61 List<PathArgument> tryPathArguments() {
66 List<PathArgument> tryReversePathArguments() {
67 return path.reverse();
71 public PathArgument getLastPathArgument() {
72 return path.isEmpty()? null : path.get(path.size() - 1);
76 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
77 if (skipFromRoot == path.size()) {
78 return EMPTY_INSTANCE;
81 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
82 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
83 for (PathArgument a : newPath) {
87 return new FixedYangInstanceIdentifier(newPath, hash.build());
90 private Object readResolve() throws ObjectStreamException {
91 return path.isEmpty() ? EMPTY_INSTANCE : this;
95 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
96 if (other instanceof FixedYangInstanceIdentifier) {
97 return path.equals(((FixedYangInstanceIdentifier) other).path);
99 return super.pathArgumentsEqual(other);
104 public YangInstanceIdentifier toOptimized() {