Browse Source

Overriding hashCode and equals for Phonenumber.PhoneNumber.

pull/567/head
Lara Scheidegger 15 years ago
committed by Mihaela Rosca
parent
commit
5a31de401d
4 changed files with 117 additions and 14 deletions
  1. +3
    -0
      java/release_notes.txt
  2. +27
    -0
      java/src/com/google/i18n/phonenumbers/Phonenumber.java
  3. +1
    -14
      java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  4. +86
    -0
      java/test/com/google/i18n/phonenumbers/PhonenumberTest.java

+ 3
- 0
java/release_notes.txt View File

@ -1,3 +1,6 @@
Jan 31st, 2011
- Introducing equals() and hashCode() methods for the Phonenumber.PhoneNumber class
Jan 28th, 2011 Jan 28th, 2011
* Code changes: * Code changes:
- Fixing critical bug with non-Android-compatible code. isEmpty() was being used and CANON_EQ in - Fixing critical bug with non-Android-compatible code. isEmpty() was being used and CANON_EQ in


+ 27
- 0
java/src/com/google/i18n/phonenumbers/Phonenumber.java View File

@ -174,11 +174,38 @@ public final class Phonenumber {
} }
public boolean exactlySameAs(PhoneNumber other) { public boolean exactlySameAs(PhoneNumber other) {
if (other == null) {
return false;
}
if (this == other) {
return true;
}
return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ && return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ &&
extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ && extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ &&
rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_); rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_);
} }
@Override
public boolean equals(Object that) {
return (that instanceof PhoneNumber) && exactlySameAs((PhoneNumber) that);
}
@Override
public int hashCode() {
// Simplified rendition of the hashCode function automatically generated from the proto
// compiler with java_generate_equals_and_hash set to true. We are happy with unset values to
// be considered equal to their explicitly-set equivalents, so don't check if any value is
// unknown.
int hash = 41;
hash = (53 * hash) + getCountryCode();
hash = (53 * hash) + Long.valueOf(getNationalNumber()).hashCode();
hash = (53 * hash) + getExtension().hashCode();
hash = (53 * hash) + (getItalianLeadingZero() ? 1231 : 1237);
hash = (53 * hash) + getRawInput().hashCode();
hash = (53 * hash) + getCountryCodeSource().hashCode();
return hash;
}
@Override @Override
public String toString() { public String toString() {
StringBuffer outputString = new StringBuffer(); StringBuffer outputString = new StringBuffer();


+ 1
- 14
java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -65,18 +65,6 @@ public class PhoneNumberUtilTest extends TestCase {
super.tearDown(); super.tearDown();
} }
private void assertEquals(PhoneNumber number1, PhoneNumber number2) {
boolean equal = false;
if (number1 == null && number2 == null) {
equal = true;
} else if (number1 != null && number2 != null) {
equal = number1.exactlySameAs(number2);
}
if (!equal) {
fail("The phone numbers do not match");
}
}
public void testGetInstanceLoadUSMetadata() { public void testGetInstanceLoadUSMetadata() {
PhoneMetadata metadata = phoneUtil.getMetadataForRegion("US"); PhoneMetadata metadata = phoneUtil.getMetadataForRegion("US");
assertEquals("US", metadata.getId()); assertEquals("US", metadata.getId());
@ -833,7 +821,6 @@ public class PhoneNumberUtilTest extends TestCase {
assertEquals(0, phoneUtil.getCountryCodeForRegion("CS")); assertEquals(0, phoneUtil.getCountryCodeForRegion("CS"));
} }
@SuppressWarnings("deprecation")
public void testGetNationalDiallingPrefixForRegion() { public void testGetNationalDiallingPrefixForRegion() {
assertEquals("1", phoneUtil.getNddPrefixForRegion("US", false)); assertEquals("1", phoneUtil.getNddPrefixForRegion("US", false));
// Test non-main country to see it gets the national dialling prefix for the main country with // Test non-main country to see it gets the national dialling prefix for the main country with
@ -1614,7 +1601,7 @@ public class PhoneNumberUtilTest extends TestCase {
phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", "US")); phoneUtil.parse("(800) 901-3355 ,extensi\u00F3n 7246433", "US"));
// Repeat with the small letter o with acute accent created by combining characters. // Repeat with the small letter o with acute accent created by combining characters.
assertEquals(usWithExtension, assertEquals(usWithExtension,
phoneUtil.parse("(800) 901-3355 ,extension 7246433", "US"));
phoneUtil.parse("(800) 901-3355 ,extensio\u0301n 7246433", "US"));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", "US")); assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 , 7246433", "US"));
assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", "US")); assertEquals(usWithExtension, phoneUtil.parse("(800) 901-3355 ext: 7246433", "US"));


+ 86
- 0
java/test/com/google/i18n/phonenumbers/PhonenumberTest.java View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.i18n.phonenumbers;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
import junit.framework.TestCase;
/**
* Tests for the Phonenumber.PhoneNumber object itself.
*
* @author Lara Rennie
*/
public class PhonenumberTest extends TestCase {
public void testEqualsSimpleNumber() throws Exception {
PhoneNumber numberA = new PhoneNumber();
numberA.setCountryCode(1).setNationalNumber(6502530000L);
PhoneNumber numberB = new PhoneNumber();
numberB.setCountryCode(1).setNationalNumber(6502530000L);
assertEquals(numberA, numberB);
assertEquals(numberA.hashCode(), numberB.hashCode());
}
public void testEqualsWithOtherFields() throws Exception {
PhoneNumber numberA = new PhoneNumber();
numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(false);
PhoneNumber numberB = new PhoneNumber();
numberB.setCountryCode(1).setNationalNumber(6502530000L);
// These should still be equal, since the default value for this field is false.
assertEquals(numberA, numberB);
assertEquals(numberA.hashCode(), numberB.hashCode());
numberA.setRawInput("+1 650 253 00 00").
setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
numberB.setRawInput("+1 650 253 00 00").
setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
assertEquals(numberA, numberB);
assertEquals(numberA.hashCode(), numberB.hashCode());
}
public void testNonEqualWithOtherFields() throws Exception {
PhoneNumber numberA = new PhoneNumber();
numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(true);
PhoneNumber numberB = new PhoneNumber();
numberB.setCountryCode(1).setNationalNumber(6502530000L);
assertFalse(numberA.equals(numberB));
assertFalse(numberA.hashCode() == numberB.hashCode());
}
public void testNonEqualWithAllFields() throws Exception {
PhoneNumber numberA = new PhoneNumber();
numberA.setCountryCode(1).setNationalNumber(6502530000L).setRawInput("+1 650 253 00 00").
setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
PhoneNumber numberB = new PhoneNumber();
// Although these numbers would pass an isNumberMatch test, they are not considered "equal" as
// objects, since their raw input is different.
numberB.setCountryCode(1).setNationalNumber(6502530000L).setRawInput("+1-650-253-00-00").
setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN);
assertFalse(numberA.equals(numberB));
assertFalse(numberA.hashCode() == numberB.hashCode());
}
}

Loading…
Cancel
Save