• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Libphonenumber Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * Definition of the class representing international telephone numbers. This class is hand-created
19  * based on the class file compiled from phonenumber.proto. Please refer to that file for detailed
20  * descriptions of the meaning of each field.
21  */
22 
23 package com.android.i18n.phonenumbers;
24 
25 import java.io.Serializable;
26 
27 public final class Phonenumber {
Phonenumber()28   private Phonenumber() {}
29   public static class PhoneNumber implements Serializable {
30     private static final long serialVersionUID = 1L;
31     public enum CountryCodeSource {
32       FROM_NUMBER_WITH_PLUS_SIGN,
33       FROM_NUMBER_WITH_IDD,
34       FROM_NUMBER_WITHOUT_PLUS_SIGN,
35       FROM_DEFAULT_COUNTRY
36     }
37 
PhoneNumber()38     public PhoneNumber() {
39       countryCodeSource_ = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN;
40     }
41 
42     // required int32 country_code = 1;
43     private boolean hasCountryCode;
44     private int countryCode_ = 0;
hasCountryCode()45     public boolean hasCountryCode() { return hasCountryCode; }
getCountryCode()46     public int getCountryCode() { return countryCode_; }
setCountryCode(int value)47     public PhoneNumber setCountryCode(int value) {
48       hasCountryCode = true;
49       countryCode_ = value;
50       return this;
51     }
clearCountryCode()52     public PhoneNumber clearCountryCode() {
53       hasCountryCode = false;
54       countryCode_ = 0;
55       return this;
56     }
57 
58     // required uint64 national_number = 2;
59     private boolean hasNationalNumber;
60     private long nationalNumber_ = 0L;
hasNationalNumber()61     public boolean hasNationalNumber() { return hasNationalNumber; }
getNationalNumber()62     public long getNationalNumber() { return nationalNumber_; }
setNationalNumber(long value)63     public PhoneNumber setNationalNumber(long value) {
64       hasNationalNumber = true;
65       nationalNumber_ = value;
66       return this;
67     }
clearNationalNumber()68     public PhoneNumber clearNationalNumber() {
69       hasNationalNumber = false;
70       nationalNumber_ = 0L;
71       return this;
72     }
73 
74     // optional string extension = 3;
75     private boolean hasExtension;
76     private java.lang.String extension_ = "";
hasExtension()77     public boolean hasExtension() { return hasExtension; }
getExtension()78     public String getExtension() { return extension_; }
setExtension(String value)79     public PhoneNumber setExtension(String value) {
80       if (value == null) {
81         throw new NullPointerException();
82       }
83       hasExtension = true;
84       extension_ = value;
85       return this;
86     }
clearExtension()87     public PhoneNumber clearExtension() {
88       hasExtension = false;
89       extension_ = "";
90       return this;
91     }
92 
93     // optional bool italian_leading_zero = 4;
94     private boolean hasItalianLeadingZero;
95     private boolean italianLeadingZero_ = false;
hasItalianLeadingZero()96     public boolean hasItalianLeadingZero() { return hasItalianLeadingZero; }
isItalianLeadingZero()97     public boolean isItalianLeadingZero() { return italianLeadingZero_; }
setItalianLeadingZero(boolean value)98     public PhoneNumber setItalianLeadingZero(boolean value) {
99       hasItalianLeadingZero = true;
100       italianLeadingZero_ = value;
101       return this;
102     }
clearItalianLeadingZero()103     public PhoneNumber clearItalianLeadingZero() {
104       hasItalianLeadingZero = false;
105       italianLeadingZero_ = false;
106       return this;
107     }
108 
109     // optional int32 number_of_leading_zeros = 8 [default = 1];
110     private boolean hasNumberOfLeadingZeros;
111     private int numberOfLeadingZeros_ = 1;
hasNumberOfLeadingZeros()112     public boolean hasNumberOfLeadingZeros() { return hasNumberOfLeadingZeros; }
getNumberOfLeadingZeros()113     public int getNumberOfLeadingZeros() { return numberOfLeadingZeros_; }
setNumberOfLeadingZeros(int value)114     public PhoneNumber setNumberOfLeadingZeros(int value) {
115       hasNumberOfLeadingZeros = true;
116       numberOfLeadingZeros_ = value;
117       return this;
118     }
clearNumberOfLeadingZeros()119     public PhoneNumber clearNumberOfLeadingZeros() {
120       hasNumberOfLeadingZeros = false;
121       numberOfLeadingZeros_ = 1;
122       return this;
123     }
124 
125     // optional string raw_input = 5;
126     private boolean hasRawInput;
127     private String rawInput_ = "";
hasRawInput()128     public boolean hasRawInput() { return hasRawInput; }
getRawInput()129     public String getRawInput() { return rawInput_; }
setRawInput(String value)130     public PhoneNumber setRawInput(String value) {
131       if (value == null) {
132         throw new NullPointerException();
133       }
134       hasRawInput = true;
135       rawInput_ = value;
136       return this;
137     }
clearRawInput()138     public PhoneNumber clearRawInput() {
139       hasRawInput = false;
140       rawInput_ = "";
141       return this;
142     }
143 
144     // optional CountryCodeSource country_code_source = 6;
145     private boolean hasCountryCodeSource;
146     private CountryCodeSource countryCodeSource_;
hasCountryCodeSource()147     public boolean hasCountryCodeSource() { return hasCountryCodeSource; }
getCountryCodeSource()148     public CountryCodeSource getCountryCodeSource() { return countryCodeSource_; }
setCountryCodeSource(CountryCodeSource value)149     public PhoneNumber setCountryCodeSource(CountryCodeSource value) {
150       if (value == null) {
151         throw new NullPointerException();
152       }
153       hasCountryCodeSource = true;
154       countryCodeSource_ = value;
155       return this;
156     }
clearCountryCodeSource()157     public PhoneNumber clearCountryCodeSource() {
158       hasCountryCodeSource = false;
159       countryCodeSource_ = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN;
160       return this;
161     }
162 
163     // optional string preferred_domestic_carrier_code = 7;
164     private boolean hasPreferredDomesticCarrierCode;
165     private java.lang.String preferredDomesticCarrierCode_ = "";
hasPreferredDomesticCarrierCode()166     public boolean hasPreferredDomesticCarrierCode() { return hasPreferredDomesticCarrierCode; }
getPreferredDomesticCarrierCode()167     public String getPreferredDomesticCarrierCode() { return preferredDomesticCarrierCode_; }
setPreferredDomesticCarrierCode(String value)168     public PhoneNumber setPreferredDomesticCarrierCode(String value) {
169       if (value == null) {
170         throw new NullPointerException();
171       }
172       hasPreferredDomesticCarrierCode = true;
173       preferredDomesticCarrierCode_ = value;
174       return this;
175     }
clearPreferredDomesticCarrierCode()176     public PhoneNumber clearPreferredDomesticCarrierCode() {
177       hasPreferredDomesticCarrierCode = false;
178       preferredDomesticCarrierCode_ = "";
179       return this;
180     }
181 
clear()182     public final PhoneNumber clear() {
183       clearCountryCode();
184       clearNationalNumber();
185       clearExtension();
186       clearItalianLeadingZero();
187       clearNumberOfLeadingZeros();
188       clearRawInput();
189       clearCountryCodeSource();
190       clearPreferredDomesticCarrierCode();
191       return this;
192     }
193 
mergeFrom(PhoneNumber other)194     public PhoneNumber mergeFrom(PhoneNumber other) {
195       if (other.hasCountryCode()) {
196         setCountryCode(other.getCountryCode());
197       }
198       if (other.hasNationalNumber()) {
199         setNationalNumber(other.getNationalNumber());
200       }
201       if (other.hasExtension()) {
202         setExtension(other.getExtension());
203       }
204       if (other.hasItalianLeadingZero()) {
205         setItalianLeadingZero(other.isItalianLeadingZero());
206       }
207       if (other.hasNumberOfLeadingZeros()) {
208         setNumberOfLeadingZeros(other.getNumberOfLeadingZeros());
209       }
210       if (other.hasRawInput()) {
211         setRawInput(other.getRawInput());
212       }
213       if (other.hasCountryCodeSource()) {
214         setCountryCodeSource(other.getCountryCodeSource());
215       }
216       if (other.hasPreferredDomesticCarrierCode()) {
217         setPreferredDomesticCarrierCode(other.getPreferredDomesticCarrierCode());
218       }
219       return this;
220     }
221 
exactlySameAs(PhoneNumber other)222     public boolean exactlySameAs(PhoneNumber other) {
223       if (other == null) {
224         return false;
225       }
226       if (this == other) {
227         return true;
228       }
229       return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ &&
230           extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ &&
231           numberOfLeadingZeros_ == other.numberOfLeadingZeros_ &&
232           rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_ &&
233           preferredDomesticCarrierCode_.equals(other.preferredDomesticCarrierCode_) &&
234           hasPreferredDomesticCarrierCode() == other.hasPreferredDomesticCarrierCode());
235     }
236 
237     @Override
equals(Object that)238     public boolean equals(Object that) {
239       return (that instanceof PhoneNumber) && exactlySameAs((PhoneNumber) that);
240     }
241 
242     @Override
hashCode()243     public int hashCode() {
244       // Simplified rendition of the hashCode function automatically generated from the proto
245       // compiler with java_generate_equals_and_hash set to true. We are happy with unset values to
246       // be considered equal to their explicitly-set equivalents, so don't check if any value is
247       // unknown. The only exception to this is the preferred domestic carrier code.
248       int hash = 41;
249       hash = (53 * hash) + getCountryCode();
250       hash = (53 * hash) + Long.valueOf(getNationalNumber()).hashCode();
251       hash = (53 * hash) + getExtension().hashCode();
252       hash = (53 * hash) + (isItalianLeadingZero() ? 1231 : 1237);
253       hash = (53 * hash) + getNumberOfLeadingZeros();
254       hash = (53 * hash) + getRawInput().hashCode();
255       hash = (53 * hash) + getCountryCodeSource().hashCode();
256       hash = (53 * hash) + getPreferredDomesticCarrierCode().hashCode();
257       hash = (53 * hash) + (hasPreferredDomesticCarrierCode() ? 1231 : 1237);
258       return hash;
259     }
260 
261     @Override
toString()262     public String toString() {
263       StringBuilder outputString = new StringBuilder();
264       outputString.append("Country Code: ").append(countryCode_);
265       outputString.append(" National Number: ").append(nationalNumber_);
266       if (hasItalianLeadingZero() && isItalianLeadingZero()) {
267         outputString.append(" Leading Zero(s): true");
268       }
269       if (hasNumberOfLeadingZeros()) {
270         outputString.append(" Number of leading zeros: ").append(numberOfLeadingZeros_);
271       }
272       if (hasExtension()) {
273         outputString.append(" Extension: ").append(extension_);
274       }
275       if (hasCountryCodeSource()) {
276         outputString.append(" Country Code Source: ").append(countryCodeSource_);
277       }
278       if (hasPreferredDomesticCarrierCode()) {
279         outputString.append(" Preferred Domestic Carrier Code: ").
280             append(preferredDomesticCarrierCode_);
281       }
282       return outputString.toString();
283     }
284   }
285 }
286