Google Protocol Buffers Java实例 - Go语言中文社区

Google Protocol Buffers Java实例


Google Protocol Buffers 是 Google 公司开发的一款简洁、高效的数据序列化/反序列化的工具,有着跨平台、可扩展的优点,尤其适合作为数据存储和RPC数据交换格式。目前,已经被 Hadoop 用作其 RPC 模块的基础依赖。


本文将根据网络上流行的一个例子,用Java程序来直观地展示如何用 Protocol Buffers 进行代码生成、数据序列化存储以及数据反序列化读取的过程。


步骤:

1、环境搭建:

本文在 Windows 系统下进行开发,因此下载https://protobuf.googlecode.com/files/protoc-2.5.0-win32.zip。解压后便可直接使用:


2、创建好工作目录

注:父目录路径为D:StudyComputerStudyBigData_CloudProtobufpractise



3、编辑proto 文件 D:StudyComputerStudyBigData_CloudProtobufpractiseprotoaddressbook.proto,以定义message对象

package tutorial;  
option java_package = "study.java.arithmetic.protobuf";  
option java_outer_classname = "AddressBookProtos";  
message Person {  
  required string name = 1;  
  required int32 id = 2;        // Unique ID number for this person.  
  optional string email = 3;  
  enum PhoneType {  
    MOBILE = 0;  
    HOME = 1;  
    WORK = 2;  
  }  
  message PhoneNumber {  
    required string number = 1;  
    optional PhoneType type = 2 [default = HOME];  
  }  
  repeated PhoneNumber phone = 4;  
}  
// Our address book file is just one of these.  
message AddressBook {  
  repeated Person person = 1;  
} 

4、通过proto文件自动生成java代码

命令:protoc -I=D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\ --java_out=D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\src D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\proto\addressbook.proto

注:AddressBookProtos.java代码在本文最后的“附-1”部分


5、创建 Java 工程,并将protobuf-java-2.5.0.jar包加入到工程classpath中。然后,将自动生成的AddressBookProtos.java添加进工程

6、在工程的同一个文件夹下,创建AddPerson.java类——该类负责将数据写入PB的存储文件中:

package study.java.arithmetic.protobuf;

import study.java.arithmetic.protobuf.AddressBookProtos.AddressBook;  
import study.java.arithmetic.protobuf.AddressBookProtos.Person;   
import java.io.BufferedReader;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;   
import java.io.InputStreamReader;    
import java.io.IOException;    
import java.io.PrintStream;      
class AddPerson{      
    // This function fills in a Person message based on user input.      
    static Person PromptForAddress(BufferedReader stdin,PrintStream stdout)throws IOException{        
        Person.Builder person = Person.newBuilder();  
        stdout.print("Enter person ID: ");        
        person.setId(Integer.valueOf(stdin.readLine()));          
        stdout.print("Enter name: ");        
        person.setName(stdin.readLine());          
        stdout.print("Enter email address (blank for none): ");        
        String email = stdin.readLine();        
        if (email.length() > 0){          
            person.setEmail(email);       
        }         
        while (true){          
            stdout.print("Enter a phone number (or leave blank to finish): ");  
            String number = stdin.readLine();          
            if (number.length() == 0){            
                break;          
            }            
            Person.PhoneNumber.Builder phoneNumber = Person.PhoneNumber.newBuilder().setNumber(number);  
            stdout.print("Is this a mobile, home, or work phone? ");          
            String type = stdin.readLine();          
            if (type.equals("mobile")){            
                phoneNumber.setType(Person.PhoneType.MOBILE);      
            } else if (type.equals("home")) {            
                phoneNumber.setType(Person.PhoneType.HOME);          
            } else if (type.equals("work")) {           
                phoneNumber.setType(Person.PhoneType.WORK);          
            } else {            
                stdout.println("Unknown phone type.  Using default.");          
            }            
            person.addPhone(phoneNumber);        
        }          
        return person.build();      
    }        
    // Main function: Reads the entire address book from a file,  adds one person based on user input,   
    //then writes it back out to the same file.      
    public static void main(String[] args) throws Exception{        
        if (args.length != 1) {          
            System.err.println("Usage:  AddPerson ADDRESS_BOOK_FILE");          
            System.exit(-1);        
        }          
        AddressBook.Builder addressBook = AddressBook.newBuilder();          
        // Read the existing address book.        
        try {          
            addressBook.mergeFrom(new FileInputStream(args[0]));        
        } catch (FileNotFoundException e) {          
            System.out.println(args[0] + ": File not found.  Creating a new file.");        
        }          
        // Add an address.        
        addressBook.addPerson(          
            PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), System.out));          
        // Write the new address book back to disk.        
        FileOutputStream output = new FileOutputStream(args[0]);        
        addressBook.build().writeTo(output);       
        output.close();      
    }   
}  

7、执行AddPerson.java类的main方法,并输入参数“D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\file\addressbook”作为数据存储文件的位置。

根据提示,逐一输入 Person 对象的属性值:



最后,该类将Person对象数据写入到指定的PB的存储文件D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\file\addressbook中。文件格式为二进制:


8、编写 Java 类ListPeople.java,用于从存储文件中反序列化对象

ListPeople.java:

package study.java.arithmetic.protobuf;

import study.java.arithmetic.protobuf.AddressBookProtos.AddressBook;  
import study.java.arithmetic.protobuf.AddressBookProtos.Person;  
import java.io.FileInputStream;        
    public class ListPeople {      
        // Iterates though all people in the AddressBook and prints info about them.      
        static void Print(AddressBook addressBook) {        
            for (Person person: addressBook.getPersonList()) {          
                System.out.println("Person ID: " + person.getId());          
                System.out.println("  Name: " + person.getName());          
                if (person.hasEmail()) {            
                    System.out.println("  E-mail address: " + person.getEmail());          
                }            
                for (Person.PhoneNumber phoneNumber : person.getPhoneList()) {            
                    switch (phoneNumber.getType()) {              
                        case MOBILE:                
                            System.out.print("  Mobile phone #: ");                
                            break;              
                        case HOME:                
                            System.out.print("  Home phone #: ");                
                            break;              
                        case WORK:                
                            System.out.print("  Work phone #: ");                
                        break;            
                    }            
                    System.out.println(phoneNumber.getNumber());          
                }        
            }      
        }        
        // Main function:  Reads the entire address book from a file and prints all  the information inside.      
        /** 
         * @param args 
         * @throws Exception 
         */  
        public static void main(String[] args) throws Exception {        
            if (args.length != 1) {          
                System.err.println("Usage:  ListPeople ADDRESS_BOOK_FILE");          
                System.exit(-1);        
            }          
            // Read the existing address book.        
            AddressBook addressBook = AddressBook.parseFrom(new FileInputStream(args[0]));  
            Print(addressBook);      
        }    
}  

9、使用ListPeople.java 从存储文件D:\Study\ComputerStudy\BigData_Cloud\Protobuf\practise\file\addressbook中反序列化Person对象:





附-1:AddressBookProtos.java代码

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: proto/addressbook.proto

package study.java.arithmetic.protobuf;

public final class AddressBookProtos {
  private AddressBookProtos() {}
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
  }
  public interface PersonOrBuilder
      extends com.google.protobuf.MessageOrBuilder {

    // required string name = 1;
    /**
     * <code>required string name = 1;</code>
     */
    boolean hasName();
    /**
     * <code>required string name = 1;</code>
     */
    java.lang.String getName();
    /**
     * <code>required string name = 1;</code>
     */
    com.google.protobuf.ByteString
        getNameBytes();

    // required int32 id = 2;
    /**
     * <code>required int32 id = 2;</code>
     *
     * <pre>
     * Unique ID number for this person.  
     * </pre>
     */
    boolean hasId();
    /**
     * <code>required int32 id = 2;</code>
     *
     * <pre>
     * Unique ID number for this person.  
     * </pre>
     */
    int getId();

    // optional string email = 3;
    /**
     * <code>optional string email = 3;</code>
     */
    boolean hasEmail();
    /**
     * <code>optional string email = 3;</code>
     */
    java.lang.String getEmail();
    /**
     * <code>optional string email = 3;</code>
     */
    com.google.protobuf.ByteString
        getEmailBytes();

    // repeated .tutorial.Person.PhoneNumber phone = 4;
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    java.util.List<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber> 
        getPhoneList();
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber getPhone(int index);
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    int getPhoneCount();
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    java.util.List<? extends study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumberOrBuilder> 
        getPhoneOrBuilderList();
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumberOrBuilder getPhoneOrBuilder(
        int index);
  }
  /**
   * Protobuf type {@code tutorial.Person}
   */
  public static final class Person extends
      com.google.protobuf.GeneratedMessage
      implements PersonOrBuilder {
    // Use Person.newBuilder() to construct.
    private Person(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
      super(builder);
      this.unknownFields = builder.getUnknownFields();
    }
    private Person(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }

    private static final Person defaultInstance;
    public static Person getDefaultInstance() {
      return defaultInstance;
    }

    public Person getDefaultInstanceForType() {
      return defaultInstance;
    }

    private final com.google.protobuf.UnknownFieldSet unknownFields;
    @java.lang.Override
    public final com.google.protobuf.UnknownFieldSet
        getUnknownFields() {
      return this.unknownFields;
    }
    private Person(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      initFields();
      int mutable_bitField0_ = 0;
      com.google.protobuf.UnknownFieldSet.Builder unknownFields =
          com.google.protobuf.UnknownFieldSet.newBuilder();
      try {
        boolean done = false;
        while (!done) {
          int tag = input.readTag();
          switch (tag) {
            case 0:
              done = true;
              break;
            default: {
              if (!parseUnknownField(input, unknownFields,
                                     extensionRegistry, tag)) {
                done = true;
              }
              break;
            }
            case 10: {
              bitField0_ |= 0x00000001;
              name_ = input.readBytes();
              break;
            }
            case 16: {
              bitField0_ |= 0x00000002;
              id_ = input.readInt32();
              break;
            }
            case 26: {
              bitField0_ |= 0x00000004;
              email_ = input.readBytes();
              break;
            }
            case 34: {
              if (!((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
                phone_ = new java.util.ArrayList<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber>();
                mutable_bitField0_ |= 0x00000008;
              }
              phone_.add(input.readMessage(study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.PARSER, extensionRegistry));
              break;
            }
          }
        }
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw e.setUnfinishedMessage(this);
      } catch (java.io.IOException e) {
        throw new com.google.protobuf.InvalidProtocolBufferException(
            e.getMessage()).setUnfinishedMessage(this);
      } finally {
        if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
          phone_ = java.util.Collections.unmodifiableList(phone_);
        }
        this.unknownFields = unknownFields.build();
        makeExtensionsImmutable();
      }
    }
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_descriptor;
    }

    protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              study.java.arithmetic.protobuf.AddressBookProtos.Person.class, study.java.arithmetic.protobuf.AddressBookProtos.Person.Builder.class);
    }

    public static com.google.protobuf.Parser<Person> PARSER =
        new com.google.protobuf.AbstractParser<Person>() {
      public Person parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return new Person(input, extensionRegistry);
      }
    };

    @java.lang.Override
    public com.google.protobuf.Parser<Person> getParserForType() {
      return PARSER;
    }

    /**
     * Protobuf enum {@code tutorial.Person.PhoneType}
     */
    public enum PhoneType
        implements com.google.protobuf.ProtocolMessageEnum {
      /**
       * <code>MOBILE = 0;</code>
       */
      MOBILE(0, 0),
      /**
       * <code>HOME = 1;</code>
       */
      HOME(1, 1),
      /**
       * <code>WORK = 2;</code>
       */
      WORK(2, 2),
      ;

      /**
       * <code>MOBILE = 0;</code>
       */
      public static final int MOBILE_VALUE = 0;
      /**
       * <code>HOME = 1;</code>
       */
      public static final int HOME_VALUE = 1;
      /**
       * <code>WORK = 2;</code>
       */
      public static final int WORK_VALUE = 2;


      public final int getNumber() { return value; }

      public static PhoneType valueOf(int value) {
        switch (value) {
          case 0: return MOBILE;
          case 1: return HOME;
          case 2: return WORK;
          default: return null;
        }
      }

      public static com.google.protobuf.Internal.EnumLiteMap<PhoneType>
          internalGetValueMap() {
        return internalValueMap;
      }
      private static com.google.protobuf.Internal.EnumLiteMap<PhoneType>
          internalValueMap =
            new com.google.protobuf.Internal.EnumLiteMap<PhoneType>() {
              public PhoneType findValueByNumber(int number) {
                return PhoneType.valueOf(number);
              }
            };

      public final com.google.protobuf.Descriptors.EnumValueDescriptor
          getValueDescriptor() {
        return getDescriptor().getValues().get(index);
      }
      public final com.google.protobuf.Descriptors.EnumDescriptor
          getDescriptorForType() {
        return getDescriptor();
      }
      public static final com.google.protobuf.Descriptors.EnumDescriptor
          getDescriptor() {
        return study.java.arithmetic.protobuf.AddressBookProtos.Person.getDescriptor().getEnumTypes().get(0);
      }

      private static final PhoneType[] VALUES = values();

      public static PhoneType valueOf(
          com.google.protobuf.Descriptors.EnumValueDescriptor desc) {
        if (desc.getType() != getDescriptor()) {
          throw new java.lang.IllegalArgumentException(
            "EnumValueDescriptor is not for this type.");
        }
        return VALUES[desc.getIndex()];
      }

      private final int index;
      private final int value;

      private PhoneType(int index, int value) {
        this.index = index;
        this.value = value;
      }

      // @@protoc_insertion_point(enum_scope:tutorial.Person.PhoneType)
    }

    public interface PhoneNumberOrBuilder
        extends com.google.protobuf.MessageOrBuilder {

      // required string number = 1;
      /**
       * <code>required string number = 1;</code>
       */
      boolean hasNumber();
      /**
       * <code>required string number = 1;</code>
       */
      java.lang.String getNumber();
      /**
       * <code>required string number = 1;</code>
       */
      com.google.protobuf.ByteString
          getNumberBytes();

      // optional .tutorial.Person.PhoneType type = 2 [default = HOME];
      /**
       * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
       */
      boolean hasType();
      /**
       * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
       */
      study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType getType();
    }
    /**
     * Protobuf type {@code tutorial.Person.PhoneNumber}
     */
    public static final class PhoneNumber extends
        com.google.protobuf.GeneratedMessage
        implements PhoneNumberOrBuilder {
      // Use PhoneNumber.newBuilder() to construct.
      private PhoneNumber(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
        super(builder);
        this.unknownFields = builder.getUnknownFields();
      }
      private PhoneNumber(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }

      private static final PhoneNumber defaultInstance;
      public static PhoneNumber getDefaultInstance() {
        return defaultInstance;
      }

      public PhoneNumber getDefaultInstanceForType() {
        return defaultInstance;
      }

      private final com.google.protobuf.UnknownFieldSet unknownFields;
      @java.lang.Override
      public final com.google.protobuf.UnknownFieldSet
          getUnknownFields() {
        return this.unknownFields;
      }
      private PhoneNumber(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        initFields();
        int mutable_bitField0_ = 0;
        com.google.protobuf.UnknownFieldSet.Builder unknownFields =
            com.google.protobuf.UnknownFieldSet.newBuilder();
        try {
          boolean done = false;
          while (!done) {
            int tag = input.readTag();
            switch (tag) {
              case 0:
                done = true;
                break;
              default: {
                if (!parseUnknownField(input, unknownFields,
                                       extensionRegistry, tag)) {
                  done = true;
                }
                break;
              }
              case 10: {
                bitField0_ |= 0x00000001;
                number_ = input.readBytes();
                break;
              }
              case 16: {
                int rawValue = input.readEnum();
                study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType value = study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType.valueOf(rawValue);
                if (value == null) {
                  unknownFields.mergeVarintField(2, rawValue);
                } else {
                  bitField0_ |= 0x00000002;
                  type_ = value;
                }
                break;
              }
            }
          }
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          throw e.setUnfinishedMessage(this);
        } catch (java.io.IOException e) {
          throw new com.google.protobuf.InvalidProtocolBufferException(
              e.getMessage()).setUnfinishedMessage(this);
        } finally {
          this.unknownFields = unknownFields.build();
          makeExtensionsImmutable();
        }
      }
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_PhoneNumber_descriptor;
      }

      protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_PhoneNumber_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.class, study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.Builder.class);
      }

      public static com.google.protobuf.Parser<PhoneNumber> PARSER =
          new com.google.protobuf.AbstractParser<PhoneNumber>() {
        public PhoneNumber parsePartialFrom(
            com.google.protobuf.CodedInputStream input,
            com.google.protobuf.ExtensionRegistryLite extensionRegistry)
            throws com.google.protobuf.InvalidProtocolBufferException {
          return new PhoneNumber(input, extensionRegistry);
        }
      };

      @java.lang.Override
      public com.google.protobuf.Parser<PhoneNumber> getParserForType() {
        return PARSER;
      }

      private int bitField0_;
      // required string number = 1;
      public static final int NUMBER_FIELD_NUMBER = 1;
      private java.lang.Object number_;
      /**
       * <code>required string number = 1;</code>
       */
      public boolean hasNumber() {
        return ((bitField0_ & 0x00000001) == 0x00000001);
      }
      /**
       * <code>required string number = 1;</code>
       */
      public java.lang.String getNumber() {
        java.lang.Object ref = number_;
        if (ref instanceof java.lang.String) {
          return (java.lang.String) ref;
        } else {
          com.google.protobuf.ByteString bs = 
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          if (bs.isValidUtf8()) {
            number_ = s;
          }
          return s;
        }
      }
      /**
       * <code>required string number = 1;</code>
       */
      public com.google.protobuf.ByteString
          getNumberBytes() {
        java.lang.Object ref = number_;
        if (ref instanceof java.lang.String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          number_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }

      // optional .tutorial.Person.PhoneType type = 2 [default = HOME];
      public static final int TYPE_FIELD_NUMBER = 2;
      private study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType type_;
      /**
       * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
       */
      public boolean hasType() {
        return ((bitField0_ & 0x00000002) == 0x00000002);
      }
      /**
       * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
       */
      public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType getType() {
        return type_;
      }

      private void initFields() {
        number_ = "";
        type_ = study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType.HOME;
      }
      private byte memoizedIsInitialized = -1;
      public final boolean isInitialized() {
        byte isInitialized = memoizedIsInitialized;
        if (isInitialized != -1) return isInitialized == 1;

        if (!hasNumber()) {
          memoizedIsInitialized = 0;
          return false;
        }
        memoizedIsInitialized = 1;
        return true;
      }

      public void writeTo(com.google.protobuf.CodedOutputStream output)
                          throws java.io.IOException {
        getSerializedSize();
        if (((bitField0_ & 0x00000001) == 0x00000001)) {
          output.writeBytes(1, getNumberBytes());
        }
        if (((bitField0_ & 0x00000002) == 0x00000002)) {
          output.writeEnum(2, type_.getNumber());
        }
        getUnknownFields().writeTo(output);
      }

      private int memoizedSerializedSize = -1;
      public int getSerializedSize() {
        int size = memoizedSerializedSize;
        if (size != -1) return size;

        size = 0;
        if (((bitField0_ & 0x00000001) == 0x00000001)) {
          size += com.google.protobuf.CodedOutputStream
            .computeBytesSize(1, getNumberBytes());
        }
        if (((bitField0_ & 0x00000002) == 0x00000002)) {
          size += com.google.protobuf.CodedOutputStream
            .computeEnumSize(2, type_.getNumber());
        }
        size += getUnknownFields().getSerializedSize();
        memoizedSerializedSize = size;
        return size;
      }

      private static final long serialVersionUID = 0L;
      @java.lang.Override
      protected java.lang.Object writeReplace()
          throws java.io.ObjectStreamException {
        return super.writeReplace();
      }

      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          com.google.protobuf.ByteString data)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          com.google.protobuf.ByteString data,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data, extensionRegistry);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(byte[] data)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          byte[] data,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        return PARSER.parseFrom(data, extensionRegistry);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(java.io.InputStream input)
          throws java.io.IOException {
        return PARSER.parseFrom(input);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          java.io.InputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return PARSER.parseFrom(input, extensionRegistry);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseDelimitedFrom(java.io.InputStream input)
          throws java.io.IOException {
        return PARSER.parseDelimitedFrom(input);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseDelimitedFrom(
          java.io.InputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return PARSER.parseDelimitedFrom(input, extensionRegistry);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          com.google.protobuf.CodedInputStream input)
          throws java.io.IOException {
        return PARSER.parseFrom(input);
      }
      public static study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parseFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        return PARSER.parseFrom(input, extensionRegistry);
      }

      public static Builder newBuilder() { return Builder.create(); }
      public Builder newBuilderForType() { return newBuilder(); }
      public static Builder newBuilder(study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber prototype) {
        return newBuilder().mergeFrom(prototype);
      }
      public Builder toBuilder() { return newBuilder(this); }

      @java.lang.Override
      protected Builder newBuilderForType(
          com.google.protobuf.GeneratedMessage.BuilderParent parent) {
        Builder builder = new Builder(parent);
        return builder;
      }
      /**
       * Protobuf type {@code tutorial.Person.PhoneNumber}
       */
      public static final class Builder extends
          com.google.protobuf.GeneratedMessage.Builder<Builder>
         implements study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumberOrBuilder {
        public static final com.google.protobuf.Descriptors.Descriptor
            getDescriptor() {
          return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_PhoneNumber_descriptor;
        }

        protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
            internalGetFieldAccessorTable() {
          return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_PhoneNumber_fieldAccessorTable
              .ensureFieldAccessorsInitialized(
                  study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.class, study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.Builder.class);
        }

        // Construct using study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.newBuilder()
        private Builder() {
          maybeForceBuilderInitialization();
        }

        private Builder(
            com.google.protobuf.GeneratedMessage.BuilderParent parent) {
          super(parent);
          maybeForceBuilderInitialization();
        }
        private void maybeForceBuilderInitialization() {
          if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
          }
        }
        private static Builder create() {
          return new Builder();
        }

        public Builder clear() {
          super.clear();
          number_ = "";
          bitField0_ = (bitField0_ & ~0x00000001);
          type_ = study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType.HOME;
          bitField0_ = (bitField0_ & ~0x00000002);
          return this;
        }

        public Builder clone() {
          return create().mergeFrom(buildPartial());
        }

        public com.google.protobuf.Descriptors.Descriptor
            getDescriptorForType() {
          return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_PhoneNumber_descriptor;
        }

        public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber getDefaultInstanceForType() {
          return study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.getDefaultInstance();
        }

        public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber build() {
          study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber result = buildPartial();
          if (!result.isInitialized()) {
            throw newUninitializedMessageException(result);
          }
          return result;
        }

        public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber buildPartial() {
          study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber result = new study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber(this);
          int from_bitField0_ = bitField0_;
          int to_bitField0_ = 0;
          if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
            to_bitField0_ |= 0x00000001;
          }
          result.number_ = number_;
          if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
            to_bitField0_ |= 0x00000002;
          }
          result.type_ = type_;
          result.bitField0_ = to_bitField0_;
          onBuilt();
          return result;
        }

        public Builder mergeFrom(com.google.protobuf.Message other) {
          if (other instanceof study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber) {
            return mergeFrom((study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber)other);
          } else {
            super.mergeFrom(other);
            return this;
          }
        }

        public Builder mergeFrom(study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber other) {
          if (other == study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber.getDefaultInstance()) return this;
          if (other.hasNumber()) {
            bitField0_ |= 0x00000001;
            number_ = other.number_;
            onChanged();
          }
          if (other.hasType()) {
            setType(other.getType());
          }
          this.mergeUnknownFields(other.getUnknownFields());
          return this;
        }

        public final boolean isInitialized() {
          if (!hasNumber()) {
            
            return false;
          }
          return true;
        }

        public Builder mergeFrom(
            com.google.protobuf.CodedInputStream input,
            com.google.protobuf.ExtensionRegistryLite extensionRegistry)
            throws java.io.IOException {
          study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber parsedMessage = null;
          try {
            parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
          } catch (com.google.protobuf.InvalidProtocolBufferException e) {
            parsedMessage = (study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber) e.getUnfinishedMessage();
            throw e;
          } finally {
            if (parsedMessage != null) {
              mergeFrom(parsedMessage);
            }
          }
          return this;
        }
        private int bitField0_;

        // required string number = 1;
        private java.lang.Object number_ = "";
        /**
         * <code>required string number = 1;</code>
         */
        public boolean hasNumber() {
          return ((bitField0_ & 0x00000001) == 0x00000001);
        }
        /**
         * <code>required string number = 1;</code>
         */
        public java.lang.String getNumber() {
          java.lang.Object ref = number_;
          if (!(ref instanceof java.lang.String)) {
            java.lang.String s = ((com.google.protobuf.ByteString) ref)
                .toStringUtf8();
            number_ = s;
            return s;
          } else {
            return (java.lang.String) ref;
          }
        }
        /**
         * <code>required string number = 1;</code>
         */
        public com.google.protobuf.ByteString
            getNumberBytes() {
          java.lang.Object ref = number_;
          if (ref instanceof String) {
            com.google.protobuf.ByteString b = 
                com.google.protobuf.ByteString.copyFromUtf8(
                    (java.lang.String) ref);
            number_ = b;
            return b;
          } else {
            return (com.google.protobuf.ByteString) ref;
          }
        }
        /**
         * <code>required string number = 1;</code>
         */
        public Builder setNumber(
            java.lang.String value) {
          if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000001;
          number_ = value;
          onChanged();
          return this;
        }
        /**
         * <code>required string number = 1;</code>
         */
        public Builder clearNumber() {
          bitField0_ = (bitField0_ & ~0x00000001);
          number_ = getDefaultInstance().getNumber();
          onChanged();
          return this;
        }
        /**
         * <code>required string number = 1;</code>
         */
        public Builder setNumberBytes(
            com.google.protobuf.ByteString value) {
          if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000001;
          number_ = value;
          onChanged();
          return this;
        }

        // optional .tutorial.Person.PhoneType type = 2 [default = HOME];
        private study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType type_ = study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType.HOME;
        /**
         * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
         */
        public boolean hasType() {
          return ((bitField0_ & 0x00000002) == 0x00000002);
        }
        /**
         * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
         */
        public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType getType() {
          return type_;
        }
        /**
         * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
         */
        public Builder setType(study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType value) {
          if (value == null) {
            throw new NullPointerException();
          }
          bitField0_ |= 0x00000002;
          type_ = value;
          onChanged();
          return this;
        }
        /**
         * <code>optional .tutorial.Person.PhoneType type = 2 [default = HOME];</code>
         */
        public Builder clearType() {
          bitField0_ = (bitField0_ & ~0x00000002);
          type_ = study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneType.HOME;
          onChanged();
          return this;
        }

        // @@protoc_insertion_point(builder_scope:tutorial.Person.PhoneNumber)
      }

      static {
        defaultInstance = new PhoneNumber(true);
        defaultInstance.initFields();
      }

      // @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
    }

    private int bitField0_;
    // required string name = 1;
    public static final int NAME_FIELD_NUMBER = 1;
    private java.lang.Object name_;
    /**
     * <code>required string name = 1;</code>
     */
    public boolean hasName() {
      return ((bitField0_ & 0x00000001) == 0x00000001);
    }
    /**
     * <code>required string name = 1;</code>
     */
    public java.lang.String getName() {
      java.lang.Object ref = name_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          name_ = s;
        }
        return s;
      }
    }
    /**
     * <code>required string name = 1;</code>
     */
    public com.google.protobuf.ByteString
        getNameBytes() {
      java.lang.Object ref = name_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        name_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    // required int32 id = 2;
    public static final int ID_FIELD_NUMBER = 2;
    private int id_;
    /**
     * <code>required int32 id = 2;</code>
     *
     * <pre>
     * Unique ID number for this person.  
     * </pre>
     */
    public boolean hasId() {
      return ((bitField0_ & 0x00000002) == 0x00000002);
    }
    /**
     * <code>required int32 id = 2;</code>
     *
     * <pre>
     * Unique ID number for this person.  
     * </pre>
     */
    public int getId() {
      return id_;
    }

    // optional string email = 3;
    public static final int EMAIL_FIELD_NUMBER = 3;
    private java.lang.Object email_;
    /**
     * <code>optional string email = 3;</code>
     */
    public boolean hasEmail() {
      return ((bitField0_ & 0x00000004) == 0x00000004);
    }
    /**
     * <code>optional string email = 3;</code>
     */
    public java.lang.String getEmail() {
      java.lang.Object ref = email_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        if (bs.isValidUtf8()) {
          email_ = s;
        }
        return s;
      }
    }
    /**
     * <code>optional string email = 3;</code>
     */
    public com.google.protobuf.ByteString
        getEmailBytes() {
      java.lang.Object ref = email_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        email_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    // repeated .tutorial.Person.PhoneNumber phone = 4;
    public static final int PHONE_FIELD_NUMBER = 4;
    private java.util.List<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber> phone_;
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    public java.util.List<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber> getPhoneList() {
      return phone_;
    }
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    public java.util.List<? extends study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumberOrBuilder> 
        getPhoneOrBuilderList() {
      return phone_;
    }
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    public int getPhoneCount() {
      return phone_.size();
    }
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber getPhone(int index) {
      return phone_.get(index);
    }
    /**
     * <code>repeated .tutorial.Person.PhoneNumber phone = 4;</code>
     */
    public study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumberOrBuilder getPhoneOrBuilder(
        int index) {
      return phone_.get(index);
    }

    private void initFields() {
      name_ = "";
      id_ = 0;
      email_ = "";
      phone_ = java.util.Collections.emptyList();
    }
    private byte memoizedIsInitialized = -1;
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized != -1) return isInitialized == 1;

      if (!hasName()) {
        memoizedIsInitialized = 0;
        return false;
      }
      if (!hasId()) {
        memoizedIsInitialized = 0;
        return false;
      }
      for (int i = 0; i < getPhoneCount(); i++) {
        if (!getPhone(i).isInitialized()) {
          memoizedIsInitialized = 0;
          return false;
        }
      }
      memoizedIsInitialized = 1;
      return true;
    }

    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      getSerializedSize();
      if (((bitField0_ & 0x00000001) == 0x00000001)) {
        output.writeBytes(1, getNameBytes());
      }
      if (((bitField0_ & 0x00000002) == 0x00000002)) {
        output.writeInt32(2, id_);
      }
      if (((bitField0_ & 0x00000004) == 0x00000004)) {
        output.writeBytes(3, getEmailBytes());
      }
      for (int i = 0; i < phone_.size(); i++) {
        output.writeMessage(4, phone_.get(i));
      }
      getUnknownFields().writeTo(output);
    }

    private int memoizedSerializedSize = -1;
    public int getSerializedSize() {
      int size = memoizedSerializedSize;
      if (size != -1) return size;

      size = 0;
      if (((bitField0_ & 0x00000001) == 0x00000001)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(1, getNameBytes());
      }
      if (((bitField0_ & 0x00000002) == 0x00000002)) {
        size += com.google.protobuf.CodedOutputStream
          .computeInt32Size(2, id_);
      }
      if (((bitField0_ & 0x00000004) == 0x00000004)) {
        size += com.google.protobuf.CodedOutputStream
          .computeBytesSize(3, getEmailBytes());
      }
      for (int i = 0; i < phone_.size(); i++) {
        size += com.google.protobuf.CodedOutputStream
          .computeMessageSize(4, phone_.get(i));
      }
      size += getUnknownFields().getSerializedSize();
      memoizedSerializedSize = size;
      return size;
    }

    private static final long serialVersionUID = 0L;
    @java.lang.Override
    protected java.lang.Object writeReplace()
        throws java.io.ObjectStreamException {
      return super.writeReplace();
    }

    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input, extensionRegistry);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static study.java.arithmetic.protobuf.AddressBookProtos.Person parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }

    public static Builder newBuilder() { return Builder.create(); }
    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder(study.java.arithmetic.protobuf.AddressBookProtos.Person prototype) {
      return newBuilder().mergeFrom(prototype);
    }
    public Builder toBuilder() { return newBuilder(this); }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessage.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code tutorial.Person}
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessage.Builder<Builder>
       implements study.java.arithmetic.protobuf.AddressBookProtos.PersonOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_descriptor;
      }

      protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                study.java.arithmetic.protobuf.AddressBookProtos.Person.class, study.java.arithmetic.protobuf.AddressBookProtos.Person.Builder.class);
      }

      // Construct using study.java.arithmetic.protobuf.AddressBookProtos.Person.newBuilder()
      private Builder() {
        maybeForceBuilderInitialization();
      }

      private Builder(
          com.google.protobuf.GeneratedMessage.BuilderParent parent) {
        super(parent);
        maybeForceBuilderInitialization();
      }
      private void maybeForceBuilderInitialization() {
        if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
          getPhoneFieldBuilder();
        }
      }
      private static Builder create() {
        return new Builder();
      }

      public Builder clear() {
        super.clear();
        name_ = "";
        bitField0_ = (bitField0_ & ~0x00000001);
        id_ = 0;
        bitField0_ = (bitField0_ & ~0x00000002);
        email_ = "";
        bitField0_ = (bitField0_ & ~0x00000004);
        if (phoneBuilder_ == null) {
          phone_ = java.util.Collections.emptyList();
          bitField0_ = (bitField0_ & ~0x00000008);
        } else {
          phoneBuilder_.clear();
        }
        return this;
      }

      public Builder clone() {
        return create().mergeFrom(buildPartial());
      }

      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return study.java.arithmetic.protobuf.AddressBookProtos.internal_static_tutorial_Person_descriptor;
      }

      public study.java.arithmetic.protobuf.AddressBookProtos.Person getDefaultInstanceForType() {
        return study.java.arithmetic.protobuf.AddressBookProtos.Person.getDefaultInstance();
      }

      public study.java.arithmetic.protobuf.AddressBookProtos.Person build() {
        study.java.arithmetic.protobuf.AddressBookProtos.Person result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      public study.java.arithmetic.protobuf.AddressBookProtos.Person buildPartial() {
        study.java.arithmetic.protobuf.AddressBookProtos.Person result = new study.java.arithmetic.protobuf.AddressBookProtos.Person(this);
        int from_bitField0_ = bitField0_;
        int to_bitField0_ = 0;
        if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
          to_bitField0_ |= 0x00000001;
        }
        result.name_ = name_;
        if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
          to_bitField0_ |= 0x00000002;
        }
        result.id_ = id_;
        if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
          to_bitField0_ |= 0x00000004;
        }
        result.email_ = email_;
        if (phoneBuilder_ == null) {
          if (((bitField0_ & 0x00000008) == 0x00000008)) {
            phone_ = java.util.Collections.unmodifiableList(phone_);
            bitField0_ = (bitField0_ & ~0x00000008);
          }
          result.phone_ = phone_;
        } else {
          result.phone_ = phoneBuilder_.build();
        }
        result.bitField0_ = to_bitField0_;
        onBuilt();
        return result;
      }

      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof study.java.arithmetic.protobuf.AddressBookProtos.Person) {
          return mergeFrom((study.java.arithmetic.protobuf.AddressBookProtos.Person)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(study.java.arithmetic.protobuf.AddressBookProtos.Person other) {
        if (other == study.java.arithmetic.protobuf.AddressBookProtos.Person.getDefaultInstance()) return this;
        if (other.hasName()) {
          bitField0_ |= 0x00000001;
          name_ = other.name_;
          onChanged();
        }
        if (other.hasId()) {
          setId(other.getId());
        }
        if (other.hasEmail()) {
          bitField0_ |= 0x00000004;
          email_ = other.email_;
          onChanged();
        }
        if (phoneBuilder_ == null) {
          if (!other.phone_.isEmpty()) {
            if (phone_.isEmpty()) {
              phone_ = other.phone_;
              bitField0_ = (bitField0_ & ~0x00000008);
            } else {
              ensurePhoneIsMutable();
              phone_.addAll(other.phone_);
            }
            onChanged();
          }
        } else {
          if (!other.phone_.isEmpty()) {
            if (phoneBuilder_.isEmpty()) {
              phoneBuilder_.dispose();
              phoneBuilder_ = null;
              phone_ = other.phone_;
              bitField0_ = (bitField0_ & ~0x00000008);
              phoneBuilder_ = 
                com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders ?
                   getPhoneFieldBuilder() : null;
            } else {
              phoneBuilder_.addAllMessages(other.phone_);
            }
          }
        }
        this.mergeUnknownFields(other.getUnknownFields());
        return this;
      }

      public final boolean isInitialized() {
        if (!hasName()) {
          
          return false;
        }
        if (!hasId()) {
          
          return false;
        }
        for (int i = 0; i < getPhoneCount(); i++) {
          if (!getPhone(i).isInitialized()) {
            
            return false;
          }
        }
        return true;
      }

      public Builder mergeFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        study.java.arithmetic.protobuf.AddressBookProtos.Person parsedMessage = null;
        try {
          parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          parsedMessage = (study.java.arithmetic.protobuf.AddressBookProtos.Person) e.getUnfinishedMessage();
          throw e;
        } finally {
          if (parsedMessage != null) {
            mergeFrom(parsedMessage);
          }
        }
        return this;
      }
      private int bitField0_;

      // required string name = 1;
      private java.lang.Object name_ = "";
      /**
       * <code>required string name = 1;</code>
       */
      public boolean hasName() {
        return ((bitField0_ & 0x00000001) == 0x00000001);
      }
      /**
       * <code>required string name = 1;</code>
       */
      public java.lang.String getName() {
        java.lang.Object ref = name_;
        if (!(ref instanceof java.lang.String)) {
          java.lang.String s = ((com.google.protobuf.ByteString) ref)
              .toStringUtf8();
          name_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>required string name = 1;</code>
       */
      public com.google.protobuf.ByteString
          getNameBytes() {
        java.lang.Object ref = name_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          name_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>required string name = 1;</code>
       */
      public Builder setName(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000001;
        name_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>required string name = 1;</code>
       */
      public Builder clearName() {
        bitField0_ = (bitField0_ & ~0x00000001);
        name_ = getDefaultInstance().getName();
        onChanged();
        return this;
      }
      /**
       * <code>required string name = 1;</code>
       */
      public Builder setNameBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000001;
        name_ = value;
        onChanged();
        return this;
      }

      // required int32 id = 2;
      private int id_ ;
      /**
       * <code>required int32 id = 2;</code>
       *
       * <pre>
       * Unique ID number for this person.  
       * </pre>
       */
      public boolean hasId() {
        return ((bitField0_ & 0x00000002) == 0x00000002);
      }
      /**
       * <code>required int32 id = 2;</code>
       *
       * <pre>
       * Unique ID number for this person.  
       * </pre>
       */
      public int getId() {
        return id_;
      }
      /**
       * <code>required int32 id = 2;</code>
       *
       * <pre>
       * Unique ID number for this person.  
       * </pre>
       */
      public Builder setId(int value) {
        bitField0_ |= 0x00000002;
        id_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>required int32 id = 2;</code>
       *
       * <pre>
       * Unique ID number for this person.  
       * </pre>
       */
      public Builder clearId() {
        bitField0_ = (bitField0_ & ~0x00000002);
        id_ = 0;
        onChanged();
        return this;
      }

      // optional string email = 3;
      private java.lang.Object email_ = "";
      /**
       * <code>optional string email = 3;</code>
       */
      public boolean hasEmail() {
        return ((bitField0_ & 0x00000004) == 0x00000004);
      }
      /**
       * <code>optional string email = 3;</code>
       */
      public java.lang.String getEmail() {
        java.lang.Object ref = email_;
        if (!(ref instanceof java.lang.String)) {
          java.lang.String s = ((com.google.protobuf.ByteString) ref)
              .toStringUtf8();
          email_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string email = 3;</code>
       */
      public com.google.protobuf.ByteString
          getEmailBytes() {
        java.lang.Object ref = email_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          email_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string email = 3;</code>
       */
      public Builder setEmail(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000004;
        email_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string email = 3;</code>
       */
      public Builder clearEmail() {
        bitField0_ = (bitField0_ & ~0x00000004);
        email_ = getDefaultInstance().getEmail();
        onChanged();
        return this;
      }
      /**
       * <code>optional string email = 3;</code>
       */
      public Builder setEmailBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  bitField0_ |= 0x00000004;
        email_ = value;
        onChanged();
        return this;
      }

      // repeated .tutorial.Person.PhoneNumber phone = 4;
      private java.util.List<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber> phone_ =
        java.util.Collections.emptyList();
      private void ensurePhoneIsMutable() {
        if (!((bitField0_ & 0x00000008) == 0x00000008)) {
          phone_ = new java.util.ArrayList<study.java.arithmetic.protobuf.AddressBookProtos.Person.PhoneNumber>(phone_);
          bitField0_ |= 0x00000008;
         }
      }

      private com.google.protobuf.RepeatedFieldBuilder<
          study.
                        
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/samhacker/article/details/41949845
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-02 18:26:05
  • 阅读 ( 825 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢