20045cf43ca3a61d0e287a930fb2fe9ae7065195
[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  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.yangtools.yang.data.api;
8
9 import com.google.common.base.Preconditions;
10 import com.google.common.collect.ImmutableList;
11 import java.io.ObjectStreamException;
12 import java.util.List;
13 import org.opendaylight.yangtools.util.HashCodeBuilder;
14
15 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier {
16     static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
17     private static final long serialVersionUID = 1L;
18     private final ImmutableList<PathArgument> path;
19     private transient volatile YangInstanceIdentifier parent;
20
21     private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
22         super(hash);
23         this.path = Preconditions.checkNotNull(path, "path must not be null.");
24     }
25
26     static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
27         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
28     }
29
30     @Override
31     public boolean isEmpty() {
32         return path.isEmpty();
33     }
34
35     @Override
36     public YangInstanceIdentifier getParent() {
37         if (path.isEmpty()) {
38             return null;
39         }
40
41         YangInstanceIdentifier ret = parent;
42         if (ret == null) {
43             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
44             parent = ret;
45         }
46
47         return ret;
48     }
49
50     @Override
51     public List<PathArgument> getPath() {
52         return path;
53     }
54
55     @Override
56     public List<PathArgument> getPathArguments() {
57         return path;
58     }
59
60     @Override
61     public List<PathArgument> getReversePathArguments() {
62         return path.reverse();
63     }
64
65     @Override
66     List<PathArgument> tryPathArguments() {
67         return path;
68     }
69
70     @Override
71     List<PathArgument> tryReversePathArguments() {
72         return path.reverse();
73     }
74
75     @Override
76     public PathArgument getLastPathArgument() {
77         return path.isEmpty()? null : path.get(path.size() - 1);
78     }
79
80     @Override
81     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
82         if (skipFromRoot == path.size()) {
83             return EMPTY_INSTANCE;
84         }
85
86         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
87         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
88         for (PathArgument a : newPath) {
89             hash.addArgument(a);
90         }
91
92         return new FixedYangInstanceIdentifier(newPath, hash.build());
93     }
94
95     private Object readResolve() throws ObjectStreamException {
96         return path.isEmpty() ? EMPTY_INSTANCE : this;
97     }
98
99     @Override
100     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
101         if (other instanceof FixedYangInstanceIdentifier) {
102             return path.equals(((FixedYangInstanceIdentifier) other).path);
103         } else {
104             return super.pathArgumentsEqual(other);
105         }
106     }
107 }