Cleanup use of Guava library
[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 javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.util.HashCodeBuilder;
18
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;
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 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     @Nonnull
66     @Override
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());
70
71         if (depth == path.size()) {
72             return this;
73         }
74         if (depth == path.size() - 1) {
75             // Use the parent cache
76             return getParent();
77         }
78         return YangInstanceIdentifier.create(path.subList(0, depth));
79     }
80
81     @Override
82     public List<PathArgument> getPathArguments() {
83         return path;
84     }
85
86     @Override
87     public List<PathArgument> getReversePathArguments() {
88         return path.reverse();
89     }
90
91     @Nonnull
92     @Override
93     List<PathArgument> tryPathArguments() {
94         return path;
95     }
96
97     @Nonnull
98     @Override
99     List<PathArgument> tryReversePathArguments() {
100         return path.reverse();
101     }
102
103     @Override
104     public PathArgument getLastPathArgument() {
105         return path.isEmpty() ? null : path.get(path.size() - 1);
106     }
107
108     @Nonnull
109     @Override
110     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
111         if (skipFromRoot == path.size()) {
112             return EMPTY_INSTANCE;
113         }
114
115         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
116         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
117         for (PathArgument a : newPath) {
118             hash.addArgument(a);
119         }
120
121         return new FixedYangInstanceIdentifier(newPath, hash.build());
122     }
123
124     private Object readResolve() throws ObjectStreamException {
125         return path.isEmpty() ? EMPTY_INSTANCE : this;
126     }
127
128     @Override
129     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
130         if (other instanceof FixedYangInstanceIdentifier) {
131             return path.equals(((FixedYangInstanceIdentifier) other).path);
132         }
133         return super.pathArgumentsEqual(other);
134     }
135
136     @Override
137     public FixedYangInstanceIdentifier toOptimized() {
138         return this;
139     }
140 }