Fix checkstyle issues reported by odlparent-3.0.0's checkstyle
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefPath.java
index 649d911ac3ea88dd79e8fc010bf3da4fb74a7f7e..5d70ef206e066affcf8ba31b3f79282b9bf69dc0 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,15 +7,16 @@
  */
 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 {
 
@@ -34,9 +35,9 @@ 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);
         }
     }
 
@@ -55,15 +56,15 @@ 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,7 +92,7 @@ public abstract class LeafRefPath implements Immutable {
     private final int hash;
 
     /**
-     * Cached legacy path, filled-in when {@link #getPath()} or
+     * Cached legacy path, filled-in when {@link #getPathFromRoot()} or
      * {@link #getPathTowardsRoot()} is invoked.
      */
     private volatile ImmutableList<QNameWithPredicate> legacyPath;
@@ -106,30 +107,16 @@ 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;
     }
 
     /**
@@ -170,14 +157,14 @@ public abstract class LeafRefPath implements Immutable {
     /**
      * Create a new instance.
      *
-     * @param parent
+     * @param newParent
      *            Parent LeafRefPath
-     * @param qname
+     * @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
@@ -192,12 +179,12 @@ 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;
     }
 
     /**
@@ -209,15 +196,14 @@ public abstract class LeafRefPath implements Immutable {
      * @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;
     }
 
     /**
@@ -252,35 +238,28 @@ public abstract class LeafRefPath implements Immutable {
      *         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;
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException("Component removal not supported");
             }
         };
     }
@@ -328,37 +307,20 @@ public abstract class LeafRefPath implements Immutable {
             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