Remove unneeded Iterator.remove() overrides
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefPath.java
index 649d911ac3ea88dd79e8fc010bf3da4fb74a7f7e..1b475e4447207ff9aafc69e05e48ec03b86a4bf2 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,24 +7,23 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
-import org.opendaylight.yangtools.concepts.Immutable;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 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;
 
 public abstract class LeafRefPath implements Immutable {
-
     /**
      * An absolute LeafRefPath.
      */
     private static final class AbsoluteLeafRefPath extends LeafRefPath {
-        private AbsoluteLeafRefPath(final LeafRefPath parent,
-                final QNameWithPredicate qname) {
+        private AbsoluteLeafRefPath(final LeafRefPath parent, final QNameWithPredicate qname) {
             super(parent, qname);
         }
 
@@ -34,9 +33,8 @@ public abstract class LeafRefPath implements Immutable {
         }
 
         @Override
-        protected LeafRefPath createInstance(final LeafRefPath parent,
-                final QNameWithPredicate qname) {
-            return new AbsoluteLeafRefPath(parent, qname);
+        protected LeafRefPath createInstance(final LeafRefPath newParent, final QNameWithPredicate newQname) {
+            return new AbsoluteLeafRefPath(newParent, newQname);
         }
     }
 
@@ -44,8 +42,7 @@ public abstract class LeafRefPath implements Immutable {
      * A relative LeafRefPath.
      */
     private static final class RelativeLeafRefPath extends LeafRefPath {
-        private RelativeLeafRefPath(final LeafRefPath parent,
-                final QNameWithPredicate qname) {
+        private RelativeLeafRefPath(final LeafRefPath parent, final QNameWithPredicate qname) {
             super(parent, qname);
         }
 
@@ -55,15 +52,14 @@ public abstract class LeafRefPath implements Immutable {
         }
 
         @Override
-        protected LeafRefPath createInstance(final LeafRefPath parent,
-                final QNameWithPredicate qname) {
-            return new RelativeLeafRefPath(parent, qname);
+        protected LeafRefPath createInstance(final LeafRefPath newParent, final QNameWithPredicate newQname) {
+            return new RelativeLeafRefPath(newParent, newQname);
         }
     }
 
     @SuppressWarnings("rawtypes")
-    private static final AtomicReferenceFieldUpdater<LeafRefPath, ImmutableList> LEGACYPATH_UPDATER = AtomicReferenceFieldUpdater
-            .newUpdater(LeafRefPath.class, ImmutableList.class, "legacyPath");
+    private static final AtomicReferenceFieldUpdater<LeafRefPath, ImmutableList> LEGACYPATH_UPDATER =
+        AtomicReferenceFieldUpdater.newUpdater(LeafRefPath.class, ImmutableList.class, "legacyPath");
 
     /**
      * Shared instance of the conceptual root schema node.
@@ -91,8 +87,7 @@ public abstract class LeafRefPath implements Immutable {
     private final int hash;
 
     /**
-     * Cached legacy path, filled-in when {@link #getPath()} or
-     * {@link #getPathTowardsRoot()} is invoked.
+     * Cached legacy path, filled-in when {@link #getPathFromRoot()} or {@link #getPathTowardsRoot()} is invoked.
      */
     private volatile ImmutableList<QNameWithPredicate> legacyPath;
 
@@ -106,46 +101,26 @@ public abstract class LeafRefPath implements Immutable {
         return ret;
     }
 
-    /**
-     * Returns the complete path to schema node.
-     *
-     * @return list of <code>QNameWithPredicate</code> instances which
-     *         represents complete path to schema node
-     *
-     * @deprecated Use {@link #getPathFromRoot()} instead.
-     */
-    @Deprecated
-    public List<QNameWithPredicate> getPath() {
-        return getLegacyPath();
-    }
-
-    protected LeafRefPath(final LeafRefPath parent,
-            final QNameWithPredicate qname) {
+    protected LeafRefPath(final LeafRefPath parent, final QNameWithPredicate qname) {
         this.parent = parent;
         this.qname = qname;
 
-        int h = parent == null ? 0 : parent.hashCode();
+        int hc = Objects.hashCode(parent);
         if (qname != null) {
-            h = h * 31 + qname.hashCode();
+            hc = hc * 31 + qname.hashCode();
         }
 
-        hash = h;
+        hash = hc;
     }
 
     /**
      * Constructs new instance of this class with the concrete path.
      *
-     * @param path
-     *            list of QNameWithPredicate instances which specifies exact
-     *            path to the module node
-     * @param absolute
-     *            boolean value which specifies if the path is absolute or
-     *            relative
-     *
+     * @param path list of QNameWithPredicate instances which specifies exact path to the module node
+     * @param absolute boolean value which specifies if the path is absolute or relative
      * @return A LeafRefPath instance.
      */
-    public static LeafRefPath create(final Iterable<QNameWithPredicate> path,
-            final boolean absolute) {
+    public static LeafRefPath create(final Iterable<QNameWithPredicate> path, final boolean absolute) {
         final LeafRefPath parent = absolute ? ROOT : SAME;
         return parent.createChild(path);
     }
@@ -153,38 +128,27 @@ public abstract class LeafRefPath implements Immutable {
     /**
      * 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 QNameWithPredicate instances which specifies exact
-     *            path to the module node
-     *
+     * @param absolute boolean value which specifies if the path is absolute or relative
+     * @param path one or more QNameWithPredicate instances which specifies exact path to the module node
      * @return A LeafRefPath instance.
      */
-    public static LeafRefPath create(final boolean absolute,
-            final QNameWithPredicate... path) {
+    public static LeafRefPath create(final boolean absolute, final QNameWithPredicate... path) {
         return create(Arrays.asList(path), absolute);
     }
 
     /**
      * Create a new instance.
      *
-     * @param parent
-     *            Parent LeafRefPath
-     * @param qname
-     *            next path element
+     * @param newParent Parent LeafRefPath
+     * @param newQname next path element
      * @return A new LeafRefPath instance
      */
-    protected abstract LeafRefPath createInstance(LeafRefPath parent,
-            QNameWithPredicate qname);
+    protected abstract LeafRefPath createInstance(LeafRefPath newParent, QNameWithPredicate newQname);
 
     /**
-     * Create a child path based on concatenation of this path and a relative
-     * path.
+     * Create a child path based on concatenation of this path and a relative path.
      *
-     * @param relative
-     *            Relative path
+     * @param relative Relative path
      * @return A new child path
      */
     public LeafRefPath createChild(final Iterable<QNameWithPredicate> relative) {
@@ -192,40 +156,35 @@ public abstract class LeafRefPath implements Immutable {
             return this;
         }
 
-        LeafRefPath parent = this;
-        for (QNameWithPredicate qname : relative) {
-            parent = parent.createInstance(parent, qname);
+        LeafRefPath newParent = this;
+        for (QNameWithPredicate relativeQname : relative) {
+            newParent = newParent.createInstance(newParent, relativeQname);
         }
 
-        return parent;
+        return newParent;
     }
 
     /**
-     * Create a child path based on concatenation of this path and a relative
-     * path.
+     * Create a child path based on concatenation of this path and a relative path.
      *
-     * @param relative
-     *            Relative LeafRefPath
+     * @param relative Relative LeafRefPath
      * @return A new child path
      */
     public LeafRefPath createChild(final LeafRefPath relative) {
-        Preconditions.checkArgument(!relative.isAbsolute(),
-                "Child creation requires relative path");
+        checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
 
-        LeafRefPath parent = this;
-        for (QNameWithPredicate qname : relative.getPathFromRoot()) {
-            parent = parent.createInstance(parent, qname);
+        LeafRefPath newParent = this;
+        for (QNameWithPredicate relativeQname : relative.getPathFromRoot()) {
+            newParent = newParent.createInstance(newParent, relativeQname);
         }
 
-        return parent;
+        return newParent;
     }
 
     /**
-     * Create a child path based on concatenation of this path and additional
-     * path elements.
+     * Create a child path based on concatenation of this path and additional path elements.
      *
-     * @param elements
-     *            Relative LeafRefPath elements
+     * @param elements Relative LeafRefPath elements
      * @return A new child path
      */
     public LeafRefPath createChild(final QNameWithPredicate... elements) {
@@ -233,54 +192,39 @@ public abstract class LeafRefPath implements Immutable {
     }
 
     /**
-     * Returns the list of nodes which need to be traversed to get from the
-     * starting point (root for absolute LeafRefPaths) to the node represented
-     * by this object.
+     * Returns the list of nodes which need to be traversed to get from the starting point (root for absolute
+     * LeafRefPaths) to the node represented by this object.
      *
-     * @return list of <code>qname</code> instances which represents path from
-     *         the root to the schema node.
+     * @return list of {@code qname} instances which represents path from the root to the schema node.
      */
     public Iterable<QNameWithPredicate> getPathFromRoot() {
         return getLegacyPath();
     }
 
     /**
-     * Returns the list of nodes which need to be traversed to get from this
-     * node to the starting point (root for absolute LeafRefPaths).
+     * Returns the list of nodes which need to be traversed to get from this node to the starting point (root
+     * for absolute LeafRefPaths).
      *
-     * @return list of <code>qname</code> instances which represents path from
-     *         the schema node towards the root.
+     * @return list of {@code qname} instances which represents path from the schema node towards the root.
      */
     public Iterable<QNameWithPredicate> getPathTowardsRoot() {
-        return new Iterable<QNameWithPredicate>() {
+        return () -> new Iterator<QNameWithPredicate>() {
+            private LeafRefPath current = LeafRefPath.this;
+
             @Override
-            public Iterator<QNameWithPredicate> iterator() {
-                return new Iterator<QNameWithPredicate>() {
-                    private LeafRefPath current = LeafRefPath.this;
-
-                    @Override
-                    public boolean hasNext() {
-                        return current.parent != null;
-                    }
-
-                    @Override
-                    public QNameWithPredicate next() {
-                        if (current.parent != null) {
-                            final QNameWithPredicate 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 QNameWithPredicate next() {
+                if (current.parent == null) {
+                    throw new NoSuchElementException("No more elements available");
+                }
+
+                final QNameWithPredicate ret = current.qname;
+                current = current.parent;
+                return ret;
             }
         };
     }
@@ -306,8 +250,7 @@ public abstract class LeafRefPath implements Immutable {
     /**
      * Describes whether schema path is|isn't absolute.
      *
-     * @return boolean value which is <code>true</code> if schema path is
-     *         absolute.
+     * @return boolean value which is {@code true} if schema path is  absolute.
      */
     public abstract boolean isAbsolute();
 
@@ -321,54 +264,23 @@ public abstract class LeafRefPath implements Immutable {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (obj == null || getClass() != obj.getClass()) {
             return false;
         }
         final LeafRefPath other = (LeafRefPath) 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 String toString() {
         StringBuilder sb = new StringBuilder();
 
-        Iterable<QNameWithPredicate> pathFromRoot = this.getPathFromRoot();
-
         sb.append(isAbsolute() ? "Absolute path:" : "Relative path:");
 
-        for (QNameWithPredicate qName : pathFromRoot) {
-            sb.append("/" + qName);
+        for (QNameWithPredicate qnameWithPredicate : getPathFromRoot()) {
+            sb.append('/').append(qnameWithPredicate);
         }
 
         return sb.toString();
-
     }
-
-    // @Override
-    // public final String toString() {
-    // return addToStringAttributes(Objects.toStringHelper(this)).toString();
-    // }
-    //
-    // protected ToStringHelper addToStringAttributes(final ToStringHelper
-    // toStringHelper) {
-    // return toStringHelper.add("path", getPathFromRoot());
-    // }
-
 }