df9c0842f03130f29e51129f960a812d6f69e23e
[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 org.opendaylight.yangtools.util.HashCodeBuilder;
15
16 final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
17     static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
18     private static final long serialVersionUID = 1L;
19     private final ImmutableList<PathArgument> path;
20     private transient volatile YangInstanceIdentifier parent;
21
22     private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
23         super(hash);
24         this.path = Preconditions.checkNotNull(path, "path must not be null.");
25     }
26
27     static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
28         return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
29     }
30
31     @Override
32     public boolean isEmpty() {
33         return path.isEmpty();
34     }
35
36     @Override
37     public FixedYangInstanceIdentifier clone() {
38         return new FixedYangInstanceIdentifier(path, hashCode());
39     }
40
41     @Override
42     public YangInstanceIdentifier getParent() {
43         if (path.isEmpty()) {
44             return null;
45         }
46
47         YangInstanceIdentifier ret = parent;
48         if (ret == null) {
49             ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
50             parent = ret;
51         }
52
53         return ret;
54     }
55
56     @Override
57     public List<PathArgument> getPathArguments() {
58         return path;
59     }
60
61     @Override
62     public List<PathArgument> getReversePathArguments() {
63         return path.reverse();
64     }
65
66     @Override
67     List<PathArgument> tryPathArguments() {
68         return path;
69     }
70
71     @Override
72     List<PathArgument> tryReversePathArguments() {
73         return path.reverse();
74     }
75
76     @Override
77     public PathArgument getLastPathArgument() {
78         return path.isEmpty()? null : path.get(path.size() - 1);
79     }
80
81     @Override
82     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
83         if (skipFromRoot == path.size()) {
84             return EMPTY_INSTANCE;
85         }
86
87         final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
88         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
89         for (PathArgument a : newPath) {
90             hash.addArgument(a);
91         }
92
93         return new FixedYangInstanceIdentifier(newPath, hash.build());
94     }
95
96     private Object readResolve() throws ObjectStreamException {
97         return path.isEmpty() ? EMPTY_INSTANCE : this;
98     }
99
100     @Override
101     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
102         if (other instanceof FixedYangInstanceIdentifier) {
103             return path.equals(((FixedYangInstanceIdentifier) other).path);
104         } else {
105             return super.pathArgumentsEqual(other);
106         }
107     }
108
109     @Override
110     public YangInstanceIdentifier toOptimized() {
111         return this;
112     }
113 }