• 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.google.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       UNSPECIFIED
37     }
38 
PhoneNumber()39     public PhoneNumber() {
40       countryCodeSource_ = CountryCodeSource.UNSPECIFIED;
41     }
42 
43     // required int32 country_code = 1;
44     private boolean hasCountryCode;
45     private int countryCode_ = 0;
hasCountryCode()46     public boolean hasCountryCode() { return hasCountryCode; }
getCountryCode()47     public int getCountryCode() { return countryCode_; }
setCountryCode(int value)48     public PhoneNumber setCountryCode(int value) {
49       hasCountryCode = true;
50       countryCode_ = value;
51       return this;
52     }
clearCountryCode()53     public PhoneNumber clearCountryCode() {
54       hasCountryCode = false;
55       countryCode_ = 0;
56       return this;
57     }
58 
59     // required uint64 national_number = 2;
60     private boolean hasNationalNumber;
61     private long nationalNumber_ = 0L;
hasNationalNumber()62     public boolean hasNationalNumber() { return hasNationalNumber; }
getNationalNumber()63     public long getNationalNumber() { return nationalNumber_; }
setNationalNumber(long value)64     public PhoneNumber setNationalNumber(long value) {
65       hasNationalNumber = true;
66       nationalNumber_ = value;
67       return this;
68     }
clearNationalNumber()69     public PhoneNumber clearNationalNumber() {
70       hasNationalNumber = false;
71       nationalNumber_ = 0L;
72       return this;
73     }
74 
75     // optional string extension = 3;
76     private boolean hasExtension;
77     private java.lang.String extension_ = "";
hasExtension()78     public boolean hasExtension() { return hasExtension; }
getExtension()79     public String getExtension() { return extension_; }
setExtension(String value)80     public PhoneNumber setExtension(String value) {
81       if (value == null) {
82         throw new NullPointerException();
83       }
84       hasExtension = true;
85       extension_ = value;
86       return this;
87     }
clearExtension()88     public PhoneNumber clearExtension() {
89       hasExtension = false;
90       extension_ = "";
91       return this;
92     }
93 
94     // optional bool italian_leading_zero = 4;
95     private boolean hasItalianLeadingZero;
96     private boolean italianLeadingZero_ = false;
hasItalianLeadingZero()97     public boolean hasItalianLeadingZero() { return hasItalianLeadingZero; }
isItalianLeadingZero()98     public boolean isItalianLeadingZero() { return italianLeadingZero_; }
setItalianLeadingZero(boolean value)99     public PhoneNumber setItalianLeadingZero(boolean value) {
100       hasItalianLeadingZero = true;
101       italianLeadingZero_ = value;
102       return this;
103     }
clearItalianLeadingZero()104     public PhoneNumber clearItalianLeadingZero() {
105       hasItalianLeadingZero = false;
106       italianLeadingZero_ = false;
107       return this;
108     }
109 
110     // optional int32 number_of_leading_zeros = 8 [default = 1];
111     private boolean hasNumberOfLeadingZeros;
112     private int numberOfLeadingZeros_ = 1;
hasNumberOfLeadingZeros()113     public boolean hasNumberOfLeadingZeros() { return hasNumberOfLeadingZeros; }
getNumberOfLeadingZeros()114     public int getNumberOfLeadingZeros() { return numberOfLeadingZeros_; }
setNumberOfLeadingZeros(int value)115     public PhoneNumber setNumberOfLeadingZeros(int value) {
116       hasNumberOfLeadingZeros = true;
117       numberOfLeadingZeros_ = value;
118       return this;
119     }
clearNumberOfLeadingZeros()120     public PhoneNumber clearNumberOfLeadingZeros() {
121       hasNumberOfLeadingZeros = false;
122       numberOfLeadingZeros_ = 1;
123       return this;
124     }
125 
126     // optional string raw_input = 5;
127     private boolean hasRawInput;
128     private String rawInput_ = "";
hasRawInput()129     public boolean hasRawInput() { return hasRawInput; }
getRawInput()130     public String getRawInput() { return rawInput_; }
setRawInput(String value)131     public PhoneNumber setRawInput(String value) {
132       if (value == null) {
133         throw new NullPointerException();
134       }
135       hasRawInput = true;
136       rawInput_ = value;
137       return this;
138     }
clearRawInput()139     public PhoneNumber clearRawInput() {
140       hasRawInput = false;
141       rawInput_ = "";
142       return this;
143     }
144 
145     // optional CountryCodeSource country_code_source = 6;
146     private boolean hasCountryCodeSource;
147     private CountryCodeSource countryCodeSource_;
hasCountryCodeSource()148     public boolean hasCountryCodeSource() { return hasCountryCodeSource; }
getCountryCodeSource()149     public CountryCodeSource getCountryCodeSource() { return countryCodeSource_; }
setCountryCodeSource(CountryCodeSource value)150     public PhoneNumber setCountryCodeSource(CountryCodeSource value) {
151       if (value == null) {
152         throw new NullPointerException();
153       }
154       hasCountryCodeSource = true;
155       countryCodeSource_ = value;
156       return this;
157     }
clearCountryCodeSource()158     public PhoneNumber clearCountryCodeSource() {
159       hasCountryCodeSource = false;
160       countryCodeSource_ = CountryCodeSource.UNSPECIFIED;
161       return this;
162     }
163 
164     // optional string preferred_domestic_carrier_code = 7;
165     private boolean hasPreferredDomesticCarrierCode;
166     private java.lang.String preferredDomesticCarrierCode_ = "";
hasPreferredDomesticCarrierCode()167     public boolean hasPreferredDomesticCarrierCode() { return hasPreferredDomesticCarrierCode; }
getPreferredDomesticCarrierCode()168     public String getPreferredDomesticCarrierCode() { return preferredDomesticCarrierCode_; }
setPreferredDomesticCarrierCode(String value)169     public PhoneNumber setPreferredDomesticCarrierCode(String value) {
170       if (value == null) {
171         throw new NullPointerException();
172       }
173       hasPreferredDomesticCarrierCode = true;
174       preferredDomesticCarrierCode_ = value;
175       return this;
176     }
clearPreferredDomesticCarrierCode()177     public PhoneNumber clearPreferredDomesticCarrierCode() {
178       hasPreferredDomesticCarrierCode = false;
179       preferredDomesticCarrierCode_ = "";
180       return this;
181     }
182 
clear()183     public final PhoneNumber clear() {
184       clearCountryCode();
185       clearNationalNumber();
186       clearExtension();
187       clearItalianLeadingZero();
188       clearNumberOfLeadingZeros();
189       clearRawInput();
190       clearCountryCodeSource();
191       clearPreferredDomesticCarrierCode();
192       return this;
193     }
194 
mergeFrom(PhoneNumber other)195     public PhoneNumber mergeFrom(PhoneNumber other) {
196       if (other.hasCountryCode()) {
197         setCountryCode(other.getCountryCode());
198       }
199       if (other.hasNationalNumber()) {
200         setNationalNumber(other.getNationalNumber());
201       }
202       if (other.hasExtension()) {
203         setExtension(other.getExtension());
204       }
205       if (other.hasItalianLeadingZero()) {
206         setItalianLeadingZero(other.isItalianLeadingZero());
207       }
208       if (other.hasNumberOfLeadingZeros()) {
209         setNumberOfLeadingZeros(other.getNumberOfLeadingZeros());
210       }
211       if (other.hasRawInput()) {
212         setRawInput(other.getRawInput());
213       }
214       if (other.hasCountryCodeSource()) {
215         setCountryCodeSource(other.getCountryCodeSource());
216       }
217       if (other.hasPreferredDomesticCarrierCode()) {
218         setPreferredDomesticCarrierCode(other.getPreferredDomesticCarrierCode());
219       }
220       return this;
221     }
222 
exactlySameAs(PhoneNumber other)223     public boolean exactlySameAs(PhoneNumber other) {
224       if (other == null) {
225         return false;
226       }
227       if (this == other) {
228         return true;
229       }
230       return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ &&
231           extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ &&
232           numberOfLeadingZeros_ == other.numberOfLeadingZeros_ &&
233           rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_ &&
234           preferredDomesticCarrierCode_.equals(other.preferredDomesticCarrierCode_) &&
235           hasPreferredDomesticCarrierCode() == other.hasPreferredDomesticCarrierCode());
236     }
237 
238     @Override
equals(Object that)239     public boolean equals(Object that) {
240       return (that instanceof PhoneNumber) && exactlySameAs((PhoneNumber) that);
241     }
242 
243     @Override
hashCode()244     public int hashCode() {
245       // Simplified rendition of the hashCode function automatically generated from the proto
246       // compiler with java_generate_equals_and_hash set to true. We are happy with unset values to
247       // be considered equal to their explicitly-set equivalents, so don't check if any value is
248       // unknown. The only exception to this is the preferred domestic carrier code.
249       int hash = 41;
250       hash = (53 * hash) + getCountryCode();
251       hash = (53 * hash) + Long.valueOf(getNationalNumber()).hashCode();
252       hash = (53 * hash) + getExtension().hashCode();
253       hash = (53 * hash) + (isItalianLeadingZero() ? 1231 : 1237);
254       hash = (53 * hash) + getNumberOfLeadingZeros();
255       hash = (53 * hash) + getRawInput().hashCode();
256       hash = (53 * hash) + getCountryCodeSource().hashCode();
257       hash = (53 * hash) + getPreferredDomesticCarrierCode().hashCode();
258       hash = (53 * hash) + (hasPreferredDomesticCarrierCode() ? 1231 : 1237);
259       return hash;
260     }
261 
262     @Override
toString()263     public String toString() {
264       StringBuilder outputString = new StringBuilder();
265       outputString.append("Country Code: ").append(countryCode_);
266       outputString.append(" National Number: ").append(nationalNumber_);
267       if (hasItalianLeadingZero() && isItalianLeadingZero()) {
268         outputString.append(" Leading Zero(s): true");
269       }
270       if (hasNumberOfLeadingZeros()) {
271         outputString.append(" Number of leading zeros: ").append(numberOfLeadingZeros_);
272       }
273       if (hasExtension()) {
274         outputString.append(" Extension: ").append(extension_);
275       }
276       if (hasCountryCodeSource()) {
277         outputString.append(" Country Code Source: ").append(countryCodeSource_);
278       }
279       if (hasPreferredDomesticCarrierCode()) {
280         outputString.append(" Preferred Domestic Carrier Code: ").
281             append(preferredDomesticCarrierCode_);
282       }
283       return outputString.toString();
284     }
285   }
286 }
287