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 org.opendaylight.yangtools.util.HashCodeBuilder;
16 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
17 static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
18 private static final long serialVersionUID = 1L;
19 private final ImmutableList<PathArgument> path;
20 private transient volatile YangInstanceIdentifier parent;
22 private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
24 this.path = Preconditions.checkNotNull(path, "path must not be null.");
27 static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
28 return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
32 public boolean isEmpty() {
33 return path.isEmpty();
37 public FixedYangInstanceIdentifier clone() {
38 return new FixedYangInstanceIdentifier(path, hashCode());
42 public YangInstanceIdentifier getParent() {
47 YangInstanceIdentifier ret = parent;
49 ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
57 public List<PathArgument> getPathArguments() {
62 public List<PathArgument> getReversePathArguments() {
63 return path.reverse();
67 List<PathArgument> tryPathArguments() {
72 List<PathArgument> tryReversePathArguments() {
73 return path.reverse();
77 public PathArgument getLastPathArgument() {
78 return path.isEmpty()? null : path.get(path.size() - 1);
82 YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
83 if (skipFromRoot == path.size()) {
84 return EMPTY_INSTANCE;
87 final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
88 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
89 for (PathArgument a : newPath) {
93 return new FixedYangInstanceIdentifier(newPath, hash.build());
96 private Object readResolve() throws ObjectStreamException {
97 return path.isEmpty() ? EMPTY_INSTANCE : this;
101 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
102 if (other instanceof FixedYangInstanceIdentifier) {
103 return path.equals(((FixedYangInstanceIdentifier) other).path);
105 return super.pathArgumentsEqual(other);
110 public YangInstanceIdentifier toOptimized() {