hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1044525 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn: routing/HttpRoute.java routing/RouteTracker.java scheme/Scheme.java
Date Fri, 10 Dec 2010 22:41:08 GMT
Author: sebb
Date: Fri Dec 10 22:41:07 2010
New Revision: 1044525

URL: http://svn.apache.org/viewvc?rev=1044525&view=rev
Log:
Improve equals performance: no need to check for null; perform cheapest checks first.
Use short-cut evaluation

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/scheme/Scheme.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java?rev=1044525&r1=1044524&r2=1044525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java
Fri Dec 10 22:41:07 2010
@@ -324,22 +324,23 @@ public final class HttpRoute implements 
     /**
      * Compares this route to another.
      *
-     * @param o         the object to compare with
+     * @param obj         the object to compare with
      *
      * @return  <code>true</code> if the argument is the same route,
      *          <code>false</code>
      */
     @Override
     public final boolean equals(Object obj) {
-        if (obj == null) return false;
         if (this == obj) return true;
         if (obj instanceof HttpRoute) {
             HttpRoute that = (HttpRoute) obj;
-            return LangUtils.equals(this.targetHost, that.targetHost) &&
-                LangUtils.equals(this.localAddress, that.localAddress) &&
+            return 
+                // Do the cheapest tests first
                 (this.secure    == that.secure) &&
                 (this.tunnelled == that.tunnelled) &&
                 (this.layered   == that.layered) &&
+                LangUtils.equals(this.targetHost, that.targetHost) &&
+                LangUtils.equals(this.localAddress, that.localAddress) &&
                 LangUtils.equals(this.proxyChain, that.proxyChain);
         } else {
             return false;

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java?rev=1044525&r1=1044524&r2=1044525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/routing/RouteTracker.java
Fri Dec 10 22:41:07 2010
@@ -30,6 +30,7 @@ package org.apache.http.conn.routing;
 import java.net.InetAddress;
 
 import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.util.LangUtils;
 
 import org.apache.http.HttpHost;
 
@@ -293,30 +294,15 @@ public final class RouteTracker implemen
             return false;
 
         RouteTracker that = (RouteTracker) o;
-        boolean equal = this.targetHost.equals(that.targetHost);
-        equal &=
-            ( this.localAddress == that.localAddress) ||
-            ((this.localAddress != null) &&
-              this.localAddress.equals(that.localAddress));
-        equal &=
-            ( this.proxyChain        == that.proxyChain) ||
-            ((this.proxyChain        != null) &&
-             (that.proxyChain        != null) &&
-             (this.proxyChain.length == that.proxyChain.length));
-        // comparison of actual proxies follows below
-        equal &=
+        return 
+            // Do the cheapest checks first
             (this.connected == that.connected) &&
             (this.secure    == that.secure) &&
             (this.tunnelled == that.tunnelled) &&
-            (this.layered   == that.layered);
-
-        // chain length has been compared above, now check the proxies
-        if (equal && (this.proxyChain != null)) {
-            for (int i=0; equal && (i<this.proxyChain.length); i++)
-                equal = this.proxyChain[i].equals(that.proxyChain[i]);
-        }
-
-        return equal;
+            (this.layered   == that.layered) &&
+            LangUtils.equals(this.targetHost, that.targetHost) &&
+            LangUtils.equals(this.localAddress, that.localAddress) &&
+            LangUtils.equals(this.proxyChain, that.proxyChain);
     }
 
     /**

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/scheme/Scheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/scheme/Scheme.java?rev=1044525&r1=1044524&r2=1044525&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/scheme/Scheme.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/scheme/Scheme.java
Fri Dec 10 22:41:07 2010
@@ -237,7 +237,6 @@ public final class Scheme {
 
     @Override
     public final boolean equals(Object obj) {
-        if (obj == null) return false;
         if (this == obj) return true;
         if (obj instanceof Scheme) {
             Scheme that = (Scheme) obj;



Mime
View raw message