Improve equals performance: no need to check for null; perform cheapest checks first.

Use short-cut evaluation

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1044525 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2010-12-10 22:41:07 +00:00
parent 19022393be
commit 74581924cc
3 changed files with 12 additions and 26 deletions

View File

@ -324,22 +324,23 @@ public final class HttpRoute implements RouteInfo, Cloneable {
/**
* 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;

View File

@ -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 implements RouteInfo, Cloneable {
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);
}
/**

View File

@ -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;