From d03fa994224f969a6e4466e2a4dc2904e46f7ef8 Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Fri, 9 Oct 2015 18:30:55 +0200 Subject: [PATCH] replace the generation from proto with generation from nanoproto --- .../i18n/phonenumbers/AsYouTypeFormatter.java | 4 +- .../i18n/phonenumbers/MetadataManager.java | 32 +- .../i18n/phonenumbers/MetadataSource.java | 2 +- .../MultiFileMetadataSourceImpl.java | 17 +- .../i18n/phonenumbers/PhoneNumberMatcher.java | 6 +- .../i18n/phonenumbers/PhoneNumberUtil.java | 33 +- .../i18n/phonenumbers/Phonemetadata.java | 603 ---------- .../i18n/phonenumbers/ShortNumberInfo.java | 6 +- .../SingleFileMetadataSourceImpl.java | 13 +- .../phonenumbers/internal/MatcherApi.java | 2 +- .../internal/RegexBasedMatcher.java | 2 +- .../i18n/phonenumbers/nano/Phonemetadata.java | 1040 +++++++++++++++++ .../i18n/phonenumbers/ExampleNumbersTest.java | 2 +- .../phonenumbers/MetadataManagerTest.java | 2 +- .../phonenumbers/PhoneNumberUtilTest.java | 13 +- .../phonenumbers/BuildMetadataFromXml.java | 28 +- .../BuildMetadataFromXmlTest.java | 6 +- tools/java/cpp-build/pom.xml | 21 +- .../phonenumbers/BuildMetadataCppFromXml.java | 14 +- .../BuildMetadataJsonFromXml.java | 8 +- .../BuildMetadataProtoFromXml.java | 30 +- 21 files changed, 1195 insertions(+), 689 deletions(-) delete mode 100644 java/libphonenumber/src/com/google/i18n/phonenumbers/Phonemetadata.java create mode 100644 java/libphonenumber/src/com/google/i18n/phonenumbers/nano/Phonemetadata.java diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java index ee124e78e..d5c4fe540 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java @@ -16,8 +16,8 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; import java.util.ArrayList; import java.util.Iterator; diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java index 04b1c8940..0d998c751 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataManager.java @@ -16,16 +16,16 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.protobuf.nano.CodedInputByteBufferNano; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; @@ -71,14 +71,33 @@ class MetadataManager { } } + // The size of the byte buffer used for deserializing the alternate formats and short number + // metadata files for each region. + private static final int BUFFER_SIZE = 16 * 1024; + + static CodedInputByteBufferNano convertStreamToByteBuffer(ObjectInputStream in, int bufferSize) + throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[bufferSize]; + + while ((nRead = in.read(data, 0, bufferSize)) != -1) { + outputStream.write(data, 0, nRead); + } + + outputStream.flush(); + return CodedInputByteBufferNano.newInstance(outputStream.toByteArray()); + } + private static void loadAlternateFormatsMetadataFromFile(int countryCallingCode) { InputStream source = PhoneNumberMatcher.class.getResourceAsStream( ALTERNATE_FORMATS_FILE_PREFIX + "_" + countryCallingCode); ObjectInputStream in = null; try { in = new ObjectInputStream(source); + CodedInputByteBufferNano byteBuffer = convertStreamToByteBuffer(in, BUFFER_SIZE); PhoneMetadataCollection alternateFormats = new PhoneMetadataCollection(); - alternateFormats.readExternal(in); + alternateFormats.mergeFrom(byteBuffer); for (PhoneMetadata metadata : alternateFormats.metadata) { callingCodeToAlternateFormatsMap.put(metadata.countryCode, metadata); } @@ -107,8 +126,9 @@ class MetadataManager { ObjectInputStream in = null; try { in = new ObjectInputStream(source); + CodedInputByteBufferNano byteBuffer = convertStreamToByteBuffer(in, BUFFER_SIZE); PhoneMetadataCollection shortNumberMetadata = new PhoneMetadataCollection(); - shortNumberMetadata.readExternal(in); + shortNumberMetadata.mergeFrom(byteBuffer); for (PhoneMetadata metadata : shortNumberMetadata.metadata) { regionCodeToShortNumberMetadataMap.put(regionCode, metadata); } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataSource.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataSource.java index d0ec50dfe..018ff3da9 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataSource.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataSource.java @@ -16,7 +16,7 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; /** * A source for phone metadata for all regions. diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java index 1b9461b8e..e2d721404 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java @@ -16,15 +16,15 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.protobuf.nano.CodedInputByteBufferNano; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -103,9 +103,8 @@ final class MultiFileMetadataSourceImpl implements MetadataSource { logger.log(Level.SEVERE, "missing metadata: " + fileName); throw new IllegalStateException("missing metadata: " + fileName); } - ObjectInputStream in = null; try { - in = new ObjectInputStream(source); + ObjectInputStream in = new ObjectInputStream(source); PhoneMetadataCollection metadataCollection = loadMetadataAndCloseInput(in); PhoneMetadata[] metadataList = metadataCollection.metadata; if (metadataList.length == 0) { @@ -135,9 +134,15 @@ final class MultiFileMetadataSourceImpl implements MetadataSource { * @return the loaded metadata protocol buffer. */ private static PhoneMetadataCollection loadMetadataAndCloseInput(ObjectInputStream source) { + // The size of the byte buffer used for deserializing the phone number metadata files for each + // region. + final int MULTI_FILE_BUFFER_SIZE = 16 * 1024; + PhoneMetadataCollection metadataCollection = new PhoneMetadataCollection(); try { - metadataCollection.readExternal(source); + CodedInputByteBufferNano byteBuffer = MetadataManager.convertStreamToByteBuffer(source, + MULTI_FILE_BUFFER_SIZE); + metadataCollection.mergeFrom(byteBuffer); } catch (IOException e) { logger.log(Level.WARNING, "error reading input (ignored)", e); } finally { diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java index fdfc8c1a9..a9d918f22 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java @@ -19,10 +19,10 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.PhoneNumberUtil.Leniency; import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; import java.lang.Character.UnicodeBlock; import java.util.Iterator; diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index b785824ae..6b4ff847b 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -16,11 +16,11 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import java.io.InputStream; import java.util.ArrayList; @@ -586,6 +586,24 @@ public class PhoneNumberUtil { nanpaRegions.addAll(countryCallingCodeToRegionCodeMap.get(NANPA_COUNTRY_CODE)); } + /** + * Returns a copy of the given NumberFormat object. + */ + static NumberFormat copyNumberFormat(NumberFormat other) { + NumberFormat copy = new NumberFormat(); + copy.pattern = other.pattern; + copy.format = other.format; + int leadingDigitsPatternSize = other.leadingDigitsPattern.length; + copy.leadingDigitsPattern = new String[leadingDigitsPatternSize]; + for (int i = 0; i < leadingDigitsPatternSize; i++) { + copy.leadingDigitsPattern[i] = other.leadingDigitsPattern[i]; + } + copy.nationalPrefixFormattingRule = other.nationalPrefixFormattingRule; + copy.domesticCarrierCodeFormattingRule = other.domesticCarrierCodeFormattingRule; + copy.nationalPrefixOptionalWhenFormatting = other.nationalPrefixOptionalWhenFormatting; + return copy; + } + /** * Attempts to extract a possible number from the string passed in. This currently strips all * leading characters that cannot be used to start a phone number. Characters that can be used to @@ -1119,11 +1137,10 @@ public class PhoneNumberUtil { // If no pattern above is matched, we format the number as a whole. formattedNumber.append(nationalSignificantNumber); } else { - NumberFormat numFormatCopy = new NumberFormat(); // Before we do a replacement of the national prefix pattern $NP with the national prefix, we // need to copy the rule so that subsequent replacements for different numbers have the // appropriate national prefix. - numFormatCopy.mergeFrom(formattingPattern); + NumberFormat numFormatCopy = copyNumberFormat(formattingPattern); String nationalPrefixFormattingRule = formattingPattern.nationalPrefixFormattingRule; if (nationalPrefixFormattingRule.length() > 0) { String nationalPrefix = metadata.nationalPrefix; @@ -1475,8 +1492,7 @@ public class PhoneNumberUtil { break; } // Otherwise, we need to remove the national prefix from our output. - NumberFormat numFormatCopy = new NumberFormat(); - numFormatCopy.mergeFrom(formatRule); + NumberFormat numFormatCopy = copyNumberFormat(formatRule); numFormatCopy.nationalPrefixFormattingRule = ""; List numberFormats = new ArrayList(1); numberFormats.add(numFormatCopy); @@ -1604,8 +1620,7 @@ public class PhoneNumberUtil { // If no pattern above is matched, we format the original input. return rawInput; } - NumberFormat newFormat = new NumberFormat(); - newFormat.mergeFrom(formattingPattern); + NumberFormat newFormat = copyNumberFormat(formattingPattern); // The first group is the first group of digits that the user wrote together. newFormat.pattern = "(\\d+)(.*)"; // Here we just concatenate them back together after the national prefix has been fixed. diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonemetadata.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonemetadata.java deleted file mode 100644 index 4bc0a1699..000000000 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonemetadata.java +++ /dev/null @@ -1,603 +0,0 @@ -/* - * Copyright (C) 2010 The Libphonenumber Authors - * - * 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. - */ - -/** - * Definition of the class representing metadata for international telephone numbers. This class is - * hand created based on the class file compiled from phonemetadata.proto. Please refer to that file - * for detailed descriptions of the meaning of each field. - */ - -package com.google.i18n.phonenumbers; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.io.OutputStream; - -public final class Phonemetadata { - private Phonemetadata() {} - public static class NumberFormat implements Externalizable { - private static final long serialVersionUID = 1; - public NumberFormat() {} - - // required string pattern = 1; - public String pattern = ""; - - // required string format = 2; - public String format = ""; - - // repeated string leading_digits_pattern = 3; - public String[] leadingDigitsPattern = new String[0]; - - // optional string national_prefix_formatting_rule = 4; - public String nationalPrefixFormattingRule = ""; - - // optional bool national_prefix_optional_when_formatting = 6; - public boolean nationalPrefixOptionalWhenFormatting = false; - - // optional string domestic_carrier_code_formatting_rule = 5; - public String domesticCarrierCodeFormattingRule = ""; - - public NumberFormat mergeFrom(NumberFormat other) { - if (other.pattern.length() != 0) { - pattern = other.pattern; - } - if (other.format.length() != 0) { - format = other.format; - } - int leadingDigitsPatternSize = other.leadingDigitsPattern.length; - leadingDigitsPattern = new String[leadingDigitsPatternSize]; - for (int i = 0; i < leadingDigitsPatternSize; i++) { - leadingDigitsPattern[i] = other.leadingDigitsPattern[i]; - } - if (other.nationalPrefixFormattingRule.length() != 0) { - nationalPrefixFormattingRule = other.nationalPrefixFormattingRule; - } - if (other.domesticCarrierCodeFormattingRule.length() != 0) { - domesticCarrierCodeFormattingRule = other.domesticCarrierCodeFormattingRule; - } - nationalPrefixOptionalWhenFormatting = other.nationalPrefixOptionalWhenFormatting; - return this; - } - - public void writeExternal(ObjectOutput objectOutput) throws IOException { - objectOutput.writeUTF(pattern); - objectOutput.writeUTF(format); - int leadingDigitsPatternSize = leadingDigitsPattern.length; - objectOutput.writeInt(leadingDigitsPatternSize); - for (int i = 0; i < leadingDigitsPatternSize; i++) { - objectOutput.writeUTF(leadingDigitsPattern[i]); - } - - objectOutput.writeBoolean(nationalPrefixFormattingRule.length() != 0); - if (nationalPrefixFormattingRule.length() != 0) { - objectOutput.writeUTF(nationalPrefixFormattingRule); - } - objectOutput.writeBoolean(domesticCarrierCodeFormattingRule.length() != 0); - if (domesticCarrierCodeFormattingRule.length() != 0) { - objectOutput.writeUTF(domesticCarrierCodeFormattingRule); - } - objectOutput.writeBoolean(nationalPrefixOptionalWhenFormatting); - } - - public void readExternal(ObjectInput objectInput) throws IOException { - pattern = objectInput.readUTF(); - format = objectInput.readUTF(); - int leadingDigitsPatternSize = objectInput.readInt(); - leadingDigitsPattern = new String[leadingDigitsPatternSize]; - for (int i = 0; i < leadingDigitsPatternSize; i++) { - leadingDigitsPattern[i] = objectInput.readUTF(); - } - if (objectInput.readBoolean()) { - nationalPrefixFormattingRule = objectInput.readUTF(); - } - if (objectInput.readBoolean()) { - domesticCarrierCodeFormattingRule = objectInput.readUTF(); - } - nationalPrefixOptionalWhenFormatting = objectInput.readBoolean(); - } - } - - public static class PhoneNumberDesc implements Externalizable { - private static final long serialVersionUID = 1; - public PhoneNumberDesc() {} - - // optional string national_number_pattern = 2; - public String nationalNumberPattern = ""; - - // optional string possible_number_pattern = 3; - public String possibleNumberPattern = ""; - - // optional string example_number = 6; - public String exampleNumber = ""; - - public PhoneNumberDesc mergeFrom(PhoneNumberDesc other) { - if (other.nationalNumberPattern.length() != 0) { - nationalNumberPattern = other.nationalNumberPattern; - } - if (other.possibleNumberPattern.length() != 0) { - possibleNumberPattern = other.possibleNumberPattern; - } - if (other.exampleNumber.length() != 0) { - exampleNumber = other.exampleNumber; - } - return this; - } - - public boolean exactlySameAs(PhoneNumberDesc other) { - return nationalNumberPattern.equals(other.nationalNumberPattern) && - possibleNumberPattern.equals(other.possibleNumberPattern) && - exampleNumber.equals(other.exampleNumber); - } - - public void writeExternal(ObjectOutput objectOutput) throws IOException { - objectOutput.writeBoolean(nationalNumberPattern.length() != 0); - if (nationalNumberPattern.length() != 0) { - objectOutput.writeUTF(nationalNumberPattern); - } - - objectOutput.writeBoolean(possibleNumberPattern.length() != 0); - if (possibleNumberPattern.length() != 0) { - objectOutput.writeUTF(possibleNumberPattern); - } - - objectOutput.writeBoolean(exampleNumber.length() != 0); - if (exampleNumber.length() != 0) { - objectOutput.writeUTF(exampleNumber); - } - } - - public void readExternal(ObjectInput objectInput) throws IOException { - if (objectInput.readBoolean()) { - nationalNumberPattern = objectInput.readUTF(); - } - - if (objectInput.readBoolean()) { - possibleNumberPattern = objectInput.readUTF(); - } - - if (objectInput.readBoolean()) { - exampleNumber = objectInput.readUTF(); - } - } - } - - public static class PhoneMetadata implements Externalizable { - private static final long serialVersionUID = 1; - public PhoneMetadata() {} - - // optional PhoneNumberDesc general_desc = 1; - public PhoneNumberDesc generalDesc = null; - - // optional PhoneNumberDesc fixed_line = 2; - public PhoneNumberDesc fixedLine = null; - - // optional PhoneNumberDesc mobile = 3; - public PhoneNumberDesc mobile = null; - - // optional PhoneNumberDesc toll_free = 4; - public PhoneNumberDesc tollFree = null; - - // optional PhoneNumberDesc premium_rate = 5; - public PhoneNumberDesc premiumRate = null; - - // optional PhoneNumberDesc shared_cost = 6; - public PhoneNumberDesc sharedCost = null; - - // optional PhoneNumberDesc personal_number = 7; - public PhoneNumberDesc personalNumber = null; - - // optional PhoneNumberDesc voip = 8; - public PhoneNumberDesc voip = null; - - // optional PhoneNumberDesc pager = 21; - public PhoneNumberDesc pager = null; - - // optional PhoneNumberDesc uan = 25; - public PhoneNumberDesc uan = null; - - // optional PhoneNumberDesc emergency = 27; - public PhoneNumberDesc emergency = null; - - // optional PhoneNumberDesc voicemail = 28; - public PhoneNumberDesc voicemail = null; - - // optional PhoneNumberDesc short_code = 29; - public PhoneNumberDesc shortCode = null; - - // optional PhoneNumberDesc standard_rate = 30; - public PhoneNumberDesc standardRate = null; - - // optional PhoneNumberDesc carrier_specific = 31; - public PhoneNumberDesc carrierSpecific = null; - - // optional PhoneNumberDesc noInternationalDialling = 24; - public PhoneNumberDesc noInternationalDialling = null; - - // required string id = 9; - public String id = ""; - - // optional int32 country_code = 10; - public int countryCode = 0; - - // optional string international_prefix = 11; - public String internationalPrefix = ""; - - // optional string preferred_international_prefix = 17; - public String preferredInternationalPrefix = ""; - - // optional string national_prefix = 12; - public String nationalPrefix = ""; - - // optional string preferred_extn_prefix = 13; - public String preferredExtnPrefix = ""; - - // optional string national_prefix_for_parsing = 15; - public String nationalPrefixForParsing = ""; - - // optional string national_prefix_transform_rule = 16; - public String nationalPrefixTransformRule = ""; - - // optional bool same_mobile_and_fixed_line_pattern = 18 [default = false]; - public boolean sameMobileAndFixedLinePattern = false; - - // repeated NumberFormat number_format = 19; - public NumberFormat[] numberFormat = new NumberFormat[0]; - - // repeated NumberFormat intl_number_format = 20; - public NumberFormat[] intlNumberFormat = new NumberFormat[0]; - - // optional bool main_country_for_code = 22 [default = false]; - public boolean mainCountryForCode = false; - - // optional string leading_digits = 23; - public String leadingDigits = ""; - - // optional bool leading_zero_possible = 26 [default = false]; - public boolean leadingZeroPossible = false; - - // optional bool mobile_number_portable_region = 32 [default = false]; - public boolean mobileNumberPortableRegion = false; - - public void writeExternal(ObjectOutput objectOutput) throws IOException { - objectOutput.writeBoolean(generalDesc != null); - if (generalDesc != null) { - generalDesc.writeExternal(objectOutput); - } - objectOutput.writeBoolean(fixedLine != null); - if (fixedLine != null) { - fixedLine.writeExternal(objectOutput); - } - objectOutput.writeBoolean(mobile != null); - if (mobile != null) { - mobile.writeExternal(objectOutput); - } - objectOutput.writeBoolean(tollFree != null); - if (tollFree != null) { - tollFree.writeExternal(objectOutput); - } - objectOutput.writeBoolean(premiumRate != null); - if (premiumRate != null) { - premiumRate.writeExternal(objectOutput); - } - objectOutput.writeBoolean(sharedCost != null); - if (sharedCost != null) { - sharedCost.writeExternal(objectOutput); - } - objectOutput.writeBoolean(personalNumber != null); - if (personalNumber != null) { - personalNumber.writeExternal(objectOutput); - } - objectOutput.writeBoolean(voip != null); - if (voip != null) { - voip.writeExternal(objectOutput); - } - objectOutput.writeBoolean(pager != null); - if (pager != null) { - pager.writeExternal(objectOutput); - } - objectOutput.writeBoolean(uan != null); - if (uan != null) { - uan.writeExternal(objectOutput); - } - objectOutput.writeBoolean(emergency != null); - if (emergency != null) { - emergency.writeExternal(objectOutput); - } - objectOutput.writeBoolean(voicemail != null); - if (voicemail != null) { - voicemail.writeExternal(objectOutput); - } - objectOutput.writeBoolean(shortCode != null); - if (shortCode != null) { - shortCode.writeExternal(objectOutput); - } - objectOutput.writeBoolean(standardRate != null); - if (standardRate != null) { - standardRate.writeExternal(objectOutput); - } - objectOutput.writeBoolean(carrierSpecific != null); - if (carrierSpecific != null) { - carrierSpecific.writeExternal(objectOutput); - } - objectOutput.writeBoolean(noInternationalDialling != null); - if (noInternationalDialling != null) { - noInternationalDialling.writeExternal(objectOutput); - } - - objectOutput.writeUTF(id); - objectOutput.writeInt(countryCode); - objectOutput.writeUTF(internationalPrefix); - - objectOutput.writeBoolean(preferredInternationalPrefix.length() != 0); - if (preferredInternationalPrefix.length() != 0) { - objectOutput.writeUTF(preferredInternationalPrefix); - } - - objectOutput.writeBoolean(nationalPrefix.length() != 0); - if (nationalPrefix.length() != 0) { - objectOutput.writeUTF(nationalPrefix); - } - - objectOutput.writeBoolean(preferredExtnPrefix.length() != 0); - if (preferredExtnPrefix.length() != 0) { - objectOutput.writeUTF(preferredExtnPrefix); - } - - objectOutput.writeBoolean(nationalPrefixForParsing.length() != 0); - if (nationalPrefixForParsing.length() != 0) { - objectOutput.writeUTF(nationalPrefixForParsing); - } - - objectOutput.writeBoolean(nationalPrefixTransformRule.length() != 0); - if (nationalPrefixTransformRule.length() != 0) { - objectOutput.writeUTF(nationalPrefixTransformRule); - } - - objectOutput.writeBoolean(sameMobileAndFixedLinePattern); - - int numberFormatSize = numberFormat.length; - objectOutput.writeInt(numberFormatSize); - for (int i = 0; i < numberFormatSize; i++) { - numberFormat[i].writeExternal(objectOutput); - } - - int intlNumberFormatSize = intlNumberFormat.length; - objectOutput.writeInt(intlNumberFormatSize); - for (int i = 0; i < intlNumberFormatSize; i++) { - intlNumberFormat[i].writeExternal(objectOutput); - } - - objectOutput.writeBoolean(mainCountryForCode); - - objectOutput.writeBoolean(leadingDigits.length() != 0); - if (leadingDigits.length() != 3) { - objectOutput.writeUTF(leadingDigits); - } - - objectOutput.writeBoolean(leadingZeroPossible); - - objectOutput.writeBoolean(mobileNumberPortableRegion); - } - - public void readExternal(ObjectInput objectInput) throws IOException { - boolean hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - generalDesc = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - fixedLine = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - mobile = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - tollFree = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - premiumRate = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - sharedCost = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - personalNumber = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - voip = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - pager = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - uan = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - emergency = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - voicemail = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - shortCode = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - standardRate = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - carrierSpecific = desc; - } - hasDesc = objectInput.readBoolean(); - if (hasDesc) { - PhoneNumberDesc desc = new PhoneNumberDesc(); - desc.readExternal(objectInput); - noInternationalDialling = desc; - } - - id = objectInput.readUTF(); - countryCode = objectInput.readInt(); - internationalPrefix = objectInput.readUTF(); - - boolean hasString = objectInput.readBoolean(); - if (hasString) { - preferredInternationalPrefix = objectInput.readUTF(); - } - - hasString = objectInput.readBoolean(); - if (hasString) { - nationalPrefix = objectInput.readUTF(); - } - - hasString = objectInput.readBoolean(); - if (hasString) { - preferredExtnPrefix = objectInput.readUTF(); - } - - hasString = objectInput.readBoolean(); - if (hasString) { - nationalPrefixForParsing = objectInput.readUTF(); - } - - hasString = objectInput.readBoolean(); - if (hasString) { - nationalPrefixTransformRule = objectInput.readUTF(); - } - - sameMobileAndFixedLinePattern = objectInput.readBoolean(); - - int nationalFormatSize = objectInput.readInt(); - numberFormat = new NumberFormat[nationalFormatSize]; - for (int i = 0; i < nationalFormatSize; i++) { - numberFormat[i] = new NumberFormat(); - numberFormat[i].readExternal(objectInput); - } - - int intlNumberFormatSize = objectInput.readInt(); - intlNumberFormat = new NumberFormat[intlNumberFormatSize]; - for (int i = 0; i < intlNumberFormatSize; i++) { - intlNumberFormat[i] = new NumberFormat(); - intlNumberFormat[i].readExternal(objectInput); - } - - mainCountryForCode = objectInput.readBoolean(); - - hasString = objectInput.readBoolean(); - if (hasString) { - leadingDigits = objectInput.readUTF(); - } - - leadingZeroPossible = objectInput.readBoolean(); - - mobileNumberPortableRegion = objectInput.readBoolean(); - } - } - - public static class PhoneMetadataCollection implements Externalizable { - private static final long serialVersionUID = 1; - public PhoneMetadataCollection() {} - - // repeated PhoneMetadata metadata = 1; - public PhoneMetadata[] metadata = new PhoneMetadata[0]; - - public PhoneMetadataCollection addMetadata(PhoneMetadata value) { - if (value == null) { - throw new NullPointerException(); - } - List metadataList = - new ArrayList(Arrays.asList(metadata)); - metadataList.add(value); - metadata = metadataList.toArray(new PhoneMetadata[metadataList.size()]); - return this; - } - - public void writeExternal(ObjectOutput objectOutput) throws IOException { - int size = metadata.length; - objectOutput.writeInt(size); - for (int i = 0; i < size; i++) { - metadata[i].writeExternal(objectOutput); - } - } - - public void readExternal(ObjectInput objectInput) throws IOException { - int size = objectInput.readInt(); - for (int i = 0; i < size; i++) { - PhoneMetadata phoneMetadata = new PhoneMetadata(); - phoneMetadata.readExternal(objectInput); - List metadataList = - new ArrayList(Arrays.asList(metadata)); - metadataList.add(phoneMetadata); - metadata = metadataList.toArray(new PhoneMetadata[metadataList.size()]); - } - } - - public PhoneMetadataCollection clear() { - metadata = new PhoneMetadata[0]; - return this; - } - - public void writeTo(OutputStream output) throws java.io.IOException { - // Note: This is a stub for compilation purposes. - } - } -} diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java index 3ca42c575..fe5ca8f33 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/ShortNumberInfo.java @@ -16,11 +16,11 @@ package com.google.i18n.phonenumbers; +import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.internal.MatcherApi; import com.google.i18n.phonenumbers.internal.RegexBasedMatcher; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; -import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import java.util.ArrayList; import java.util.Arrays; diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java index c792811c7..e20268f0e 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/SingleFileMetadataSourceImpl.java @@ -16,8 +16,9 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.protobuf.nano.CodedInputByteBufferNano; import java.io.IOException; import java.io.InputStream; @@ -133,9 +134,15 @@ final class SingleFileMetadataSourceImpl implements MetadataSource { * @return the loaded metadata protocol buffer. */ private static PhoneMetadataCollection loadMetadataAndCloseInput(ObjectInputStream source) { + // The size of the byte buffer for deserializing the single nano metadata file which holds + // metadata for all regions. + final int SINGLE_FILE_BUFFER_SIZE = 256 * 1024; + PhoneMetadataCollection metadataCollection = new PhoneMetadataCollection(); try { - metadataCollection.readExternal(source); + CodedInputByteBufferNano byteBuffer = MetadataManager.convertStreamToByteBuffer( + source, SINGLE_FILE_BUFFER_SIZE); + metadataCollection.mergeFrom(byteBuffer); } catch (IOException e) { logger.log(Level.WARNING, "error reading input (ignored)", e); } finally { diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/MatcherApi.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/MatcherApi.java index 38319cbb3..ac2013669 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/MatcherApi.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/MatcherApi.java @@ -16,7 +16,7 @@ package com.google.i18n.phonenumbers.internal; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; /** * Internal phonenumber matching API used to isolate the underlying implementation of the diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/RegexBasedMatcher.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/RegexBasedMatcher.java index 1ada3014f..b1e7a23f3 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/RegexBasedMatcher.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/internal/RegexBasedMatcher.java @@ -16,8 +16,8 @@ package com.google.i18n.phonenumbers.internal; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.RegexCache; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import java.util.regex.Matcher; diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/nano/Phonemetadata.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/nano/Phonemetadata.java new file mode 100644 index 000000000..abf4a67bf --- /dev/null +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/nano/Phonemetadata.java @@ -0,0 +1,1040 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! + +package com.google.i18n.phonenumbers.nano; + +@SuppressWarnings("hiding") +public interface Phonemetadata { + + public static final class NumberFormat extends + com.google.protobuf.nano.MessageNano { + + private static volatile NumberFormat[] _emptyArray; + public static NumberFormat[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new NumberFormat[0]; + } + } + } + return _emptyArray; + } + + // required string pattern = 1; + public java.lang.String pattern; + + // required string format = 2; + public java.lang.String format; + + // repeated string leading_digits_pattern = 3; + public java.lang.String[] leadingDigitsPattern; + + // optional string national_prefix_formatting_rule = 4; + public java.lang.String nationalPrefixFormattingRule; + + // optional bool national_prefix_optional_when_formatting = 6; + public boolean nationalPrefixOptionalWhenFormatting; + + // optional string domestic_carrier_code_formatting_rule = 5; + public java.lang.String domesticCarrierCodeFormattingRule; + + public NumberFormat() { + clear(); + } + + public NumberFormat clear() { + pattern = ""; + format = ""; + leadingDigitsPattern = com.google.protobuf.nano.WireFormatNano.EMPTY_STRING_ARRAY; + nationalPrefixFormattingRule = ""; + nationalPrefixOptionalWhenFormatting = false; + domesticCarrierCodeFormattingRule = ""; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + output.writeString(1, this.pattern); + output.writeString(2, this.format); + if (this.leadingDigitsPattern != null && this.leadingDigitsPattern.length > 0) { + for (int i = 0; i < this.leadingDigitsPattern.length; i++) { + java.lang.String element = this.leadingDigitsPattern[i]; + if (element != null) { + output.writeString(3, element); + } + } + } + if (!this.nationalPrefixFormattingRule.equals("")) { + output.writeString(4, this.nationalPrefixFormattingRule); + } + if (!this.domesticCarrierCodeFormattingRule.equals("")) { + output.writeString(5, this.domesticCarrierCodeFormattingRule); + } + if (this.nationalPrefixOptionalWhenFormatting != false) { + output.writeBool(6, this.nationalPrefixOptionalWhenFormatting); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(1, this.pattern); + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(2, this.format); + if (this.leadingDigitsPattern != null && this.leadingDigitsPattern.length > 0) { + int dataCount = 0; + int dataSize = 0; + for (int i = 0; i < this.leadingDigitsPattern.length; i++) { + java.lang.String element = this.leadingDigitsPattern[i]; + if (element != null) { + dataCount++; + dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSizeNoTag(element); + } + } + size += dataSize; + size += 1 * dataCount; + } + if (!this.nationalPrefixFormattingRule.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(4, this.nationalPrefixFormattingRule); + } + if (!this.domesticCarrierCodeFormattingRule.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(5, this.domesticCarrierCodeFormattingRule); + } + if (this.nationalPrefixOptionalWhenFormatting != false) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeBoolSize(6, this.nationalPrefixOptionalWhenFormatting); + } + return size; + } + + @Override + public NumberFormat mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 10: { + this.pattern = input.readString(); + break; + } + case 18: { + this.format = input.readString(); + break; + } + case 26: { + int arrayLength = com.google.protobuf.nano.WireFormatNano + .getRepeatedFieldArrayLength(input, 26); + int i = this.leadingDigitsPattern == null ? 0 : this.leadingDigitsPattern.length; + java.lang.String[] newArray = new java.lang.String[i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.leadingDigitsPattern, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = input.readString(); + input.readTag(); + } + // Last one without readTag. + newArray[i] = input.readString(); + this.leadingDigitsPattern = newArray; + break; + } + case 34: { + this.nationalPrefixFormattingRule = input.readString(); + break; + } + case 42: { + this.domesticCarrierCodeFormattingRule = input.readString(); + break; + } + case 48: { + this.nationalPrefixOptionalWhenFormatting = input.readBool(); + break; + } + } + } + } + + public static NumberFormat parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new NumberFormat(), data); + } + + public static NumberFormat parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new NumberFormat().mergeFrom(input); + } + } + + public static final class PhoneNumberDesc extends + com.google.protobuf.nano.MessageNano { + + private static volatile PhoneNumberDesc[] _emptyArray; + public static PhoneNumberDesc[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new PhoneNumberDesc[0]; + } + } + } + return _emptyArray; + } + + // optional string national_number_pattern = 2; + public java.lang.String nationalNumberPattern; + + // optional string possible_number_pattern = 3; + public java.lang.String possibleNumberPattern; + + // optional string example_number = 6; + public java.lang.String exampleNumber; + + public PhoneNumberDesc() { + clear(); + } + + public PhoneNumberDesc clear() { + nationalNumberPattern = ""; + possibleNumberPattern = ""; + exampleNumber = ""; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (!this.nationalNumberPattern.equals("")) { + output.writeString(2, this.nationalNumberPattern); + } + if (!this.possibleNumberPattern.equals("")) { + output.writeString(3, this.possibleNumberPattern); + } + if (!this.exampleNumber.equals("")) { + output.writeString(6, this.exampleNumber); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (!this.nationalNumberPattern.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(2, this.nationalNumberPattern); + } + if (!this.possibleNumberPattern.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(3, this.possibleNumberPattern); + } + if (!this.exampleNumber.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(6, this.exampleNumber); + } + return size; + } + + @Override + public PhoneNumberDesc mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 18: { + this.nationalNumberPattern = input.readString(); + break; + } + case 26: { + this.possibleNumberPattern = input.readString(); + break; + } + case 50: { + this.exampleNumber = input.readString(); + break; + } + } + } + } + + public static PhoneNumberDesc parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new PhoneNumberDesc(), data); + } + + public static PhoneNumberDesc parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new PhoneNumberDesc().mergeFrom(input); + } + } + + public static final class PhoneMetadata extends + com.google.protobuf.nano.MessageNano { + + private static volatile PhoneMetadata[] _emptyArray; + public static PhoneMetadata[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new PhoneMetadata[0]; + } + } + } + return _emptyArray; + } + + // optional .i18n.phonenumbers.PhoneNumberDesc general_desc = 1; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc generalDesc; + + // optional .i18n.phonenumbers.PhoneNumberDesc fixed_line = 2; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc fixedLine; + + // optional .i18n.phonenumbers.PhoneNumberDesc mobile = 3; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc mobile; + + // optional .i18n.phonenumbers.PhoneNumberDesc toll_free = 4; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc tollFree; + + // optional .i18n.phonenumbers.PhoneNumberDesc premium_rate = 5; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc premiumRate; + + // optional .i18n.phonenumbers.PhoneNumberDesc shared_cost = 6; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc sharedCost; + + // optional .i18n.phonenumbers.PhoneNumberDesc personal_number = 7; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc personalNumber; + + // optional .i18n.phonenumbers.PhoneNumberDesc voip = 8; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc voip; + + // optional .i18n.phonenumbers.PhoneNumberDesc pager = 21; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc pager; + + // optional .i18n.phonenumbers.PhoneNumberDesc uan = 25; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc uan; + + // optional .i18n.phonenumbers.PhoneNumberDesc emergency = 27; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc emergency; + + // optional .i18n.phonenumbers.PhoneNumberDesc voicemail = 28; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc voicemail; + + // optional .i18n.phonenumbers.PhoneNumberDesc short_code = 29; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc shortCode; + + // optional .i18n.phonenumbers.PhoneNumberDesc standard_rate = 30; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc standardRate; + + // optional .i18n.phonenumbers.PhoneNumberDesc carrier_specific = 31; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc carrierSpecific; + + // optional .i18n.phonenumbers.PhoneNumberDesc no_international_dialling = 24; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc noInternationalDialling; + + // required string id = 9; + public java.lang.String id; + + // optional int32 country_code = 10; + public int countryCode; + + // optional string international_prefix = 11; + public java.lang.String internationalPrefix; + + // optional string preferred_international_prefix = 17; + public java.lang.String preferredInternationalPrefix; + + // optional string national_prefix = 12; + public java.lang.String nationalPrefix; + + // optional string preferred_extn_prefix = 13; + public java.lang.String preferredExtnPrefix; + + // optional string national_prefix_for_parsing = 15; + public java.lang.String nationalPrefixForParsing; + + // optional string national_prefix_transform_rule = 16; + public java.lang.String nationalPrefixTransformRule; + + // optional bool same_mobile_and_fixed_line_pattern = 18 [default = false]; + public boolean sameMobileAndFixedLinePattern; + + // repeated .i18n.phonenumbers.NumberFormat number_format = 19; + public com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[] numberFormat; + + // repeated .i18n.phonenumbers.NumberFormat intl_number_format = 20; + public com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[] intlNumberFormat; + + // optional bool main_country_for_code = 22 [default = false]; + public boolean mainCountryForCode; + + // optional string leading_digits = 23; + public java.lang.String leadingDigits; + + // optional bool leading_zero_possible = 26 [default = false]; + public boolean leadingZeroPossible; + + // optional bool mobile_number_portable_region = 32 [default = false]; + public boolean mobileNumberPortableRegion; + + public PhoneMetadata() { + clear(); + } + + public PhoneMetadata clear() { + generalDesc = null; + fixedLine = null; + mobile = null; + tollFree = null; + premiumRate = null; + sharedCost = null; + personalNumber = null; + voip = null; + pager = null; + uan = null; + emergency = null; + voicemail = null; + shortCode = null; + standardRate = null; + carrierSpecific = null; + noInternationalDialling = null; + id = ""; + countryCode = 0; + internationalPrefix = ""; + preferredInternationalPrefix = ""; + nationalPrefix = ""; + preferredExtnPrefix = ""; + nationalPrefixForParsing = ""; + nationalPrefixTransformRule = ""; + sameMobileAndFixedLinePattern = false; + numberFormat = com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat.emptyArray(); + intlNumberFormat = com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat.emptyArray(); + mainCountryForCode = false; + leadingDigits = ""; + leadingZeroPossible = false; + mobileNumberPortableRegion = false; + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (this.generalDesc != null) { + output.writeMessage(1, this.generalDesc); + } + if (this.fixedLine != null) { + output.writeMessage(2, this.fixedLine); + } + if (this.mobile != null) { + output.writeMessage(3, this.mobile); + } + if (this.tollFree != null) { + output.writeMessage(4, this.tollFree); + } + if (this.premiumRate != null) { + output.writeMessage(5, this.premiumRate); + } + if (this.sharedCost != null) { + output.writeMessage(6, this.sharedCost); + } + if (this.personalNumber != null) { + output.writeMessage(7, this.personalNumber); + } + if (this.voip != null) { + output.writeMessage(8, this.voip); + } + output.writeString(9, this.id); + if (this.countryCode != 0) { + output.writeInt32(10, this.countryCode); + } + if (!this.internationalPrefix.equals("")) { + output.writeString(11, this.internationalPrefix); + } + if (!this.nationalPrefix.equals("")) { + output.writeString(12, this.nationalPrefix); + } + if (!this.preferredExtnPrefix.equals("")) { + output.writeString(13, this.preferredExtnPrefix); + } + if (!this.nationalPrefixForParsing.equals("")) { + output.writeString(15, this.nationalPrefixForParsing); + } + if (!this.nationalPrefixTransformRule.equals("")) { + output.writeString(16, this.nationalPrefixTransformRule); + } + if (!this.preferredInternationalPrefix.equals("")) { + output.writeString(17, this.preferredInternationalPrefix); + } + if (this.sameMobileAndFixedLinePattern != false) { + output.writeBool(18, this.sameMobileAndFixedLinePattern); + } + if (this.numberFormat != null && this.numberFormat.length > 0) { + for (int i = 0; i < this.numberFormat.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat element = this.numberFormat[i]; + if (element != null) { + output.writeMessage(19, element); + } + } + } + if (this.intlNumberFormat != null && this.intlNumberFormat.length > 0) { + for (int i = 0; i < this.intlNumberFormat.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat element = this.intlNumberFormat[i]; + if (element != null) { + output.writeMessage(20, element); + } + } + } + if (this.pager != null) { + output.writeMessage(21, this.pager); + } + if (this.mainCountryForCode != false) { + output.writeBool(22, this.mainCountryForCode); + } + if (!this.leadingDigits.equals("")) { + output.writeString(23, this.leadingDigits); + } + if (this.noInternationalDialling != null) { + output.writeMessage(24, this.noInternationalDialling); + } + if (this.uan != null) { + output.writeMessage(25, this.uan); + } + if (this.leadingZeroPossible != false) { + output.writeBool(26, this.leadingZeroPossible); + } + if (this.emergency != null) { + output.writeMessage(27, this.emergency); + } + if (this.voicemail != null) { + output.writeMessage(28, this.voicemail); + } + if (this.shortCode != null) { + output.writeMessage(29, this.shortCode); + } + if (this.standardRate != null) { + output.writeMessage(30, this.standardRate); + } + if (this.carrierSpecific != null) { + output.writeMessage(31, this.carrierSpecific); + } + if (this.mobileNumberPortableRegion != false) { + output.writeBool(32, this.mobileNumberPortableRegion); + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (this.generalDesc != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(1, this.generalDesc); + } + if (this.fixedLine != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(2, this.fixedLine); + } + if (this.mobile != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(3, this.mobile); + } + if (this.tollFree != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(4, this.tollFree); + } + if (this.premiumRate != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(5, this.premiumRate); + } + if (this.sharedCost != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(6, this.sharedCost); + } + if (this.personalNumber != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(7, this.personalNumber); + } + if (this.voip != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(8, this.voip); + } + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(9, this.id); + if (this.countryCode != 0) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeInt32Size(10, this.countryCode); + } + if (!this.internationalPrefix.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(11, this.internationalPrefix); + } + if (!this.nationalPrefix.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(12, this.nationalPrefix); + } + if (!this.preferredExtnPrefix.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(13, this.preferredExtnPrefix); + } + if (!this.nationalPrefixForParsing.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(15, this.nationalPrefixForParsing); + } + if (!this.nationalPrefixTransformRule.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(16, this.nationalPrefixTransformRule); + } + if (!this.preferredInternationalPrefix.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(17, this.preferredInternationalPrefix); + } + if (this.sameMobileAndFixedLinePattern != false) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeBoolSize(18, this.sameMobileAndFixedLinePattern); + } + if (this.numberFormat != null && this.numberFormat.length > 0) { + for (int i = 0; i < this.numberFormat.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat element = this.numberFormat[i]; + if (element != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(19, element); + } + } + } + if (this.intlNumberFormat != null && this.intlNumberFormat.length > 0) { + for (int i = 0; i < this.intlNumberFormat.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat element = this.intlNumberFormat[i]; + if (element != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(20, element); + } + } + } + if (this.pager != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(21, this.pager); + } + if (this.mainCountryForCode != false) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeBoolSize(22, this.mainCountryForCode); + } + if (!this.leadingDigits.equals("")) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeStringSize(23, this.leadingDigits); + } + if (this.noInternationalDialling != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(24, this.noInternationalDialling); + } + if (this.uan != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(25, this.uan); + } + if (this.leadingZeroPossible != false) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeBoolSize(26, this.leadingZeroPossible); + } + if (this.emergency != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(27, this.emergency); + } + if (this.voicemail != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(28, this.voicemail); + } + if (this.shortCode != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(29, this.shortCode); + } + if (this.standardRate != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(30, this.standardRate); + } + if (this.carrierSpecific != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(31, this.carrierSpecific); + } + if (this.mobileNumberPortableRegion != false) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeBoolSize(32, this.mobileNumberPortableRegion); + } + return size; + } + + @Override + public PhoneMetadata mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 10: { + if (this.generalDesc == null) { + this.generalDesc = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.generalDesc); + break; + } + case 18: { + if (this.fixedLine == null) { + this.fixedLine = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.fixedLine); + break; + } + case 26: { + if (this.mobile == null) { + this.mobile = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.mobile); + break; + } + case 34: { + if (this.tollFree == null) { + this.tollFree = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.tollFree); + break; + } + case 42: { + if (this.premiumRate == null) { + this.premiumRate = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.premiumRate); + break; + } + case 50: { + if (this.sharedCost == null) { + this.sharedCost = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.sharedCost); + break; + } + case 58: { + if (this.personalNumber == null) { + this.personalNumber = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.personalNumber); + break; + } + case 66: { + if (this.voip == null) { + this.voip = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.voip); + break; + } + case 74: { + this.id = input.readString(); + break; + } + case 80: { + this.countryCode = input.readInt32(); + break; + } + case 90: { + this.internationalPrefix = input.readString(); + break; + } + case 98: { + this.nationalPrefix = input.readString(); + break; + } + case 106: { + this.preferredExtnPrefix = input.readString(); + break; + } + case 122: { + this.nationalPrefixForParsing = input.readString(); + break; + } + case 130: { + this.nationalPrefixTransformRule = input.readString(); + break; + } + case 138: { + this.preferredInternationalPrefix = input.readString(); + break; + } + case 144: { + this.sameMobileAndFixedLinePattern = input.readBool(); + break; + } + case 154: { + int arrayLength = com.google.protobuf.nano.WireFormatNano + .getRepeatedFieldArrayLength(input, 154); + int i = this.numberFormat == null ? 0 : this.numberFormat.length; + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[] newArray = + new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.numberFormat, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat(); + input.readMessage(newArray[i]); + input.readTag(); + } + // Last one without readTag. + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat(); + input.readMessage(newArray[i]); + this.numberFormat = newArray; + break; + } + case 162: { + int arrayLength = com.google.protobuf.nano.WireFormatNano + .getRepeatedFieldArrayLength(input, 162); + int i = this.intlNumberFormat == null ? 0 : this.intlNumberFormat.length; + com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[] newArray = + new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat[i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.intlNumberFormat, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat(); + input.readMessage(newArray[i]); + input.readTag(); + } + // Last one without readTag. + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat(); + input.readMessage(newArray[i]); + this.intlNumberFormat = newArray; + break; + } + case 170: { + if (this.pager == null) { + this.pager = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.pager); + break; + } + case 176: { + this.mainCountryForCode = input.readBool(); + break; + } + case 186: { + this.leadingDigits = input.readString(); + break; + } + case 194: { + if (this.noInternationalDialling == null) { + this.noInternationalDialling = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.noInternationalDialling); + break; + } + case 202: { + if (this.uan == null) { + this.uan = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.uan); + break; + } + case 208: { + this.leadingZeroPossible = input.readBool(); + break; + } + case 218: { + if (this.emergency == null) { + this.emergency = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.emergency); + break; + } + case 226: { + if (this.voicemail == null) { + this.voicemail = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.voicemail); + break; + } + case 234: { + if (this.shortCode == null) { + this.shortCode = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.shortCode); + break; + } + case 242: { + if (this.standardRate == null) { + this.standardRate = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.standardRate); + break; + } + case 250: { + if (this.carrierSpecific == null) { + this.carrierSpecific = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc(); + } + input.readMessage(this.carrierSpecific); + break; + } + case 256: { + this.mobileNumberPortableRegion = input.readBool(); + break; + } + } + } + } + + public static PhoneMetadata parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new PhoneMetadata(), data); + } + + public static PhoneMetadata parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new PhoneMetadata().mergeFrom(input); + } + } + + public static final class PhoneMetadataCollection extends + com.google.protobuf.nano.MessageNano { + + private static volatile PhoneMetadataCollection[] _emptyArray; + public static PhoneMetadataCollection[] emptyArray() { + // Lazily initializes the empty array + if (_emptyArray == null) { + synchronized ( + com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) { + if (_emptyArray == null) { + _emptyArray = new PhoneMetadataCollection[0]; + } + } + } + return _emptyArray; + } + + // repeated .i18n.phonenumbers.PhoneMetadata metadata = 1; + public com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata[] metadata; + + public PhoneMetadataCollection() { + clear(); + } + + public PhoneMetadataCollection clear() { + metadata = com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata.emptyArray(); + cachedSize = -1; + return this; + } + + @Override + public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output) + throws java.io.IOException { + if (this.metadata != null && this.metadata.length > 0) { + for (int i = 0; i < this.metadata.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata element = this.metadata[i]; + if (element != null) { + output.writeMessage(1, element); + } + } + } + super.writeTo(output); + } + + @Override + protected int computeSerializedSize() { + int size = super.computeSerializedSize(); + if (this.metadata != null && this.metadata.length > 0) { + for (int i = 0; i < this.metadata.length; i++) { + com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata element = this.metadata[i]; + if (element != null) { + size += com.google.protobuf.nano.CodedOutputByteBufferNano + .computeMessageSize(1, element); + } + } + } + return size; + } + + @Override + public PhoneMetadataCollection mergeFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + while (true) { + int tag = input.readTag(); + switch (tag) { + case 0: + return this; + default: { + if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) { + return this; + } + break; + } + case 10: { + int arrayLength = com.google.protobuf.nano.WireFormatNano + .getRepeatedFieldArrayLength(input, 10); + int i = this.metadata == null ? 0 : this.metadata.length; + com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata[] newArray = + new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata[i + arrayLength]; + if (i != 0) { + java.lang.System.arraycopy(this.metadata, 0, newArray, 0, i); + } + for (; i < newArray.length - 1; i++) { + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata(); + input.readMessage(newArray[i]); + input.readTag(); + } + // Last one without readTag. + newArray[i] = new com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata(); + input.readMessage(newArray[i]); + this.metadata = newArray; + break; + } + } + } + } + + public static PhoneMetadataCollection parseFrom(byte[] data) + throws com.google.protobuf.nano.InvalidProtocolBufferNanoException { + return com.google.protobuf.nano.MessageNano.mergeFrom(new PhoneMetadataCollection(), data); + } + + public static PhoneMetadataCollection parseFrom( + com.google.protobuf.nano.CodedInputByteBufferNano input) + throws java.io.IOException { + return new PhoneMetadataCollection().mergeFrom(input); + } + } +} diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java index f6c78c30e..99ed9bd4c 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/ExampleNumbersTest.java @@ -17,8 +17,8 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import junit.framework.TestCase; diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java index d653deedd..7a4b5eca2 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/MetadataManagerTest.java @@ -16,7 +16,7 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; import junit.framework.TestCase; diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 26de65df0..219073148 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -17,11 +17,11 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import java.util.ArrayList; import java.util.List; @@ -145,7 +145,12 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals("[13-689]\\d{9}|2[0-35-9]\\d{8}", metadata.generalDesc.nationalNumberPattern); assertEquals("\\d{7}(?:\\d{3})?", metadata.generalDesc.possibleNumberPattern); - assertTrue(metadata.generalDesc.exactlySameAs(metadata.fixedLine)); + assertTrue(metadata.generalDesc.nationalNumberPattern.equals( + metadata.fixedLine.nationalNumberPattern)); + assertTrue(metadata.generalDesc.possibleNumberPattern.equals( + metadata.fixedLine.possibleNumberPattern)); + assertTrue(metadata.generalDesc.exampleNumber.equals( + metadata.fixedLine.exampleNumber)); assertEquals("\\d{10}", metadata.tollFree.possibleNumberPattern); assertEquals("900\\d{7}", metadata.premiumRate.nationalNumberPattern); // No shared-cost data is available, so it should be initialised to "NA". diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java b/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java index 629af4537..7b0d280e6 100644 --- a/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java +++ b/tools/java/common/src/com/google/i18n/phonenumbers/BuildMetadataFromXml.java @@ -16,10 +16,10 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -103,6 +103,7 @@ public class BuildMetadataFromXml { // a separate constants file. boolean isShortNumberMetadata = inputXmlFile.contains("ShortNumberMetadata"); boolean isAlternateFormatsMetadata = inputXmlFile.contains("PhoneNumberAlternateFormats"); + List phoneMetadataList = new ArrayList(); for (int i = 0; i < numOfTerritories; i++) { Element territoryElement = (Element) territory.item(i); String regionCode = ""; @@ -113,8 +114,10 @@ public class BuildMetadataFromXml { } PhoneMetadata metadata = loadCountryMetadata(regionCode, territoryElement, liteBuild, isShortNumberMetadata, isAlternateFormatsMetadata); - metadataCollection.addMetadata(metadata); + phoneMetadataList.add(metadata); } + metadataCollection.metadata = + phoneMetadataList.toArray(new PhoneMetadata[phoneMetadataList.size()]); return metadataCollection; } @@ -247,7 +250,7 @@ public class BuildMetadataFromXml { throw new RuntimeException("Invalid number of intlFormat patterns for country: " + countryId); } else if (intlFormatPattern.getLength() == 0) { // Default to use the same as the national pattern if none is defined. - intlFormat.mergeFrom(nationalFormat); + intlFormat = PhoneNumberUtil.copyNumberFormat(nationalFormat); } else { intlFormat.pattern = numberFormatElement.getAttribute(PATTERN); setLeadingDigitsPatterns(numberFormatElement, intlFormat); @@ -260,7 +263,7 @@ public class BuildMetadataFromXml { if (!intlFormat.format.isEmpty()) { List formatList = - new ArrayList (Arrays.asList(metadata.intlNumberFormat)); + new ArrayList(Arrays.asList(metadata.intlNumberFormat)); formatList.add(intlFormat); metadata.intlNumberFormat = formatList.toArray(new NumberFormat[formatList.size()]); } @@ -425,7 +428,16 @@ public class BuildMetadataFromXml { numberDesc.possibleNumberPattern = "NA"; return numberDesc; } - numberDesc.mergeFrom(generalDesc); + if (generalDesc.nationalNumberPattern.length() != 0) { + numberDesc.nationalNumberPattern = generalDesc.nationalNumberPattern; + } + if (generalDesc.possibleNumberPattern.length() != 0) { + numberDesc.possibleNumberPattern = generalDesc.possibleNumberPattern; + } + if (generalDesc.exampleNumber.length() != 0) { + numberDesc.exampleNumber = generalDesc.exampleNumber; + } + if (phoneNumberDescList.getLength() > 0) { Element element = (Element) phoneNumberDescList.item(0); NodeList possiblePattern = element.getElementsByTagName(POSSIBLE_NUMBER_PATTERN); diff --git a/tools/java/common/test/com/google/i18n/phonenumbers/BuildMetadataFromXmlTest.java b/tools/java/common/test/com/google/i18n/phonenumbers/BuildMetadataFromXmlTest.java index be0cbb0d3..afb94f257 100644 --- a/tools/java/common/test/com/google/i18n/phonenumbers/BuildMetadataFromXmlTest.java +++ b/tools/java/common/test/com/google/i18n/phonenumbers/BuildMetadataFromXmlTest.java @@ -16,9 +16,9 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import junit.framework.TestCase; diff --git a/tools/java/cpp-build/pom.xml b/tools/java/cpp-build/pom.xml index 6e9062ba6..b5fae7cc4 100644 --- a/tools/java/cpp-build/pom.xml +++ b/tools/java/cpp-build/pom.xml @@ -30,25 +30,6 @@ 1.5 - - - org.apache.maven.plugins - maven-antrun-plugin - - - create-generated-directory - generate-sources - - - - - - - run - - - - org.codehaus.mojo build-helper-maven-plugin @@ -86,7 +67,7 @@ protoc - --java_out=generated + --javanano_out=java_package=phonemetadata.proto|com.google.i18n.phonenumbers.nano:../../../java/libphonenumber/src ../../../resources/phonemetadata.proto --proto_path=../../../resources diff --git a/tools/java/cpp-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java b/tools/java/cpp-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java index 7d29f53f8..08d35474f 100644 --- a/tools/java/cpp-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java +++ b/tools/java/cpp-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java @@ -17,6 +17,8 @@ package com.google.i18n.phonenumbers; import com.google.i18n.phonenumbers.CppMetadataGenerator.Type; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.protobuf.nano.CodedOutputByteBufferNano; import java.io.ByteArrayOutputStream; import java.io.File; @@ -201,8 +203,16 @@ public class BuildMetadataCppFromXml extends Command { // @VisibleForTesting void writePhoneMetadataCollection( String inputFilePath, boolean liteMetadata, OutputStream out) throws IOException, Exception { - // Note: This is a stub and does nothing. - BuildMetadataFromXml.buildPhoneMetadataCollection(inputFilePath, liteMetadata).writeTo(out); + // The size of the byte buffer used for serializing the PhoneMetadataCollection. + int COLLECTION_BUFFER_SIZE = 256 * 1024; + + PhoneMetadataCollection metadataCollection = + BuildMetadataFromXml.buildPhoneMetadataCollection(inputFilePath, liteMetadata); + byte[] outputArray = new byte[COLLECTION_BUFFER_SIZE]; + CodedOutputByteBufferNano outputByteBuffer = CodedOutputByteBufferNano.newInstance(outputArray); + metadataCollection.writeTo(outputByteBuffer); + out.write(outputArray, 0, outputByteBuffer.position()); + out.flush(); } // @VisibleForTesting diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java index c4b2ad613..178926c60 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java @@ -16,10 +16,10 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.NumberFormat; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneNumberDesc; +import com.google.i18n.phonenumbers.nano.Phonemetadata.NumberFormat; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneNumberDesc; import java.io.BufferedWriter; import java.io.FileWriter; diff --git a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java index 4048a8644..0362edfbe 100644 --- a/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java +++ b/tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataProtoFromXml.java @@ -16,8 +16,9 @@ package com.google.i18n.phonenumbers; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata; -import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadata; +import com.google.i18n.phonenumbers.nano.Phonemetadata.PhoneMetadataCollection; +import com.google.protobuf.nano.CodedOutputByteBufferNano; import java.io.BufferedWriter; import java.io.File; @@ -159,23 +160,36 @@ public class BuildMetadataProtoFromXml extends Command { BuildMetadataFromXml.buildPhoneMetadataCollection(inputFile, liteBuild); if (singleFile) { + int SINGLE_FILE_BUFFER_SIZE = 256 * 1024; FileOutputStream output = new FileOutputStream(filePrefix); ObjectOutputStream out = new ObjectOutputStream(output); - metadataCollection.writeExternal(out); + byte[] outputArray = new byte[SINGLE_FILE_BUFFER_SIZE]; + CodedOutputByteBufferNano outputByteBuffer = + CodedOutputByteBufferNano.newInstance(outputArray); + metadataCollection.writeTo(outputByteBuffer); + out.write(outputArray, 0, outputByteBuffer.position()); + out.flush(); out.close(); } else { + int MULTI_FILE_BUFFER_SIZE = 16 * 1024; for (PhoneMetadata metadata : metadataCollection.metadata) { String regionCode = metadata.id; - // For non-geographical country calling codes (e.g. +800), or for alternate formats, use the - // country calling codes instead of the region code to form the file name. + // For non-geographical country calling codes (e.g. +800), or for alternate formats, use + // the country calling codes instead of the region code to form the file name. if (regionCode.equals("001") || regionCode.isEmpty()) { regionCode = Integer.toString(metadata.countryCode); } - PhoneMetadataCollection outMetadataCollection = new PhoneMetadataCollection(); - outMetadataCollection.addMetadata(metadata); + PhoneMetadataCollection singleCollection = new PhoneMetadataCollection(); + singleCollection.metadata = new PhoneMetadata[1]; + singleCollection.metadata[0] = metadata; FileOutputStream outputForRegion = new FileOutputStream(filePrefix + "_" + regionCode); ObjectOutputStream out = new ObjectOutputStream(outputForRegion); - outMetadataCollection.writeExternal(out); + byte[] outputArray = new byte[MULTI_FILE_BUFFER_SIZE]; + CodedOutputByteBufferNano outputBufferNano = + CodedOutputByteBufferNano.newInstance(outputArray); + singleCollection.writeTo(outputBufferNano); + out.write(outputArray, 0, outputBufferNano.position()); + out.flush(); out.close(); } }