diff options
author | Brian Williammee <bwill@google.com> | 2014-08-29 17:31:22 -0700 |
---|---|---|
committer | Brian Williammee <bwill@google.com> | 2014-09-10 17:06:09 -0700 |
commit | 7d1d2e39f382416ffa8db8771c83e4f9e2f53adc (patch) | |
tree | 8105734b682ef1a7aa69ea3b0c3edb670b3ce94e /telephony | |
parent | a98f7d715d12fe1dc4b9d06c6e5cc152f6e3e347 (diff) | |
download | frameworks_base-7d1d2e39f382416ffa8db8771c83e4f9e2f53adc.zip frameworks_base-7d1d2e39f382416ffa8db8771c83e4f9e2f53adc.tar.gz frameworks_base-7d1d2e39f382416ffa8db8771c83e4f9e2f53adc.tar.bz2 |
Improve equals and hashCode in CellIdentityXxx methods
The equals methods were a lot of dead code that boiled down to
seeing if two references pointed to the same object. The hashCode
methods were likely to result in collisions on unequal objects.
Bug: 10328291
Change-Id: I4bd93efd1f4a34e9dec55ac05868df37368abd6a
Diffstat (limited to 'telephony')
4 files changed, 59 insertions, 58 deletions
diff --git a/telephony/java/android/telephony/CellIdentityCdma.java b/telephony/java/android/telephony/CellIdentityCdma.java index dbd48d9..b39b4c7 100644 --- a/telephony/java/android/telephony/CellIdentityCdma.java +++ b/telephony/java/android/telephony/CellIdentityCdma.java @@ -20,6 +20,8 @@ import android.os.Parcel; import android.os.Parcelable; import android.telephony.Rlog; +import java.util.Objects; + /** * CellIdentity is to represent a unique CDMA cell */ @@ -137,27 +139,25 @@ public final class CellIdentityCdma implements Parcelable { @Override public int hashCode() { - int primeNum = 31; - return (mNetworkId * primeNum) + (mSystemId * primeNum) + (mBasestationId * primeNum) + - (mLatitude * primeNum) + (mLongitude * primeNum); + return Objects.hash(mNetworkId, mSystemId, mBasestationId, mLatitude, mLongitude); } @Override public boolean equals(Object other) { - if (super.equals(other)) { - try { - CellIdentityCdma o = (CellIdentityCdma)other; - return mNetworkId == o.mNetworkId && - mSystemId == o.mSystemId && - mBasestationId == o.mBasestationId && - mLatitude == o.mLatitude && - mLongitude == o.mLongitude; - } catch (ClassCastException e) { - return false; - } - } else { + if (this == other) { + return true; + } + + if (!(other instanceof CellIdentityCdma)) { return false; } + + CellIdentityCdma o = (CellIdentityCdma) other; + return mNetworkId == o.mNetworkId && + mSystemId == o.mSystemId && + mBasestationId == o.mBasestationId && + mLatitude == o.mLatitude && + mLongitude == o.mLongitude; } @Override diff --git a/telephony/java/android/telephony/CellIdentityGsm.java b/telephony/java/android/telephony/CellIdentityGsm.java index 6f8cc91..90d2aa0 100644 --- a/telephony/java/android/telephony/CellIdentityGsm.java +++ b/telephony/java/android/telephony/CellIdentityGsm.java @@ -20,6 +20,8 @@ import android.os.Parcel; import android.os.Parcelable; import android.telephony.Rlog; +import java.util.Objects; + /** * CellIdentity to represent a unique GSM cell */ @@ -113,25 +115,24 @@ public final class CellIdentityGsm implements Parcelable { @Override public int hashCode() { - int primeNum = 31; - return (mMcc * primeNum) + (mMnc * primeNum) + (mLac * primeNum) + (mCid * primeNum); + return Objects.hash(mMcc, mMnc, mLac, mCid); } @Override public boolean equals(Object other) { - if (super.equals(other)) { - try { - CellIdentityGsm o = (CellIdentityGsm)other; - return mMcc == o.mMcc && - mMnc == o.mMnc && - mLac == o.mLac && - mCid == o.mCid; - } catch (ClassCastException e) { - return false; - } - } else { + if (this == other) { + return true; + } + + if (!(other instanceof CellIdentityGsm)) { return false; } + + CellIdentityGsm o = (CellIdentityGsm) other; + return mMcc == o.mMcc && + mMnc == o.mMnc && + mLac == o.mLac && + mCid == o.mCid; } @Override diff --git a/telephony/java/android/telephony/CellIdentityLte.java b/telephony/java/android/telephony/CellIdentityLte.java index 72578a4..1e7ac08 100644 --- a/telephony/java/android/telephony/CellIdentityLte.java +++ b/telephony/java/android/telephony/CellIdentityLte.java @@ -20,6 +20,8 @@ import android.os.Parcel; import android.os.Parcelable; import android.telephony.Rlog; +import java.util.Objects; + /** * CellIdentity is to represent a unique LTE cell */ @@ -117,27 +119,25 @@ public final class CellIdentityLte implements Parcelable { @Override public int hashCode() { - int primeNum = 31; - return (mMcc * primeNum) + (mMnc * primeNum) + (mCi * primeNum) + (mPci * primeNum) + - (mTac * primeNum); + return Objects.hash(mMcc, mMnc, mCi, mPci, mTac); } @Override public boolean equals(Object other) { - if (super.equals(other)) { - try { - CellIdentityLte o = (CellIdentityLte)other; - return mMcc == o.mMcc && - mMnc == o.mMnc && - mCi == o.mCi && - mPci == o.mPci && - mTac == o.mTac; - } catch (ClassCastException e) { - return false; - } - } else { + if (this == other) { + return true; + } + + if (!(other instanceof CellIdentityLte)) { return false; } + + CellIdentityLte o = (CellIdentityLte) other; + return mMcc == o.mMcc && + mMnc == o.mMnc && + mCi == o.mCi && + mPci == o.mPci && + mTac == o.mTac; } @Override diff --git a/telephony/java/android/telephony/CellIdentityWcdma.java b/telephony/java/android/telephony/CellIdentityWcdma.java index 2f8fa42..56ee8c9 100644 --- a/telephony/java/android/telephony/CellIdentityWcdma.java +++ b/telephony/java/android/telephony/CellIdentityWcdma.java @@ -20,6 +20,8 @@ import android.os.Parcel; import android.os.Parcelable; import android.telephony.Rlog; +import java.util.Objects; + /** * CellIdentity to represent a unique UMTS cell */ @@ -118,27 +120,25 @@ public final class CellIdentityWcdma implements Parcelable { @Override public int hashCode() { - int primeNum = 31; - return (mMcc * primeNum) + (mMnc * primeNum) + (mLac * primeNum) + (mCid * primeNum) + - (mPsc * primeNum); + return Objects.hash(mMcc, mMnc, mLac, mCid, mPsc); } @Override public boolean equals(Object other) { - if (super.equals(other)) { - try { - CellIdentityWcdma o = (CellIdentityWcdma)other; - return mMcc == o.mMcc && - mMnc == o.mMnc && - mLac == o.mLac && - mCid == o.mCid && - mPsc == o.mPsc; - } catch (ClassCastException e) { - return false; - } - } else { + if (this == other) { + return true; + } + + if (!(other instanceof CellIdentityWcdma)) { return false; } + + CellIdentityWcdma o = (CellIdentityWcdma) other; + return mMcc == o.mMcc && + mMnc == o.mMnc && + mLac == o.mLac && + mCid == o.mCid && + mPsc == o.mPsc; } @Override |