Fix eclipse/checkstyle warnings
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.io.ObjectStreamException;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.util.HashCodeBuilder;
16
17 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
18     static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.of(),
19             new HashCodeBuilder<>().build());
20     private static final long serialVersionUID = 1L;
21
22     private final ImmutableList<PathArgument> path;
23     private transient volatile YangInstanceIdentifier parent;
24
25     private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
26         super(hash);
27         this.path = Preconditions.checkNotNull(path, "path must not be null.");
28     }
29
30     static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
31         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
32     }
33
34     @Override
35     public boolean isEmpty() {
36         return path.isEmpty();
37     }
38
39     @Override
40     public FixedYangInstanceIdentifier clone() {
41         try {
42             return (FixedYangInstanceIdentifier) super.clone();
43         } catch (CloneNotSupportedException e) {
44             throw new IllegalStateException("clone() should be supported", e);
45         }
46     }
47
48     @Override
49     public YangInstanceIdentifier getParent() {
50         if (path.isEmpty()) {
51             return null;
52         }
53
54         YangInstanceIdentifier ret = parent;
55         if (ret == null) {
56             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
57             parent = ret;
58         }
59
60         return ret;
61     }
62
63     @Nonnull
64     @Override
65     public YangInstanceIdentifier getAncestor(final int depth) {
66         Preconditions.checkArgument(depth >= 0, "Negative depth is not allowed");
67         Preconditions.checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
68
69         if (depth == path.size()) {
70             return this;
71         }
72         if (depth == path.size() - 1) {
73             // Use the parent cache
74             return getParent();
75         }
76         return YangInstanceIdentifier.create(path.subList(0, depth));
77     }
78
79     @Override
80     public List<PathArgument> getPathArguments() {
81         return path;
82     }
83
84     @Override
85     public List<PathArgument> getReversePathArguments() {
86         return path.reverse();
87     }
88
89     @Nonnull
90     @Override
91     List<PathArgument> tryPathArguments() {
92         return path;
93     }
94
95     @Nonnull
96     @Override
97     List<PathArgument> tryReversePathArguments() {
98         return path.reverse();
99     }
100
101     @Override
102     public PathArgument getLastPathArgument() {
103         return path.isEmpty() ? null : path.get(path.size() - 1);
104     }
105
106     @Nonnull
107     @Override
108     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
109         if (skipFromRoot == path.size()) {
110             return EMPTY_INSTANCE;
111         }
112
113         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
114         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
115         for (PathArgument a : newPath) {
116             hash.addArgument(a);
117         }
118
119         return new FixedYangInstanceIdentifier(newPath, hash.build());
120     }
121
122     private Object readResolve() throws ObjectStreamException {
123         return path.isEmpty() ? EMPTY_INSTANCE : this;
124     }
125
126     @Override
127     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
128         if (other instanceof FixedYangInstanceIdentifier) {
129             return path.equals(((FixedYangInstanceIdentifier) other).path);
130         }
131         return super.pathArgumentsEqual(other);
132     }
133
134     @Override
135     public FixedYangInstanceIdentifier toOptimized() {
136         return this;
137     }
138 }