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 23package com.android.i18n.phonenumbers; 24 25import java.io.Serializable; 26 27public final class 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 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; 45 public boolean hasCountryCode() { return hasCountryCode; } 46 public int getCountryCode() { return countryCode_; } 47 public PhoneNumber setCountryCode(int value) { 48 hasCountryCode = true; 49 countryCode_ = value; 50 return this; 51 } 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; 61 public boolean hasNationalNumber() { return hasNationalNumber; } 62 public long getNationalNumber() { return nationalNumber_; } 63 public PhoneNumber setNationalNumber(long value) { 64 hasNationalNumber = true; 65 nationalNumber_ = value; 66 return this; 67 } 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_ = ""; 77 public boolean hasExtension() { return hasExtension; } 78 public String getExtension() { return extension_; } 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 } 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; 96 public boolean hasItalianLeadingZero() { return hasItalianLeadingZero; } 97 public boolean isItalianLeadingZero() { return italianLeadingZero_; } 98 public PhoneNumber setItalianLeadingZero(boolean value) { 99 hasItalianLeadingZero = true; 100 italianLeadingZero_ = value; 101 return this; 102 } 103 public PhoneNumber clearItalianLeadingZero() { 104 hasItalianLeadingZero = false; 105 italianLeadingZero_ = false; 106 return this; 107 } 108 109 // optional string raw_input = 5; 110 private boolean hasRawInput; 111 private String rawInput_ = ""; 112 public boolean hasRawInput() { return hasRawInput; } 113 public String getRawInput() { return rawInput_; } 114 public PhoneNumber setRawInput(String value) { 115 if (value == null) { 116 throw new NullPointerException(); 117 } 118 hasRawInput = true; 119 rawInput_ = value; 120 return this; 121 } 122 public PhoneNumber clearRawInput() { 123 hasRawInput = false; 124 rawInput_ = ""; 125 return this; 126 } 127 128 // optional CountryCodeSource country_code_source = 6; 129 private boolean hasCountryCodeSource; 130 private CountryCodeSource countryCodeSource_; 131 public boolean hasCountryCodeSource() { return hasCountryCodeSource; } 132 public CountryCodeSource getCountryCodeSource() { return countryCodeSource_; } 133 public PhoneNumber setCountryCodeSource(CountryCodeSource value) { 134 if (value == null) { 135 throw new NullPointerException(); 136 } 137 hasCountryCodeSource = true; 138 countryCodeSource_ = value; 139 return this; 140 } 141 public PhoneNumber clearCountryCodeSource() { 142 hasCountryCodeSource = false; 143 countryCodeSource_ = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN; 144 return this; 145 } 146 147 // optional string preferred_domestic_carrier_code = 7; 148 private boolean hasPreferredDomesticCarrierCode; 149 private java.lang.String preferredDomesticCarrierCode_ = ""; 150 public boolean hasPreferredDomesticCarrierCode() { return hasPreferredDomesticCarrierCode; } 151 public String getPreferredDomesticCarrierCode() { return preferredDomesticCarrierCode_; } 152 public PhoneNumber setPreferredDomesticCarrierCode(String value) { 153 if (value == null) { 154 throw new NullPointerException(); 155 } 156 hasPreferredDomesticCarrierCode = true; 157 preferredDomesticCarrierCode_ = value; 158 return this; 159 } 160 public PhoneNumber clearPreferredDomesticCarrierCode() { 161 hasPreferredDomesticCarrierCode = false; 162 preferredDomesticCarrierCode_ = ""; 163 return this; 164 } 165 166 public final PhoneNumber clear() { 167 clearCountryCode(); 168 clearNationalNumber(); 169 clearExtension(); 170 clearItalianLeadingZero(); 171 clearRawInput(); 172 clearCountryCodeSource(); 173 clearPreferredDomesticCarrierCode(); 174 return this; 175 } 176 177 public PhoneNumber mergeFrom(PhoneNumber other) { 178 if (other.hasCountryCode()) { 179 setCountryCode(other.getCountryCode()); 180 } 181 if (other.hasNationalNumber()) { 182 setNationalNumber(other.getNationalNumber()); 183 } 184 if (other.hasExtension()) { 185 setExtension(other.getExtension()); 186 } 187 if (other.hasItalianLeadingZero()) { 188 setItalianLeadingZero(other.isItalianLeadingZero()); 189 } 190 if (other.hasRawInput()) { 191 setRawInput(other.getRawInput()); 192 } 193 if (other.hasCountryCodeSource()) { 194 setCountryCodeSource(other.getCountryCodeSource()); 195 } 196 if (other.hasPreferredDomesticCarrierCode()) { 197 setPreferredDomesticCarrierCode(other.getPreferredDomesticCarrierCode()); 198 } 199 return this; 200 } 201 202 public boolean exactlySameAs(PhoneNumber other) { 203 if (other == null) { 204 return false; 205 } 206 if (this == other) { 207 return true; 208 } 209 return (countryCode_ == other.countryCode_ && nationalNumber_ == other.nationalNumber_ && 210 extension_.equals(other.extension_) && italianLeadingZero_ == other.italianLeadingZero_ && 211 rawInput_.equals(other.rawInput_) && countryCodeSource_ == other.countryCodeSource_ && 212 preferredDomesticCarrierCode_.equals(other.preferredDomesticCarrierCode_) && 213 hasPreferredDomesticCarrierCode() == other.hasPreferredDomesticCarrierCode()); 214 } 215 216 @Override 217 public boolean equals(Object that) { 218 return (that instanceof PhoneNumber) && exactlySameAs((PhoneNumber) that); 219 } 220 221 @Override 222 public int hashCode() { 223 // Simplified rendition of the hashCode function automatically generated from the proto 224 // compiler with java_generate_equals_and_hash set to true. We are happy with unset values to 225 // be considered equal to their explicitly-set equivalents, so don't check if any value is 226 // unknown. The only exception to this is the preferred domestic carrier code. 227 int hash = 41; 228 hash = (53 * hash) + getCountryCode(); 229 hash = (53 * hash) + Long.valueOf(getNationalNumber()).hashCode(); 230 hash = (53 * hash) + getExtension().hashCode(); 231 hash = (53 * hash) + (isItalianLeadingZero() ? 1231 : 1237); 232 hash = (53 * hash) + getRawInput().hashCode(); 233 hash = (53 * hash) + getCountryCodeSource().hashCode(); 234 hash = (53 * hash) + getPreferredDomesticCarrierCode().hashCode(); 235 hash = (53 * hash) + (hasPreferredDomesticCarrierCode() ? 1231 : 1237); 236 return hash; 237 } 238 239 @Override 240 public String toString() { 241 StringBuilder outputString = new StringBuilder(); 242 outputString.append("Country Code: ").append(countryCode_); 243 outputString.append(" National Number: ").append(nationalNumber_); 244 if (hasItalianLeadingZero() && isItalianLeadingZero()) { 245 outputString.append(" Leading Zero: true"); 246 } 247 if (hasExtension()) { 248 outputString.append(" Extension: ").append(extension_); 249 } 250 if (hasCountryCodeSource()) { 251 outputString.append(" Country Code Source: ").append(countryCodeSource_); 252 } 253 if (hasPreferredDomesticCarrierCode()) { 254 outputString.append(" Preferred Domestic Carrier Code: "). 255 append(preferredDomesticCarrierCode_); 256 } 257 return outputString.toString(); 258 } 259 } 260} 261