Add SchemaPath->SchemaNodeIdentifier conversions
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaPath.java
index b9f26f1b63a5986132cd7eef5da59553e6ad89dc..267e9786e4cd515d221074e051293b3b01955908 100644 (file)
@@ -7,23 +7,31 @@
  */
 package org.opendaylight.yangtools.yang.model.api;
 
-import com.google.common.base.Objects;
-import com.google.common.base.Objects.ToStringHelper;
-import com.google.common.base.Preconditions;
+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;
+import com.google.common.base.MoreObjects.ToStringHelper;
 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.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
-import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+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.
  */
 public abstract class SchemaPath implements Immutable {
+
     /**
      * An absolute SchemaPath.
      */
@@ -38,8 +46,8 @@ public abstract class SchemaPath implements Immutable {
         }
 
         @Override
-        protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new AbsoluteSchemaPath(parent, qname);
+        public AbsoluteSchemaPath createChild(final QName element) {
+            return new AbsoluteSchemaPath(this, requireNonNull(element));
         }
     }
 
@@ -57,24 +65,20 @@ public abstract class SchemaPath implements Immutable {
         }
 
         @Override
-        protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new RelativeSchemaPath(parent, qname);
+        public RelativeSchemaPath createChild(final QName element) {
+            return new RelativeSchemaPath(this, requireNonNull(element));
         }
     }
 
-    @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
-            AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
-
     /**
      * Shared instance of the conceptual root schema node.
      */
-    public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
+    public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
 
     /**
      * Shared instance of the "same" relative schema node.
      */
-    public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
+    public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
 
     /**
      * Parent path.
@@ -91,45 +95,16 @@ public abstract class SchemaPath implements Immutable {
      */
     private final int hash;
 
-    /**
-     * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
-     * is invoked.
-     */
-    private volatile ImmutableList<QName> legacyPath;
-
-    private ImmutableList<QName> getLegacyPath() {
-        ImmutableList<QName> ret = legacyPath;
-        if (ret == null) {
-            ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
-            LEGACYPATH_UPDATER.lazySet(this, ret);
-        }
-
-        return ret;
-    }
-
-    /**
-     * Returns the complete path to schema node.
-     *
-     * @return list of <code>QName</code> instances which represents complete
-     *         path to schema node
-     *
-     * @deprecated Use {@link #getPathFromRoot()} instead.
-     */
-    @Deprecated
-    public List<QName> getPath() {
-        return getLegacyPath();
-    }
-
-    protected SchemaPath(final SchemaPath parent, final QName qname) {
+    SchemaPath(final SchemaPath parent, final QName qname) {
         this.parent = parent;
         this.qname = qname;
 
-        int h = parent == null ? 0 : parent.hashCode();
+        int tmp = Objects.hashCode(parent);
         if (qname != null) {
-            h = h * 31 + qname.hashCode();
+            tmp = tmp * 31 + qname.hashCode();
         }
 
-        hash = h;
+        hash = tmp;
     }
 
     /**
@@ -144,9 +119,8 @@ public abstract class SchemaPath implements Immutable {
      *
      * @return A SchemaPath instance.
      */
-    public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
-        final SchemaPath parent = absolute ? ROOT : SAME;
-        return parent.createChild(path);
+    public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
+        return (absolute ? ROOT : SAME).createChild(path);
     }
 
     /**
@@ -155,24 +129,31 @@ public abstract class SchemaPath implements Immutable {
      * @param absolute
      *            boolean value which specifies if the path is absolute or
      *            relative
-     * @param path
-     *            one or more QName instances which specifies exact path to the
+     * @param element
+     *            a single QName which specifies exact path to the
      *            module node
      *
      * @return A SchemaPath instance.
      */
-    public static SchemaPath create(final boolean absolute, final QName... path) {
-        return create(Arrays.asList(path), absolute);
+    public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
+        return (absolute ? ROOT : SAME).createChild(element);
     }
 
     /**
-     * Create a new instance.
+     * Constructs new instance of this class with the concrete path.
+     *
+     * @param absolute
+     *            boolean value which specifies if the path is absolute or
+     *            relative
+     * @param path
+     *            one or more QName instances which specifies exact path to the
+     *            module node
      *
-     * @param path path from root
-     * @param hash intended hash code
-     * @return A new SchemaPath instance
+     * @return A SchemaPath instance.
      */
-    protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
+    public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
+        return create(Arrays.asList(path), absolute);
+    }
 
     /**
      * Create a child path based on concatenation of this path and a relative path.
@@ -180,17 +161,17 @@ public abstract class SchemaPath implements Immutable {
      * @param relative Relative path
      * @return A new child path
      */
-    public SchemaPath createChild(final Iterable<QName> relative) {
+    public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
         if (Iterables.isEmpty(relative)) {
             return this;
         }
 
-        SchemaPath parent = this;
-        for (QName qname : relative) {
-            parent = parent.createInstance(parent, qname);
+        SchemaPath parentPath = this;
+        for (QName item : relative) {
+            parentPath = parentPath.createChild(item);
         }
 
-        return parent;
+        return parentPath;
     }
 
     /**
@@ -199,17 +180,19 @@ public abstract class SchemaPath implements Immutable {
      * @param relative Relative SchemaPath
      * @return A new child path
      */
-    public SchemaPath createChild(final SchemaPath relative) {
-        Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
-
-        SchemaPath parent = this;
-        for (QName qname : relative.getPathFromRoot()) {
-            parent = parent.createInstance(parent, qname);
-        }
-
-        return parent;
+    public @NonNull SchemaPath createChild(final SchemaPath relative) {
+        checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
+        return createChild(relative.getPathFromRoot());
     }
 
+    /**
+     * Create a child path based on concatenation of this path and an additional path element.
+     *
+     * @param element Relative SchemaPath elements
+     * @return A new child path
+     */
+    public abstract @NonNull SchemaPath createChild(QName element);
+
     /**
      * Create a child path based on concatenation of this path and additional
      * path elements.
@@ -217,7 +200,7 @@ public abstract class SchemaPath implements Immutable {
      * @param elements Relative SchemaPath elements
      * @return A new child path
      */
-    public SchemaPath createChild(final QName... elements) {
+    public @NonNull SchemaPath createChild(final QName... elements) {
         return createChild(Arrays.asList(elements));
     }
 
@@ -229,8 +212,11 @@ public abstract class SchemaPath implements Immutable {
      * @return list of <code>qname</code> instances which represents
      *         path from the root to the schema node.
      */
-    public Iterable<QName> getPathFromRoot() {
-        return getLegacyPath();
+    public List<QName> getPathFromRoot() {
+        if (qname == null) {
+            return ImmutableList.of();
+        }
+        return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
     }
 
     /**
@@ -241,33 +227,23 @@ public abstract class SchemaPath implements Immutable {
      *         path from the schema node towards the root.
      */
     public Iterable<QName> getPathTowardsRoot() {
-        return new Iterable<QName>() {
+        return () -> new UnmodifiableIterator<>() {
+            private SchemaPath current = SchemaPath.this;
+
             @Override
-            public Iterator<QName> iterator() {
-                return new Iterator<QName>() {
-                    private SchemaPath current = SchemaPath.this;
-
-                    @Override
-                    public boolean hasNext() {
-                        return current.parent != null;
-                    }
-
-                    @Override
-                    public QName next() {
-                        if (current.parent != null) {
-                            final QName ret = current.qname;
-                            current = current.parent;
-                            return ret;
-                        } else {
-                            throw new NoSuchElementException("No more elements available");
-                        }
-                    }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException("Component removal not supported");
-                    }
-                };
+            public boolean hasNext() {
+                return current.parent != null;
+            }
+
+            @Override
+            public QName next() {
+                if (current.parent != null) {
+                    final QName ret = current.qname;
+                    current = current.parent;
+                    return ret;
+                }
+
+                throw new NoSuchElementException("No more elements available");
             }
         };
     }
@@ -298,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<QName> 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;
@@ -315,26 +331,12 @@ public abstract class SchemaPath implements Immutable {
             return false;
         }
         final SchemaPath other = (SchemaPath) obj;
-
-        if (qname != null) {
-            if (!qname.equals(other.qname)) {
-                return false;
-            }
-        } else {
-            if (other.qname != null) {
-                return false;
-            }
-        }
-
-        if (parent == null) {
-            return other.parent == null;
-        }
-        return parent.equals(other.parent);
+        return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
     }
 
     @Override
     public final String toString() {
-        return addToStringAttributes(Objects.toStringHelper(this)).toString();
+        return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
     }
 
     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {