Fix InstanceIdentifier.getAncestor() implementations
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / FixedYangInstanceIdentifier.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
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;
13
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;
19
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;
24
25     private final ImmutableList<PathArgument> path;
26     private transient volatile YangInstanceIdentifier parent;
27
28     FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
29         super(hash);
30         this.path = requireNonNull(path, "path must not be null.");
31     }
32
33     static @NonNull FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
34         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
35     }
36
37     @Override
38     public boolean isEmpty() {
39         return path.isEmpty();
40     }
41
42     @Override
43     public FixedYangInstanceIdentifier clone() {
44         try {
45             return (FixedYangInstanceIdentifier) super.clone();
46         } catch (CloneNotSupportedException e) {
47             throw new IllegalStateException("clone() should be supported", e);
48         }
49     }
50
51     @Override
52     public YangInstanceIdentifier getParent() {
53         if (path.isEmpty()) {
54             return null;
55         }
56
57         YangInstanceIdentifier ret = parent;
58         if (ret == null) {
59             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
60             parent = ret;
61         }
62
63         return ret;
64     }
65
66     @Override
67     public YangInstanceIdentifier coerceParent() {
68         return verifyNotNull(getParent(), "Empty instance identifier does not have a parent");
69     }
70
71     @Override
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());
75
76         if (depth == path.size()) {
77             return this;
78         }
79         if (depth == path.size() - 1) {
80             // Use the parent cache
81             return verifyNotNull(getParent());
82         }
83         return YangInstanceIdentifier.create(path.subList(0, depth));
84     }
85
86     @Override
87     public List<PathArgument> getPathArguments() {
88         return path;
89     }
90
91     @Override
92     public List<PathArgument> getReversePathArguments() {
93         return path.reverse();
94     }
95
96     @Override
97     @NonNull List<PathArgument> tryPathArguments() {
98         return path;
99     }
100
101     @Override
102     @NonNull List<PathArgument> tryReversePathArguments() {
103         return path.reverse();
104     }
105
106     @Override
107     public PathArgument getLastPathArgument() {
108         return path.isEmpty() ? null : path.get(path.size() - 1);
109     }
110
111     @Override
112     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
113         if (skipFromRoot == path.size()) {
114             return EMPTY_INSTANCE;
115         }
116
117         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
118         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
119         for (PathArgument a : newPath) {
120             hash.addArgument(a);
121         }
122
123         return new FixedYangInstanceIdentifier(newPath, hash.build());
124     }
125
126     private Object readResolve() throws ObjectStreamException {
127         return path.isEmpty() ? EMPTY_INSTANCE : this;
128     }
129
130     @Override
131     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
132         if (other instanceof FixedYangInstanceIdentifier) {
133             return path.equals(((FixedYangInstanceIdentifier) other).path);
134         }
135         return super.pathArgumentsEqual(other);
136     }
137
138     @Override
139     public FixedYangInstanceIdentifier toOptimized() {
140         return this;
141     }
142 }