Lower SchemaPath#getPath() memory overhead
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / SchemaPath.java
index cc8f6feebd684e317a80085ad86822a431b97d8b..fd4466d0343c7ac21c6634c8e896db72441c70ad 100644 (file)
@@ -12,10 +12,14 @@ 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.Lists;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.ArrayList;
 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;
@@ -40,7 +44,7 @@ public abstract class SchemaPath implements Immutable {
 
         @Override
         protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new AbsoluteSchemaPath(parent, qname);
+            return new AbsoluteSchemaPath(parent, Preconditions.checkNotNull(qname));
         }
     }
 
@@ -59,7 +63,7 @@ public abstract class SchemaPath implements Immutable {
 
         @Override
         protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new RelativeSchemaPath(parent, qname);
+            return new RelativeSchemaPath(parent, Preconditions.checkNotNull(qname));
         }
     }
 
@@ -98,11 +102,17 @@ public abstract class SchemaPath implements Immutable {
      */
     private volatile ImmutableList<QName> legacyPath;
 
+    /**
+     * @deprecated This constructor will be hidden in a future release.
+     * @param parent
+     * @param qname
+     */
+    @Deprecated
     protected SchemaPath(final SchemaPath parent, final QName qname) {
         this.parent = parent;
         this.qname = qname;
 
-        int h = parent == null ? 0 : parent.hashCode();
+        int h = Objects.hashCode(parent);
         if (qname != null) {
             h = h * 31 + qname.hashCode();
         }
@@ -113,7 +123,11 @@ public abstract class SchemaPath implements Immutable {
     private ImmutableList<QName> getLegacyPath() {
         ImmutableList<QName> ret = legacyPath;
         if (ret == null) {
-            ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
+            final List<QName> tmp = new ArrayList<>();
+            for (QName qname : getPathTowardsRoot()) {
+                tmp.add(qname);
+            }
+            ret = ImmutableList.copyOf(Lists.reverse(tmp));
             LEGACYPATH_UPDATER.lazySet(this, ret);
         }
 
@@ -245,7 +259,7 @@ public abstract class SchemaPath implements Immutable {
         return new Iterable<QName>() {
             @Override
             public Iterator<QName> iterator() {
-                return new Iterator<QName>() {
+                return new UnmodifiableIterator<QName>() {
                     private SchemaPath current = SchemaPath.this;
 
                     @Override
@@ -263,11 +277,6 @@ public abstract class SchemaPath implements Immutable {
                             throw new NoSuchElementException("No more elements available");
                         }
                     }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException("Component removal not supported");
-                    }
                 };
             }
         };
@@ -316,21 +325,7 @@ 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