ddce29e7642d9f03179c5981513f073e52970ecd
[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(), new HashCodeBuilder<>().build());
19     private static final long serialVersionUID = 1L;
20     private final ImmutableList<PathArgument> path;
21     private transient volatile YangInstanceIdentifier parent;
22
23     private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
24         super(hash);
25         this.path = Preconditions.checkNotNull(path, "path must not be null.");
26     }
27
28     static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
29         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
30     }
31
32     @Override
33     public boolean isEmpty() {
34         return path.isEmpty();
35     }
36
37     @Override
38     public FixedYangInstanceIdentifier clone() {
39         try {
40             return (FixedYangInstanceIdentifier) super.clone();
41         } catch (CloneNotSupportedException e) {
42             throw new IllegalStateException("clone() should be supported", e);
43         }
44     }
45
46     @Override
47     public YangInstanceIdentifier getParent() {
48         if (path.isEmpty()) {
49             return null;
50         }
51
52         YangInstanceIdentifier ret = parent;
53         if (ret == null) {
54             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
55             parent = ret;
56         }
57
58         return ret;
59     }
60
61     @Nonnull
62     @Override
63     public YangInstanceIdentifier getAncestor(final int depth) {
64         Preconditions.checkArgument(depth >= 0, "Negative depth is not allowed");
65         Preconditions.checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size());
66
67         if (depth == path.size()) {
68             return this;
69         }
70         if (depth == path.size() - 1) {
71             // Use the parent cache
72             return getParent();
73         }
74         return YangInstanceIdentifier.create(path.subList(0, depth));
75     }
76
77     @Override
78     public List<PathArgument> getPathArguments() {
79         return path;
80     }
81
82     @Override
83     public List<PathArgument> getReversePathArguments() {
84         return path.reverse();
85     }
86
87     @Nonnull
88     @Override
89     List<PathArgument> tryPathArguments() {
90         return path;
91     }
92
93     @Nonnull
94     @Override
95     List<PathArgument> tryReversePathArguments() {
96         return path.reverse();
97     }
98
99     @Override
100     public PathArgument getLastPathArgument() {
101         return path.isEmpty()? null : path.get(path.size() - 1);
102     }
103
104     @Nonnull
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 }