ff7499d9fd9f32a7d7a5044f95e1d628c5ab8daa
[yangtools.git] / yang / 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 java.util.Objects.requireNonNull;
12
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;
18
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;
23
24     private final ImmutableList<PathArgument> path;
25     private transient volatile YangInstanceIdentifier parent;
26
27     private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
28         super(hash);
29         this.path = requireNonNull(path, "path must not be null.");
30     }
31
32     static @NonNull FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
33         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
34     }
35
36     @Override
37     public boolean isEmpty() {
38         return path.isEmpty();
39     }
40
41     @Override
42     public FixedYangInstanceIdentifier clone() {
43         try {
44             return (FixedYangInstanceIdentifier) super.clone();
45         } catch (CloneNotSupportedException e) {
46             throw new IllegalStateException("clone() should be supported", e);
47         }
48     }
49
50     @Override
51     public YangInstanceIdentifier getParent() {
52         if (path.isEmpty()) {
53             return null;
54         }
55
56         YangInstanceIdentifier ret = parent;
57         if (ret == null) {
58             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
59             parent = ret;
60         }
61
62         return ret;
63     }
64
65     @Override
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());
69
70         if (depth == path.size()) {
71             return this;
72         }
73         if (depth == path.size() - 1) {
74             // Use the parent cache
75             return getParent();
76         }
77         return YangInstanceIdentifier.create(path.subList(0, depth));
78     }
79
80     @Override
81     public List<PathArgument> getPathArguments() {
82         return path;
83     }
84
85     @Override
86     public List<PathArgument> getReversePathArguments() {
87         return path.reverse();
88     }
89
90     @Override
91     @NonNull List<PathArgument> tryPathArguments() {
92         return path;
93     }
94
95     @Override
96     @NonNull List<PathArgument> tryReversePathArguments() {
97         return path.reverse();
98     }
99
100     @Override
101     public PathArgument getLastPathArgument() {
102         return path.isEmpty() ? null : path.get(path.size() - 1);
103     }
104
105     @Override
106     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
107         if (skipFromRoot == path.size()) {
108             return EMPTY_INSTANCE;
109         }
110
111         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
112         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
113         for (PathArgument a : newPath) {
114             hash.addArgument(a);
115         }
116
117         return new FixedYangInstanceIdentifier(newPath, hash.build());
118     }
119
120     private Object readResolve() throws ObjectStreamException {
121         return path.isEmpty() ? EMPTY_INSTANCE : this;
122     }
123
124     @Override
125     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
126         if (other instanceof FixedYangInstanceIdentifier) {
127             return path.equals(((FixedYangInstanceIdentifier) other).path);
128         }
129         return super.pathArgumentsEqual(other);
130     }
131
132     @Override
133     public FixedYangInstanceIdentifier toOptimized() {
134         return this;
135     }
136 }