diff --git a/java/release_notes.txt b/java/release_notes.txt index 6f6074c01..89d3eaf44 100644 --- a/java/release_notes.txt +++ b/java/release_notes.txt @@ -1,3 +1,6 @@ +Jan 31st, 2011 + - Introducing equals() and hashCode() methods for the Phonenumber.PhoneNumber class + Jan 28th, 2011 * Code changes: - Fixing critical bug with non-Android-compatible code. isEmpty() was being used and CANON_EQ in diff --git a/java/src/com/google/i18n/phonenumbers/Phonenumber.java b/java/src/com/google/i18n/phonenumbers/Phonenumber.java index 42a8dcdd9..608a3b40b 100644 --- a/java/src/com/google/i18n/phonenumbers/Phonenumber.java +++ b/java/src/com/google/i18n/phonenumbers/Phonenumber.java @@ -174,11 +174,38 @@ public final class Phonenumber { } public boolean exactlySameAs(PhoneNumber other) { + if (other == null) { + return false; + } + if (this == other) { + return true; + } return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ && extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ && 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 public String toString() { StringBuffer outputString = new StringBuffer(); diff --git a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 723435ea8..2e0f7314c 100644 --- a/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -65,18 +65,6 @@ public class PhoneNumberUtilTest extends TestCase { 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() { PhoneMetadata metadata = phoneUtil.getMetadataForRegion("US"); assertEquals("US", metadata.getId()); @@ -833,7 +821,6 @@ public class PhoneNumberUtilTest extends TestCase { assertEquals(0, phoneUtil.getCountryCodeForRegion("CS")); } - @SuppressWarnings("deprecation") public void testGetNationalDiallingPrefixForRegion() { assertEquals("1", phoneUtil.getNddPrefixForRegion("US", false)); // 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")); // Repeat with the small letter o with acute accent created by combining characters. 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 ext: 7246433", "US")); diff --git a/java/test/com/google/i18n/phonenumbers/PhonenumberTest.java b/java/test/com/google/i18n/phonenumbers/PhonenumberTest.java new file mode 100644 index 000000000..b77b9d1f3 --- /dev/null +++ b/java/test/com/google/i18n/phonenumbers/PhonenumberTest.java @@ -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()); + } +}