X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Fapi%2FSchemaPath.java;h=267e9786e4cd515d221074e051293b3b01955908;hb=a1a533ad48b9c048e8ff84003fc08346ccf27a76;hp=2704d21b3aa582c3a0dc31dd5d05b33e6f37c32f;hpb=62b5d6e245fc399524b12d58c034227e6ef8e0bb;p=yangtools.git
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java
index 2704d21b3a..267e9786e4 100644
--- a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java
+++ b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java
@@ -8,6 +8,7 @@
package org.opendaylight.yangtools.yang.model.api;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import com.google.common.base.MoreObjects;
@@ -16,11 +17,15 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.UnmodifiableIterator;
import java.util.Arrays;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
/**
* Represents unique path to the every node inside the module.
@@ -207,7 +212,7 @@ public abstract class SchemaPath implements Immutable {
* @return list of qname
instances which represents
* path from the root to the schema node.
*/
- public Iterable getPathFromRoot() {
+ public List getPathFromRoot() {
if (qname == null) {
return ImmutableList.of();
}
@@ -269,6 +274,46 @@ public abstract class SchemaPath implements Immutable {
*/
public abstract boolean isAbsolute();
+ /**
+ * Return this path as a {@link SchemaNodeIdentifier}.
+ *
+ * @return A SchemaNodeIdentifier.
+ * @throws IllegalStateException if this path is empty
+ */
+ public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
+ checkState(qname != null, "Cannot convert empty %s", this);
+ final List path = getPathFromRoot();
+ return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
+ }
+
+ /**
+ * Return this path as an {@link Absolute} SchemaNodeIdentifier.
+ *
+ * @return An SchemaNodeIdentifier.
+ * @throws IllegalStateException if this path is empty or is not absolute.
+ */
+ public final Absolute asAbsolute() {
+ final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
+ if (ret instanceof Absolute) {
+ return (Absolute) ret;
+ }
+ throw new IllegalStateException("Path " + this + " is relative");
+ }
+
+ /**
+ * Return this path as an {@link Descendant} SchemaNodeIdentifier.
+ *
+ * @return An SchemaNodeIdentifier.
+ * @throws IllegalStateException if this path is empty or is not relative.
+ */
+ public final Descendant asDescendant() {
+ final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
+ if (ret instanceof Descendant) {
+ return (Descendant) ret;
+ }
+ throw new IllegalStateException("Path " + this + " is absolute");
+ }
+
@Override
public final int hashCode() {
return hash;