Remove SchemaPath FIXMEs
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaPath.java
1 /*
2  * Copyright (c) 2013 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.model.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.UnmodifiableIterator;
18 import java.util.Arrays;
19 import java.util.NoSuchElementException;
20 import java.util.Objects;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.QName;
24
25 /**
26  * Represents unique path to the every node inside the module.
27  */
28 public abstract class SchemaPath implements Immutable {
29
30     /**
31      * An absolute SchemaPath.
32      */
33     private static final class AbsoluteSchemaPath extends SchemaPath {
34         private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
35             super(parent, qname);
36         }
37
38         @Override
39         public boolean isAbsolute() {
40             return true;
41         }
42
43         @Override
44         public AbsoluteSchemaPath createChild(final QName element) {
45             return new AbsoluteSchemaPath(this, requireNonNull(element));
46         }
47     }
48
49     /**
50      * A relative SchemaPath.
51      */
52     private static final class RelativeSchemaPath extends SchemaPath {
53         private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
54             super(parent, qname);
55         }
56
57         @Override
58         public boolean isAbsolute() {
59             return false;
60         }
61
62         @Override
63         public RelativeSchemaPath createChild(final QName element) {
64             return new RelativeSchemaPath(this, requireNonNull(element));
65         }
66     }
67
68     /**
69      * Shared instance of the conceptual root schema node.
70      */
71     public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
72
73     /**
74      * Shared instance of the "same" relative schema node.
75      */
76     public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
77
78     /**
79      * Parent path.
80      */
81     private final SchemaPath parent;
82
83     /**
84      * This component.
85      */
86     private final QName qname;
87
88     /**
89      * Cached hash code. We can use this since we are immutable.
90      */
91     private final int hash;
92
93     SchemaPath(final SchemaPath parent, final QName qname) {
94         this.parent = parent;
95         this.qname = qname;
96
97         int tmp = Objects.hashCode(parent);
98         if (qname != null) {
99             tmp = tmp * 31 + qname.hashCode();
100         }
101
102         hash = tmp;
103     }
104
105     /**
106      * Constructs new instance of this class with the concrete path.
107      *
108      * @param path
109      *            list of QName instances which specifies exact path to the
110      *            module node
111      * @param absolute
112      *            boolean value which specifies if the path is absolute or
113      *            relative
114      *
115      * @return A SchemaPath instance.
116      */
117     public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
118         return (absolute ? ROOT : SAME).createChild(path);
119     }
120
121     /**
122      * Constructs new instance of this class with the concrete path.
123      *
124      * @param absolute
125      *            boolean value which specifies if the path is absolute or
126      *            relative
127      * @param element
128      *            a single QName which specifies exact path to the
129      *            module node
130      *
131      * @return A SchemaPath instance.
132      */
133     public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
134         return (absolute ? ROOT : SAME).createChild(element);
135     }
136
137     /**
138      * Constructs new instance of this class with the concrete path.
139      *
140      * @param absolute
141      *            boolean value which specifies if the path is absolute or
142      *            relative
143      * @param path
144      *            one or more QName instances which specifies exact path to the
145      *            module node
146      *
147      * @return A SchemaPath instance.
148      */
149     public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
150         return create(Arrays.asList(path), absolute);
151     }
152
153     /**
154      * Create a child path based on concatenation of this path and a relative path.
155      *
156      * @param relative Relative path
157      * @return A new child path
158      */
159     public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
160         if (Iterables.isEmpty(relative)) {
161             return this;
162         }
163
164         SchemaPath parentPath = this;
165         for (QName item : relative) {
166             parentPath = parentPath.createChild(item);
167         }
168
169         return parentPath;
170     }
171
172     /**
173      * Create a child path based on concatenation of this path and a relative path.
174      *
175      * @param relative Relative SchemaPath
176      * @return A new child path
177      */
178     public @NonNull SchemaPath createChild(final SchemaPath relative) {
179         checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
180         return createChild(relative.getPathFromRoot());
181     }
182
183     /**
184      * Create a child path based on concatenation of this path and an additional path element.
185      *
186      * @param element Relative SchemaPath elements
187      * @return A new child path
188      */
189     public abstract @NonNull SchemaPath createChild(QName element);
190
191     /**
192      * Create a child path based on concatenation of this path and additional
193      * path elements.
194      *
195      * @param elements Relative SchemaPath elements
196      * @return A new child path
197      */
198     public @NonNull SchemaPath createChild(final QName... elements) {
199         return createChild(Arrays.asList(elements));
200     }
201
202     /**
203      * Returns the list of nodes which need to be traversed to get from the
204      * starting point (root for absolute SchemaPaths) to the node represented
205      * by this object.
206      *
207      * @return list of <code>qname</code> instances which represents
208      *         path from the root to the schema node.
209      */
210     public Iterable<QName> getPathFromRoot() {
211         if (qname == null) {
212             return ImmutableList.of();
213         }
214         return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
215     }
216
217     /**
218      * Returns the list of nodes which need to be traversed to get from this
219      * node to the starting point (root for absolute SchemaPaths).
220      *
221      * @return list of <code>qname</code> instances which represents
222      *         path from the schema node towards the root.
223      */
224     public Iterable<QName> getPathTowardsRoot() {
225         return () -> new UnmodifiableIterator<>() {
226             private SchemaPath current = SchemaPath.this;
227
228             @Override
229             public boolean hasNext() {
230                 return current.parent != null;
231             }
232
233             @Override
234             public QName next() {
235                 if (current.parent != null) {
236                     final QName ret = current.qname;
237                     current = current.parent;
238                     return ret;
239                 }
240
241                 throw new NoSuchElementException("No more elements available");
242             }
243         };
244     }
245
246     /**
247      * Returns the immediate parent SchemaPath.
248      *
249      * @return Parent path, null if this SchemaPath is already toplevel.
250      */
251     public SchemaPath getParent() {
252         return parent;
253     }
254
255     /**
256      * Get the last component of this path.
257      *
258      * @return The last component of this path.
259      */
260     public final QName getLastComponent() {
261         return qname;
262     }
263
264     /**
265      * Describes whether schema path is|isn't absolute.
266      *
267      * @return boolean value which is <code>true</code> if schema path is
268      *         absolute.
269      */
270     public abstract boolean isAbsolute();
271
272     @Override
273     public final int hashCode() {
274         return hash;
275     }
276
277     @Override
278     public boolean equals(final Object obj) {
279         if (this == obj) {
280             return true;
281         }
282         if (obj == null) {
283             return false;
284         }
285         if (getClass() != obj.getClass()) {
286             return false;
287         }
288         final SchemaPath other = (SchemaPath) obj;
289         return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
290     }
291
292     @Override
293     public final String toString() {
294         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
295     }
296
297     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
298         return toStringHelper.add("path", getPathFromRoot());
299     }
300 }