Cleanup checkstyle in yang-{data,model}-api
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / stmt / SchemaNodeIdentifier.java
index 77c36f1b8e6f4ef28084fa9b54db9090a99375e1..12f8ec04f89c12fc2e7873653d215d7c7d75d097 100644 (file)
@@ -7,23 +7,22 @@
  */
 package org.opendaylight.yangtools.yang.model.api.stmt;
 
-import com.google.common.base.Objects;
-import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Preconditions;
 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.Objects;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
- * Represents unique path to the every schema node inside the schema node identifier
- * namespace.
- *
+ * Represents unique path to the every schema node inside the schema node identifier namespace.
  */
 public abstract class SchemaNodeIdentifier implements Immutable {
 
@@ -42,14 +41,14 @@ public abstract class SchemaNodeIdentifier implements Immutable {
 
         @Override
         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Absolute(parent, qname);
+            return new Absolute(parent, Preconditions.checkNotNull(qname));
         }
     }
 
     /**
      * A relative schema node identifier.
      */
-    public static class Relative extends SchemaNodeIdentifier {
+    public static final class Relative extends SchemaNodeIdentifier {
         private Relative(final SchemaNodeIdentifier parent, final QName qname) {
             super(parent, qname);
         }
@@ -61,14 +60,15 @@ public abstract class SchemaNodeIdentifier implements Immutable {
 
         @Override
         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Relative(parent, qname);
+            return new Relative(parent, Preconditions.checkNotNull(qname));
         }
     }
 
     @SuppressWarnings("rawtypes")
     private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
-
+    private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, SchemaPath> SCHEMAPATH_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, SchemaPath.class, "schemaPath");
     /**
      * Shared instance of the conceptual root schema node.
      */
@@ -100,16 +100,21 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      */
     private volatile ImmutableList<QName> legacyPath;
 
+    /**
+     * Cached SchemaPath.
+     */
+    private volatile SchemaPath schemaPath;
+
     protected SchemaNodeIdentifier(final SchemaNodeIdentifier 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;
     }
 
     private ImmutableList<QName> getLegacyPath() {
@@ -122,19 +127,6 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         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();
-    }
-
     /**
      * Constructs new instance of this class with the concrete path.
      *
@@ -145,7 +137,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      *            boolean value which specifies if the path is absolute or
      *            relative
      *
-     * @return A SchemaPath instance.
+     * @return A SchemaNodeIdentifier instance.
      */
     public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
         final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
@@ -244,33 +236,23 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      *         path from the schema node towards the root.
      */
     public Iterable<QName> getPathTowardsRoot() {
-        return new Iterable<QName>() {
+        return () -> new UnmodifiableIterator<QName>() {
+            private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
+
             @Override
-            public Iterator<QName> iterator() {
-                return new Iterator<QName>() {
-                    private SchemaNodeIdentifier current = SchemaNodeIdentifier.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;
+                } else {
+                    throw new NoSuchElementException("No more elements available");
+                }
             }
         };
     }
@@ -293,6 +275,28 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         return qname;
     }
 
+    private SchemaPath createSchemaPath() {
+        final SchemaPath newPath;
+        if (parent == null) {
+            final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
+            newPath = qname == null ? parentPath : parentPath.createChild(qname);
+        } else {
+            newPath = parent.asSchemaPath().createChild(qname);
+        }
+
+        return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
+    }
+
+    /**
+     * Create the {@link SchemaPath} equivalent of this identifier.
+     *
+     * @return SchemaPath equivalent.
+     */
+    public final SchemaPath asSchemaPath() {
+        final SchemaPath ret = schemaPath;
+        return ret != null ? ret : createSchemaPath();
+    }
+
     /**
      * Describes whether schema node identifier is|isn't absolute.
      *
@@ -318,26 +322,12 @@ public abstract class SchemaNodeIdentifier implements Immutable {
             return false;
         }
         final SchemaNodeIdentifier other = (SchemaNodeIdentifier) 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) {