You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2009/10/01 13:48:50 UTC

svn commit: r820617 - /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java

Author: tellison
Date: Thu Oct  1 11:48:50 2009
New Revision: 820617

URL: http://svn.apache.org/viewvc?rev=820617&view=rev
Log:
Fix NetworkInterface#equals to avoid redundant null check.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java?rev=820617&r1=820616&r2=820617&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/NetworkInterface.java Thu Oct  1 11:48:50 2009
@@ -334,67 +334,66 @@
      *            the object to compare with this instance.
      * @return {@code true} if the specified object is equal to this {@code
      *         NetworkInterface}, {@code false} otherwise.
-     * @see #hashCode
+     * @see #hashCode()
      */
     @Override
     public boolean equals(Object obj) {
-        // just return true if it is the exact same object
+        // Return true if it is the exact same object.
         if (obj == this) {
             return true;
         }
 
-        if (obj instanceof NetworkInterface) {
-            /*
-             * make sure that some simple checks pass. If the name is not the
-             * same then we are sure it is not the same one. We don't check the
-             * hashcode as it is generated from the name which we check
-             */
-            NetworkInterface netif = (NetworkInterface) obj;
+        // Ensure it is the right type.
+        if (!(obj instanceof NetworkInterface)) {
+            return false;
+        }
 
-            if (netif.getIndex() != interfaceIndex) {
-                return false;
-            }
+        /*
+         * Make sure that some simple checks pass. If the name is not the same
+         * then we are sure it is not the same one. We don't check the hashcode
+         * as it is generated from the name which we check
+         */
+        NetworkInterface netif = (NetworkInterface) obj;
 
-            if (!(name.equals("")) && (!netif.getName().equals(name))) { //$NON-NLS-1$
-                return false;
-            }
+        if (netif.getIndex() != interfaceIndex) {
+            return false;
+        }
 
-            if ((name.equals("")) && (!netif.getName().equals(displayName))) { //$NON-NLS-1$
-                return false;
-            }
+        if (!(name.equals("")) && (!netif.getName().equals(name))) { //$NON-NLS-1$
+            return false;
+        }
 
-            // now check that the internet addresses are the same
-            Enumeration<InetAddress> netifAddresses = netif.getInetAddresses();
-            Enumeration<InetAddress> localifAddresses = getInetAddresses();
-            if ((netifAddresses == null) && (localifAddresses != null)) {
-                return false;
-            }
+        if ((name.equals("")) && (!netif.getName().equals(displayName))) { //$NON-NLS-1$
+            return false;
+        }
 
-            if ((netifAddresses == null) && (localifAddresses == null)) {
-                // neither have any addresses so they are the same
-                return true;
-            }
+        // Now check that the collection of internet addresses are equal.
+        Enumeration<InetAddress> netifAddresses = netif.getInetAddresses();
+        Enumeration<InetAddress> localifAddresses = getInetAddresses();
+
+        // Check for both null (same), or one null (not same).
+        if (netifAddresses == null) {
+            return localifAddresses == null;
+        }
+        if (localifAddresses == null) {
+            return false;
+        }
 
-            if (netifAddresses != null) {
-                while (netifAddresses.hasMoreElements()
-                        && localifAddresses.hasMoreElements()) {
-                    if (!(localifAddresses.nextElement()).equals(netifAddresses
-                            .nextElement())) {
-                        return false;
-                    }
-                }
-                /*
-                 * now make sure that they had the same number of internet
-                 * addresses, if not they are not the same interface
-                 */
-                if (netifAddresses.hasMoreElements()
-                        || localifAddresses.hasMoreElements()) {
-                    return false;
-                }
+        // Both are not null, check InetAddress elements.
+        while (netifAddresses.hasMoreElements()
+                && localifAddresses.hasMoreElements()) {
+            if (!(localifAddresses.nextElement()).equals(
+                    netifAddresses.nextElement())) {
+                return false;
             }
-            return true;
         }
-        return false;
+
+        /*
+         * Now make sure that they had the same number of addresses, if not they
+         * are not the same interface.
+         */
+        return !netifAddresses.hasMoreElements()
+                && !localifAddresses.hasMoreElements();
     }
 
     /**