diff --git a/spring-protobuf/pom.xml b/spring-protobuf/pom.xml new file mode 100644 index 0000000000..28338f26c3 --- /dev/null +++ b/spring-protobuf/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + com.baeldung + spring-protobuf + 0.1-SNAPSHOT + spring-protobuf + + + org.springframework.boot + spring-boot-starter-parent + 1.2.4.RELEASE + + + + + com.google.protobuf + protobuf-java + 3.0.0-beta-3 + + + com.googlecode.protobuf-java-format + protobuf-java-format + 1.4 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + diff --git a/spring-protobuf/src/main/java/com/baeldung/protobuf/Application.java b/spring-protobuf/src/main/java/com/baeldung/protobuf/Application.java new file mode 100644 index 0000000000..38a49367a3 --- /dev/null +++ b/spring-protobuf/src/main/java/com/baeldung/protobuf/Application.java @@ -0,0 +1,68 @@ +package com.baeldung.protobuf; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.protobuf.BaeldungTraining.Course; +import com.baeldung.protobuf.BaeldungTraining.Student; +import com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber; +import com.baeldung.protobuf.BaeldungTraining.Student.PhoneType; + +@SpringBootApplication +public class Application { + @Bean + RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) { + return new RestTemplate(Arrays.asList(hmc)); + } + + @Bean + ProtobufHttpMessageConverter protobufHttpMessageConverter() { + return new ProtobufHttpMessageConverter(); + } + + @Bean + public CourseRepository createDummyCourses() { + Map dummy = new HashMap<>(); + + Course course1 = Course.newBuilder().setId(1).setCourseName("REST with Spring").addAllStudent(createDummyStudents()).build(); + Course course2 = Course.newBuilder().setId(2).setCourseName("Learn Spring Security").addAllStudent(new ArrayList()).build(); + + dummy.put(course1.getId(), course1); + dummy.put(course2.getId(), course2); + + return new CourseRepository(dummy); + } + + private List createDummyStudents() { + List studentList = new ArrayList<>(); + + PhoneNumber phone1 = createPhone("123456", PhoneType.MOBILE); + Student student1 = createStudent(1, "John", "Doe", "john.doe@baeldung.com", Arrays.asList(phone1)); + + PhoneNumber phone2 = createPhone("234567", PhoneType.LANDLINE); + Student student2 = createStudent(2, "Richard", "Roe", "richard.roe@baeldung.com", Arrays.asList(phone2)); + + PhoneNumber phone3_1 = createPhone("345678", PhoneType.MOBILE); + PhoneNumber phone3_2 = createPhone("456789", PhoneType.LANDLINE); + Student student3 = createStudent(3, "Jane", "Doe", "jane.doe@baeldung.com", Arrays.asList(phone3_1, phone3_2)); + + studentList.addAll(Arrays.asList(student1, student2, student3)); + return studentList; + } + + private Student createStudent(int id, String firstName, String lastName, String email, List phones) { + return Student.newBuilder().setId(id).setFirstName(firstName).setLastName(lastName).setEmail(email).addAllPhone(phones).build(); + } + + private PhoneNumber createPhone(String number, PhoneType type) { + return PhoneNumber.newBuilder().setNumber(number).setType(type).build(); + } +} \ No newline at end of file diff --git a/spring-protobuf/src/main/java/com/baeldung/protobuf/BaeldungTraining.java b/spring-protobuf/src/main/java/com/baeldung/protobuf/BaeldungTraining.java new file mode 100644 index 0000000000..3869ebfaeb --- /dev/null +++ b/spring-protobuf/src/main/java/com/baeldung/protobuf/BaeldungTraining.java @@ -0,0 +1,2719 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: resources/baeldung.proto + +package com.baeldung.protobuf; + +public final class BaeldungTraining { + private BaeldungTraining() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + } + public interface CourseOrBuilder extends + // @@protoc_insertion_point(interface_extends:baeldung.Course) + com.google.protobuf.MessageOrBuilder { + + /** + * optional int32 id = 1; + */ + int getId(); + + /** + * optional string course_name = 2; + */ + java.lang.String getCourseName(); + /** + * optional string course_name = 2; + */ + com.google.protobuf.ByteString + getCourseNameBytes(); + + /** + * repeated .baeldung.Student student = 3; + */ + java.util.List + getStudentList(); + /** + * repeated .baeldung.Student student = 3; + */ + com.baeldung.protobuf.BaeldungTraining.Student getStudent(int index); + /** + * repeated .baeldung.Student student = 3; + */ + int getStudentCount(); + /** + * repeated .baeldung.Student student = 3; + */ + java.util.List + getStudentOrBuilderList(); + /** + * repeated .baeldung.Student student = 3; + */ + com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder getStudentOrBuilder( + int index); + } + /** + * Protobuf type {@code baeldung.Course} + */ + public static final class Course extends + com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:baeldung.Course) + CourseOrBuilder { + // Use Course.newBuilder() to construct. + private Course(com.google.protobuf.GeneratedMessage.Builder builder) { + super(builder); + } + private Course() { + id_ = 0; + courseName_ = ""; + student_ = java.util.Collections.emptyList(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); + } + private Course( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!input.skipField(tag)) { + done = true; + } + break; + } + case 8: { + + id_ = input.readInt32(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + courseName_ = s; + break; + } + case 26: { + if (!((mutable_bitField0_ & 0x00000004) == 0x00000004)) { + student_ = new java.util.ArrayList(); + mutable_bitField0_ |= 0x00000004; + } + student_.add(input.readMessage(com.baeldung.protobuf.BaeldungTraining.Student.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).setUnfinishedMessage(this); + } finally { + if (((mutable_bitField0_ & 0x00000004) == 0x00000004)) { + student_ = java.util.Collections.unmodifiableList(student_); + } + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Course_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Course_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Course.class, com.baeldung.protobuf.BaeldungTraining.Course.Builder.class); + } + + private int bitField0_; + public static final int ID_FIELD_NUMBER = 1; + private int id_; + /** + * optional int32 id = 1; + */ + public int getId() { + return id_; + } + + public static final int COURSE_NAME_FIELD_NUMBER = 2; + private volatile java.lang.Object courseName_; + /** + * optional string course_name = 2; + */ + public java.lang.String getCourseName() { + java.lang.Object ref = courseName_; + 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(); + courseName_ = s; + return s; + } + } + /** + * optional string course_name = 2; + */ + public com.google.protobuf.ByteString + getCourseNameBytes() { + java.lang.Object ref = courseName_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + courseName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int STUDENT_FIELD_NUMBER = 3; + private java.util.List student_; + /** + * repeated .baeldung.Student student = 3; + */ + public java.util.List getStudentList() { + return student_; + } + /** + * repeated .baeldung.Student student = 3; + */ + public java.util.List + getStudentOrBuilderList() { + return student_; + } + /** + * repeated .baeldung.Student student = 3; + */ + public int getStudentCount() { + return student_.size(); + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.Student getStudent(int index) { + return student_.get(index); + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder getStudentOrBuilder( + int index) { + return student_.get(index); + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (id_ != 0) { + output.writeInt32(1, id_); + } + if (!getCourseNameBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 2, courseName_); + } + for (int i = 0; i < student_.size(); i++) { + output.writeMessage(3, student_.get(i)); + } + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, id_); + } + if (!getCourseNameBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(2, courseName_); + } + for (int i = 0; i < student_.size(); i++) { + size += com.google.protobuf.CodedOutputStream + .computeMessageSize(3, student_.get(i)); + } + memoizedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Course parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.baeldung.protobuf.BaeldungTraining.Course prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code baeldung.Course} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder implements + // @@protoc_insertion_point(builder_implements:baeldung.Course) + com.baeldung.protobuf.BaeldungTraining.CourseOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Course_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Course_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Course.class, com.baeldung.protobuf.BaeldungTraining.Course.Builder.class); + } + + // Construct using com.baeldung.protobuf.BaeldungTraining.Course.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + getStudentFieldBuilder(); + } + } + public Builder clear() { + super.clear(); + id_ = 0; + + courseName_ = ""; + + if (studentBuilder_ == null) { + student_ = java.util.Collections.emptyList(); + bitField0_ = (bitField0_ & ~0x00000004); + } else { + studentBuilder_.clear(); + } + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Course_descriptor; + } + + public com.baeldung.protobuf.BaeldungTraining.Course getDefaultInstanceForType() { + return com.baeldung.protobuf.BaeldungTraining.Course.getDefaultInstance(); + } + + public com.baeldung.protobuf.BaeldungTraining.Course build() { + com.baeldung.protobuf.BaeldungTraining.Course result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public com.baeldung.protobuf.BaeldungTraining.Course buildPartial() { + com.baeldung.protobuf.BaeldungTraining.Course result = new com.baeldung.protobuf.BaeldungTraining.Course(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + result.id_ = id_; + result.courseName_ = courseName_; + if (studentBuilder_ == null) { + if (((bitField0_ & 0x00000004) == 0x00000004)) { + student_ = java.util.Collections.unmodifiableList(student_); + bitField0_ = (bitField0_ & ~0x00000004); + } + result.student_ = student_; + } else { + result.student_ = studentBuilder_.build(); + } + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.baeldung.protobuf.BaeldungTraining.Course) { + return mergeFrom((com.baeldung.protobuf.BaeldungTraining.Course)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.baeldung.protobuf.BaeldungTraining.Course other) { + if (other == com.baeldung.protobuf.BaeldungTraining.Course.getDefaultInstance()) return this; + if (other.getId() != 0) { + setId(other.getId()); + } + if (!other.getCourseName().isEmpty()) { + courseName_ = other.courseName_; + onChanged(); + } + if (studentBuilder_ == null) { + if (!other.student_.isEmpty()) { + if (student_.isEmpty()) { + student_ = other.student_; + bitField0_ = (bitField0_ & ~0x00000004); + } else { + ensureStudentIsMutable(); + student_.addAll(other.student_); + } + onChanged(); + } + } else { + if (!other.student_.isEmpty()) { + if (studentBuilder_.isEmpty()) { + studentBuilder_.dispose(); + studentBuilder_ = null; + student_ = other.student_; + bitField0_ = (bitField0_ & ~0x00000004); + studentBuilder_ = + com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders ? + getStudentFieldBuilder() : null; + } else { + studentBuilder_.addAllMessages(other.student_); + } + } + } + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.baeldung.protobuf.BaeldungTraining.Course parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.baeldung.protobuf.BaeldungTraining.Course) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + private int bitField0_; + + private int id_ ; + /** + * optional int32 id = 1; + */ + public int getId() { + return id_; + } + /** + * optional int32 id = 1; + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * optional int32 id = 1; + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private java.lang.Object courseName_ = ""; + /** + * optional string course_name = 2; + */ + public java.lang.String getCourseName() { + java.lang.Object ref = courseName_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + courseName_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string course_name = 2; + */ + public com.google.protobuf.ByteString + getCourseNameBytes() { + java.lang.Object ref = courseName_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + courseName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * optional string course_name = 2; + */ + public Builder setCourseName( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + courseName_ = value; + onChanged(); + return this; + } + /** + * optional string course_name = 2; + */ + public Builder clearCourseName() { + + courseName_ = getDefaultInstance().getCourseName(); + onChanged(); + return this; + } + /** + * optional string course_name = 2; + */ + public Builder setCourseNameBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + courseName_ = value; + onChanged(); + return this; + } + + private java.util.List student_ = + java.util.Collections.emptyList(); + private void ensureStudentIsMutable() { + if (!((bitField0_ & 0x00000004) == 0x00000004)) { + student_ = new java.util.ArrayList(student_); + bitField0_ |= 0x00000004; + } + } + + private com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student, com.baeldung.protobuf.BaeldungTraining.Student.Builder, com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder> studentBuilder_; + + /** + * repeated .baeldung.Student student = 3; + */ + public java.util.List getStudentList() { + if (studentBuilder_ == null) { + return java.util.Collections.unmodifiableList(student_); + } else { + return studentBuilder_.getMessageList(); + } + } + /** + * repeated .baeldung.Student student = 3; + */ + public int getStudentCount() { + if (studentBuilder_ == null) { + return student_.size(); + } else { + return studentBuilder_.getCount(); + } + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.Student getStudent(int index) { + if (studentBuilder_ == null) { + return student_.get(index); + } else { + return studentBuilder_.getMessage(index); + } + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder setStudent( + int index, com.baeldung.protobuf.BaeldungTraining.Student value) { + if (studentBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensureStudentIsMutable(); + student_.set(index, value); + onChanged(); + } else { + studentBuilder_.setMessage(index, value); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder setStudent( + int index, com.baeldung.protobuf.BaeldungTraining.Student.Builder builderForValue) { + if (studentBuilder_ == null) { + ensureStudentIsMutable(); + student_.set(index, builderForValue.build()); + onChanged(); + } else { + studentBuilder_.setMessage(index, builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder addStudent(com.baeldung.protobuf.BaeldungTraining.Student value) { + if (studentBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensureStudentIsMutable(); + student_.add(value); + onChanged(); + } else { + studentBuilder_.addMessage(value); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder addStudent( + int index, com.baeldung.protobuf.BaeldungTraining.Student value) { + if (studentBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensureStudentIsMutable(); + student_.add(index, value); + onChanged(); + } else { + studentBuilder_.addMessage(index, value); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder addStudent( + com.baeldung.protobuf.BaeldungTraining.Student.Builder builderForValue) { + if (studentBuilder_ == null) { + ensureStudentIsMutable(); + student_.add(builderForValue.build()); + onChanged(); + } else { + studentBuilder_.addMessage(builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder addStudent( + int index, com.baeldung.protobuf.BaeldungTraining.Student.Builder builderForValue) { + if (studentBuilder_ == null) { + ensureStudentIsMutable(); + student_.add(index, builderForValue.build()); + onChanged(); + } else { + studentBuilder_.addMessage(index, builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder addAllStudent( + java.lang.Iterable values) { + if (studentBuilder_ == null) { + ensureStudentIsMutable(); + com.google.protobuf.AbstractMessageLite.Builder.addAll( + values, student_); + onChanged(); + } else { + studentBuilder_.addAllMessages(values); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder clearStudent() { + if (studentBuilder_ == null) { + student_ = java.util.Collections.emptyList(); + bitField0_ = (bitField0_ & ~0x00000004); + onChanged(); + } else { + studentBuilder_.clear(); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public Builder removeStudent(int index) { + if (studentBuilder_ == null) { + ensureStudentIsMutable(); + student_.remove(index); + onChanged(); + } else { + studentBuilder_.remove(index); + } + return this; + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.Builder getStudentBuilder( + int index) { + return getStudentFieldBuilder().getBuilder(index); + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder getStudentOrBuilder( + int index) { + if (studentBuilder_ == null) { + return student_.get(index); } else { + return studentBuilder_.getMessageOrBuilder(index); + } + } + /** + * repeated .baeldung.Student student = 3; + */ + public java.util.List + getStudentOrBuilderList() { + if (studentBuilder_ != null) { + return studentBuilder_.getMessageOrBuilderList(); + } else { + return java.util.Collections.unmodifiableList(student_); + } + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.Builder addStudentBuilder() { + return getStudentFieldBuilder().addBuilder( + com.baeldung.protobuf.BaeldungTraining.Student.getDefaultInstance()); + } + /** + * repeated .baeldung.Student student = 3; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.Builder addStudentBuilder( + int index) { + return getStudentFieldBuilder().addBuilder( + index, com.baeldung.protobuf.BaeldungTraining.Student.getDefaultInstance()); + } + /** + * repeated .baeldung.Student student = 3; + */ + public java.util.List + getStudentBuilderList() { + return getStudentFieldBuilder().getBuilderList(); + } + private com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student, com.baeldung.protobuf.BaeldungTraining.Student.Builder, com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder> + getStudentFieldBuilder() { + if (studentBuilder_ == null) { + studentBuilder_ = new com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student, com.baeldung.protobuf.BaeldungTraining.Student.Builder, com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder>( + student_, + ((bitField0_ & 0x00000004) == 0x00000004), + getParentForChildren(), + isClean()); + student_ = null; + } + return studentBuilder_; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + + // @@protoc_insertion_point(builder_scope:baeldung.Course) + } + + // @@protoc_insertion_point(class_scope:baeldung.Course) + private static final com.baeldung.protobuf.BaeldungTraining.Course DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.baeldung.protobuf.BaeldungTraining.Course(); + } + + public static com.baeldung.protobuf.BaeldungTraining.Course getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public Course parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new Course(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public com.baeldung.protobuf.BaeldungTraining.Course getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + public interface StudentOrBuilder extends + // @@protoc_insertion_point(interface_extends:baeldung.Student) + com.google.protobuf.MessageOrBuilder { + + /** + * optional int32 id = 1; + */ + int getId(); + + /** + * optional string first_name = 2; + */ + java.lang.String getFirstName(); + /** + * optional string first_name = 2; + */ + com.google.protobuf.ByteString + getFirstNameBytes(); + + /** + * optional string last_name = 3; + */ + java.lang.String getLastName(); + /** + * optional string last_name = 3; + */ + com.google.protobuf.ByteString + getLastNameBytes(); + + /** + * optional string email = 4; + */ + java.lang.String getEmail(); + /** + * optional string email = 4; + */ + com.google.protobuf.ByteString + getEmailBytes(); + + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + java.util.List + getPhoneList(); + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getPhone(int index); + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + int getPhoneCount(); + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + java.util.List + getPhoneOrBuilderList(); + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder getPhoneOrBuilder( + int index); + } + /** + * Protobuf type {@code baeldung.Student} + */ + public static final class Student extends + com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:baeldung.Student) + StudentOrBuilder { + // Use Student.newBuilder() to construct. + private Student(com.google.protobuf.GeneratedMessage.Builder builder) { + super(builder); + } + private Student() { + id_ = 0; + firstName_ = ""; + lastName_ = ""; + email_ = ""; + phone_ = java.util.Collections.emptyList(); + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); + } + private Student( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!input.skipField(tag)) { + done = true; + } + break; + } + case 8: { + + id_ = input.readInt32(); + break; + } + case 18: { + java.lang.String s = input.readStringRequireUtf8(); + + firstName_ = s; + break; + } + case 26: { + java.lang.String s = input.readStringRequireUtf8(); + + lastName_ = s; + break; + } + case 34: { + java.lang.String s = input.readStringRequireUtf8(); + + email_ = s; + break; + } + case 42: { + if (!((mutable_bitField0_ & 0x00000010) == 0x00000010)) { + phone_ = new java.util.ArrayList(); + mutable_bitField0_ |= 0x00000010; + } + phone_.add(input.readMessage(com.baeldung.protobuf.BaeldungTraining.Student.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).setUnfinishedMessage(this); + } finally { + if (((mutable_bitField0_ & 0x00000010) == 0x00000010)) { + phone_ = java.util.Collections.unmodifiableList(phone_); + } + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Student.class, com.baeldung.protobuf.BaeldungTraining.Student.Builder.class); + } + + /** + * Protobuf enum {@code baeldung.Student.PhoneType} + */ + public enum PhoneType + implements com.google.protobuf.ProtocolMessageEnum { + /** + * MOBILE = 0; + */ + MOBILE(0), + /** + * LANDLINE = 1; + */ + LANDLINE(1), + UNRECOGNIZED(-1), + ; + + /** + * MOBILE = 0; + */ + public static final int MOBILE_VALUE = 0; + /** + * LANDLINE = 1; + */ + public static final int LANDLINE_VALUE = 1; + + + public final int getNumber() { + if (this == UNRECOGNIZED) { + throw new java.lang.IllegalArgumentException( + "Can't get the number of an unknown enum value."); + } + return value; + } + + /** + * @deprecated Use {@link #forNumber(int)} instead. + */ + @java.lang.Deprecated + public static PhoneType valueOf(int value) { + return forNumber(value); + } + + public static PhoneType forNumber(int value) { + switch (value) { + case 0: return MOBILE; + case 1: return LANDLINE; + default: return null; + } + } + + public static com.google.protobuf.Internal.EnumLiteMap + internalGetValueMap() { + return internalValueMap; + } + private static final com.google.protobuf.Internal.EnumLiteMap< + PhoneType> internalValueMap = + new com.google.protobuf.Internal.EnumLiteMap() { + public PhoneType findValueByNumber(int number) { + return PhoneType.forNumber(number); + } + }; + + public final com.google.protobuf.Descriptors.EnumValueDescriptor + getValueDescriptor() { + return getDescriptor().getValues().get(ordinal()); + } + public final com.google.protobuf.Descriptors.EnumDescriptor + getDescriptorForType() { + return getDescriptor(); + } + public static final com.google.protobuf.Descriptors.EnumDescriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.Student.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."); + } + if (desc.getIndex() == -1) { + return UNRECOGNIZED; + } + return VALUES[desc.getIndex()]; + } + + private final int value; + + private PhoneType(int value) { + this.value = value; + } + + // @@protoc_insertion_point(enum_scope:baeldung.Student.PhoneType) + } + + public interface PhoneNumberOrBuilder extends + // @@protoc_insertion_point(interface_extends:baeldung.Student.PhoneNumber) + com.google.protobuf.MessageOrBuilder { + + /** + * optional string number = 1; + */ + java.lang.String getNumber(); + /** + * optional string number = 1; + */ + com.google.protobuf.ByteString + getNumberBytes(); + + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + int getTypeValue(); + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + com.baeldung.protobuf.BaeldungTraining.Student.PhoneType getType(); + } + /** + * Protobuf type {@code baeldung.Student.PhoneNumber} + */ + public static final class PhoneNumber extends + com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:baeldung.Student.PhoneNumber) + PhoneNumberOrBuilder { + // Use PhoneNumber.newBuilder() to construct. + private PhoneNumber(com.google.protobuf.GeneratedMessage.Builder builder) { + super(builder); + } + private PhoneNumber() { + number_ = ""; + type_ = 0; + } + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return com.google.protobuf.UnknownFieldSet.getDefaultInstance(); + } + private PhoneNumber( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!input.skipField(tag)) { + done = true; + } + break; + } + case 10: { + java.lang.String s = input.readStringRequireUtf8(); + + number_ = s; + break; + } + case 16: { + int rawValue = input.readEnum(); + + type_ = rawValue; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_PhoneNumber_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_PhoneNumber_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.class, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder.class); + } + + public static final int NUMBER_FIELD_NUMBER = 1; + private volatile java.lang.Object number_; + /** + * optional string number = 1; + */ + 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(); + number_ = s; + return s; + } + } + /** + * optional string number = 1; + */ + 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; + } + } + + public static final int TYPE_FIELD_NUMBER = 2; + private int type_; + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public int getTypeValue() { + return type_; + } + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneType getType() { + com.baeldung.protobuf.BaeldungTraining.Student.PhoneType result = com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.forNumber(type_); + return result == null ? com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.UNRECOGNIZED : result; + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!getNumberBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 1, number_); + } + if (type_ != com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.MOBILE.getNumber()) { + output.writeEnum(2, type_); + } + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!getNumberBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(1, number_); + } + if (type_ != com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.MOBILE.getNumber()) { + size += com.google.protobuf.CodedOutputStream + .computeEnumSize(2, type_); + } + memoizedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code baeldung.Student.PhoneNumber} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder implements + // @@protoc_insertion_point(builder_implements:baeldung.Student.PhoneNumber) + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_PhoneNumber_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_PhoneNumber_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.class, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder.class); + } + + // Construct using com.baeldung.protobuf.BaeldungTraining.Student.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) { + } + } + public Builder clear() { + super.clear(); + number_ = ""; + + type_ = 0; + + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_PhoneNumber_descriptor; + } + + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getDefaultInstanceForType() { + return com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.getDefaultInstance(); + } + + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber build() { + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber buildPartial() { + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber result = new com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber(this); + result.number_ = number_; + result.type_ = type_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber) { + return mergeFrom((com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber other) { + if (other == com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.getDefaultInstance()) return this; + if (!other.getNumber().isEmpty()) { + number_ = other.number_; + onChanged(); + } + if (other.type_ != 0) { + setTypeValue(other.getTypeValue()); + } + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private java.lang.Object number_ = ""; + /** + * optional string number = 1; + */ + public java.lang.String getNumber() { + java.lang.Object ref = number_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + number_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string number = 1; + */ + 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; + } + } + /** + * optional string number = 1; + */ + public Builder setNumber( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + number_ = value; + onChanged(); + return this; + } + /** + * optional string number = 1; + */ + public Builder clearNumber() { + + number_ = getDefaultInstance().getNumber(); + onChanged(); + return this; + } + /** + * optional string number = 1; + */ + public Builder setNumberBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + number_ = value; + onChanged(); + return this; + } + + private int type_ = 0; + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public int getTypeValue() { + return type_; + } + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public Builder setTypeValue(int value) { + type_ = value; + onChanged(); + return this; + } + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneType getType() { + com.baeldung.protobuf.BaeldungTraining.Student.PhoneType result = com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.forNumber(type_); + return result == null ? com.baeldung.protobuf.BaeldungTraining.Student.PhoneType.UNRECOGNIZED : result; + } + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public Builder setType(com.baeldung.protobuf.BaeldungTraining.Student.PhoneType value) { + if (value == null) { + throw new NullPointerException(); + } + + type_ = value.getNumber(); + onChanged(); + return this; + } + /** + * optional .baeldung.Student.PhoneType type = 2; + */ + public Builder clearType() { + + type_ = 0; + onChanged(); + return this; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + + // @@protoc_insertion_point(builder_scope:baeldung.Student.PhoneNumber) + } + + // @@protoc_insertion_point(class_scope:baeldung.Student.PhoneNumber) + private static final com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber(); + } + + public static com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public PhoneNumber parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new PhoneNumber(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private int bitField0_; + public static final int ID_FIELD_NUMBER = 1; + private int id_; + /** + * optional int32 id = 1; + */ + public int getId() { + return id_; + } + + public static final int FIRST_NAME_FIELD_NUMBER = 2; + private volatile java.lang.Object firstName_; + /** + * optional string first_name = 2; + */ + public java.lang.String getFirstName() { + java.lang.Object ref = firstName_; + 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(); + firstName_ = s; + return s; + } + } + /** + * optional string first_name = 2; + */ + public com.google.protobuf.ByteString + getFirstNameBytes() { + java.lang.Object ref = firstName_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + firstName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int LAST_NAME_FIELD_NUMBER = 3; + private volatile java.lang.Object lastName_; + /** + * optional string last_name = 3; + */ + public java.lang.String getLastName() { + java.lang.Object ref = lastName_; + 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(); + lastName_ = s; + return s; + } + } + /** + * optional string last_name = 3; + */ + public com.google.protobuf.ByteString + getLastNameBytes() { + java.lang.Object ref = lastName_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + lastName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + public static final int EMAIL_FIELD_NUMBER = 4; + private volatile java.lang.Object email_; + /** + * optional string email = 4; + */ + 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(); + email_ = s; + return s; + } + } + /** + * optional string email = 4; + */ + 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; + } + } + + public static final int PHONE_FIELD_NUMBER = 5; + private java.util.List phone_; + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public java.util.List getPhoneList() { + return phone_; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public java.util.List + getPhoneOrBuilderList() { + return phone_; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public int getPhoneCount() { + return phone_.size(); + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getPhone(int index) { + return phone_.get(index); + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder getPhoneOrBuilder( + int index) { + return phone_.get(index); + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (id_ != 0) { + output.writeInt32(1, id_); + } + if (!getFirstNameBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 2, firstName_); + } + if (!getLastNameBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 3, lastName_); + } + if (!getEmailBytes().isEmpty()) { + com.google.protobuf.GeneratedMessage.writeString(output, 4, email_); + } + for (int i = 0; i < phone_.size(); i++) { + output.writeMessage(5, phone_.get(i)); + } + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (id_ != 0) { + size += com.google.protobuf.CodedOutputStream + .computeInt32Size(1, id_); + } + if (!getFirstNameBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(2, firstName_); + } + if (!getLastNameBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(3, lastName_); + } + if (!getEmailBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessage.computeStringSize(4, email_); + } + for (int i = 0; i < phone_.size(); i++) { + size += com.google.protobuf.CodedOutputStream + .computeMessageSize(5, phone_.get(i)); + } + memoizedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input); + } + public static com.baeldung.protobuf.BaeldungTraining.Student parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessage + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(com.baeldung.protobuf.BaeldungTraining.Student prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code baeldung.Student} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder implements + // @@protoc_insertion_point(builder_implements:baeldung.Student) + com.baeldung.protobuf.BaeldungTraining.StudentOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_fieldAccessorTable + .ensureFieldAccessorsInitialized( + com.baeldung.protobuf.BaeldungTraining.Student.class, com.baeldung.protobuf.BaeldungTraining.Student.Builder.class); + } + + // Construct using com.baeldung.protobuf.BaeldungTraining.Student.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(); + } + } + public Builder clear() { + super.clear(); + id_ = 0; + + firstName_ = ""; + + lastName_ = ""; + + email_ = ""; + + if (phoneBuilder_ == null) { + phone_ = java.util.Collections.emptyList(); + bitField0_ = (bitField0_ & ~0x00000010); + } else { + phoneBuilder_.clear(); + } + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return com.baeldung.protobuf.BaeldungTraining.internal_static_baeldung_Student_descriptor; + } + + public com.baeldung.protobuf.BaeldungTraining.Student getDefaultInstanceForType() { + return com.baeldung.protobuf.BaeldungTraining.Student.getDefaultInstance(); + } + + public com.baeldung.protobuf.BaeldungTraining.Student build() { + com.baeldung.protobuf.BaeldungTraining.Student result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public com.baeldung.protobuf.BaeldungTraining.Student buildPartial() { + com.baeldung.protobuf.BaeldungTraining.Student result = new com.baeldung.protobuf.BaeldungTraining.Student(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + result.id_ = id_; + result.firstName_ = firstName_; + result.lastName_ = lastName_; + result.email_ = email_; + if (phoneBuilder_ == null) { + if (((bitField0_ & 0x00000010) == 0x00000010)) { + phone_ = java.util.Collections.unmodifiableList(phone_); + bitField0_ = (bitField0_ & ~0x00000010); + } + 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 com.baeldung.protobuf.BaeldungTraining.Student) { + return mergeFrom((com.baeldung.protobuf.BaeldungTraining.Student)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(com.baeldung.protobuf.BaeldungTraining.Student other) { + if (other == com.baeldung.protobuf.BaeldungTraining.Student.getDefaultInstance()) return this; + if (other.getId() != 0) { + setId(other.getId()); + } + if (!other.getFirstName().isEmpty()) { + firstName_ = other.firstName_; + onChanged(); + } + if (!other.getLastName().isEmpty()) { + lastName_ = other.lastName_; + onChanged(); + } + if (!other.getEmail().isEmpty()) { + email_ = other.email_; + onChanged(); + } + if (phoneBuilder_ == null) { + if (!other.phone_.isEmpty()) { + if (phone_.isEmpty()) { + phone_ = other.phone_; + bitField0_ = (bitField0_ & ~0x00000010); + } else { + ensurePhoneIsMutable(); + phone_.addAll(other.phone_); + } + onChanged(); + } + } else { + if (!other.phone_.isEmpty()) { + if (phoneBuilder_.isEmpty()) { + phoneBuilder_.dispose(); + phoneBuilder_ = null; + phone_ = other.phone_; + bitField0_ = (bitField0_ & ~0x00000010); + phoneBuilder_ = + com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders ? + getPhoneFieldBuilder() : null; + } else { + phoneBuilder_.addAllMessages(other.phone_); + } + } + } + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + com.baeldung.protobuf.BaeldungTraining.Student parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (com.baeldung.protobuf.BaeldungTraining.Student) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + private int bitField0_; + + private int id_ ; + /** + * optional int32 id = 1; + */ + public int getId() { + return id_; + } + /** + * optional int32 id = 1; + */ + public Builder setId(int value) { + + id_ = value; + onChanged(); + return this; + } + /** + * optional int32 id = 1; + */ + public Builder clearId() { + + id_ = 0; + onChanged(); + return this; + } + + private java.lang.Object firstName_ = ""; + /** + * optional string first_name = 2; + */ + public java.lang.String getFirstName() { + java.lang.Object ref = firstName_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + firstName_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string first_name = 2; + */ + public com.google.protobuf.ByteString + getFirstNameBytes() { + java.lang.Object ref = firstName_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + firstName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * optional string first_name = 2; + */ + public Builder setFirstName( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + firstName_ = value; + onChanged(); + return this; + } + /** + * optional string first_name = 2; + */ + public Builder clearFirstName() { + + firstName_ = getDefaultInstance().getFirstName(); + onChanged(); + return this; + } + /** + * optional string first_name = 2; + */ + public Builder setFirstNameBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + firstName_ = value; + onChanged(); + return this; + } + + private java.lang.Object lastName_ = ""; + /** + * optional string last_name = 3; + */ + public java.lang.String getLastName() { + java.lang.Object ref = lastName_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + lastName_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string last_name = 3; + */ + public com.google.protobuf.ByteString + getLastNameBytes() { + java.lang.Object ref = lastName_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (java.lang.String) ref); + lastName_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * optional string last_name = 3; + */ + public Builder setLastName( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + lastName_ = value; + onChanged(); + return this; + } + /** + * optional string last_name = 3; + */ + public Builder clearLastName() { + + lastName_ = getDefaultInstance().getLastName(); + onChanged(); + return this; + } + /** + * optional string last_name = 3; + */ + public Builder setLastNameBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + lastName_ = value; + onChanged(); + return this; + } + + private java.lang.Object email_ = ""; + /** + * optional string email = 4; + */ + public java.lang.String getEmail() { + java.lang.Object ref = email_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + email_ = s; + return s; + } else { + return (java.lang.String) ref; + } + } + /** + * optional string email = 4; + */ + 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; + } + } + /** + * optional string email = 4; + */ + public Builder setEmail( + java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + + email_ = value; + onChanged(); + return this; + } + /** + * optional string email = 4; + */ + public Builder clearEmail() { + + email_ = getDefaultInstance().getEmail(); + onChanged(); + return this; + } + /** + * optional string email = 4; + */ + public Builder setEmailBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + email_ = value; + onChanged(); + return this; + } + + private java.util.List phone_ = + java.util.Collections.emptyList(); + private void ensurePhoneIsMutable() { + if (!((bitField0_ & 0x00000010) == 0x00000010)) { + phone_ = new java.util.ArrayList(phone_); + bitField0_ |= 0x00000010; + } + } + + private com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder> phoneBuilder_; + + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public java.util.List getPhoneList() { + if (phoneBuilder_ == null) { + return java.util.Collections.unmodifiableList(phone_); + } else { + return phoneBuilder_.getMessageList(); + } + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public int getPhoneCount() { + if (phoneBuilder_ == null) { + return phone_.size(); + } else { + return phoneBuilder_.getCount(); + } + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber getPhone(int index) { + if (phoneBuilder_ == null) { + return phone_.get(index); + } else { + return phoneBuilder_.getMessage(index); + } + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder setPhone( + int index, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber value) { + if (phoneBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensurePhoneIsMutable(); + phone_.set(index, value); + onChanged(); + } else { + phoneBuilder_.setMessage(index, value); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder setPhone( + int index, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder builderForValue) { + if (phoneBuilder_ == null) { + ensurePhoneIsMutable(); + phone_.set(index, builderForValue.build()); + onChanged(); + } else { + phoneBuilder_.setMessage(index, builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder addPhone(com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber value) { + if (phoneBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensurePhoneIsMutable(); + phone_.add(value); + onChanged(); + } else { + phoneBuilder_.addMessage(value); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder addPhone( + int index, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber value) { + if (phoneBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + ensurePhoneIsMutable(); + phone_.add(index, value); + onChanged(); + } else { + phoneBuilder_.addMessage(index, value); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder addPhone( + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder builderForValue) { + if (phoneBuilder_ == null) { + ensurePhoneIsMutable(); + phone_.add(builderForValue.build()); + onChanged(); + } else { + phoneBuilder_.addMessage(builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder addPhone( + int index, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder builderForValue) { + if (phoneBuilder_ == null) { + ensurePhoneIsMutable(); + phone_.add(index, builderForValue.build()); + onChanged(); + } else { + phoneBuilder_.addMessage(index, builderForValue.build()); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder addAllPhone( + java.lang.Iterable values) { + if (phoneBuilder_ == null) { + ensurePhoneIsMutable(); + com.google.protobuf.AbstractMessageLite.Builder.addAll( + values, phone_); + onChanged(); + } else { + phoneBuilder_.addAllMessages(values); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder clearPhone() { + if (phoneBuilder_ == null) { + phone_ = java.util.Collections.emptyList(); + bitField0_ = (bitField0_ & ~0x00000010); + onChanged(); + } else { + phoneBuilder_.clear(); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public Builder removePhone(int index) { + if (phoneBuilder_ == null) { + ensurePhoneIsMutable(); + phone_.remove(index); + onChanged(); + } else { + phoneBuilder_.remove(index); + } + return this; + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder getPhoneBuilder( + int index) { + return getPhoneFieldBuilder().getBuilder(index); + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder getPhoneOrBuilder( + int index) { + if (phoneBuilder_ == null) { + return phone_.get(index); } else { + return phoneBuilder_.getMessageOrBuilder(index); + } + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public java.util.List + getPhoneOrBuilderList() { + if (phoneBuilder_ != null) { + return phoneBuilder_.getMessageOrBuilderList(); + } else { + return java.util.Collections.unmodifiableList(phone_); + } + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder addPhoneBuilder() { + return getPhoneFieldBuilder().addBuilder( + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.getDefaultInstance()); + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder addPhoneBuilder( + int index) { + return getPhoneFieldBuilder().addBuilder( + index, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.getDefaultInstance()); + } + /** + * repeated .baeldung.Student.PhoneNumber phone = 5; + */ + public java.util.List + getPhoneBuilderList() { + return getPhoneFieldBuilder().getBuilderList(); + } + private com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder> + getPhoneFieldBuilder() { + if (phoneBuilder_ == null) { + phoneBuilder_ = new com.google.protobuf.RepeatedFieldBuilder< + com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumber.Builder, com.baeldung.protobuf.BaeldungTraining.Student.PhoneNumberOrBuilder>( + phone_, + ((bitField0_ & 0x00000010) == 0x00000010), + getParentForChildren(), + isClean()); + phone_ = null; + } + return phoneBuilder_; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return this; + } + + + // @@protoc_insertion_point(builder_scope:baeldung.Student) + } + + // @@protoc_insertion_point(class_scope:baeldung.Student) + private static final com.baeldung.protobuf.BaeldungTraining.Student DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new com.baeldung.protobuf.BaeldungTraining.Student(); + } + + public static com.baeldung.protobuf.BaeldungTraining.Student getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public Student parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new Student(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public com.baeldung.protobuf.BaeldungTraining.Student getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_baeldung_Course_descriptor; + private static final + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_baeldung_Course_fieldAccessorTable; + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_baeldung_Student_descriptor; + private static final + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_baeldung_Student_fieldAccessorTable; + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_baeldung_Student_PhoneNumber_descriptor; + private static final + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_baeldung_Student_PhoneNumber_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String[] descriptorData = { + "\n\030resources/baeldung.proto\022\010baeldung\"M\n\006" + + "Course\022\n\n\002id\030\001 \001(\005\022\023\n\013course_name\030\002 \001(\t\022" + + "\"\n\007student\030\003 \003(\0132\021.baeldung.Student\"\352\001\n\007" + + "Student\022\n\n\002id\030\001 \001(\005\022\022\n\nfirst_name\030\002 \001(\t\022" + + "\021\n\tlast_name\030\003 \001(\t\022\r\n\005email\030\004 \001(\t\022,\n\005pho" + + "ne\030\005 \003(\0132\035.baeldung.Student.PhoneNumber\032" + + "H\n\013PhoneNumber\022\016\n\006number\030\001 \001(\t\022)\n\004type\030\002" + + " \001(\0162\033.baeldung.Student.PhoneType\"%\n\tPho" + + "neType\022\n\n\006MOBILE\020\000\022\014\n\010LANDLINE\020\001B)\n\025com." + + "baeldung.protobufB\020BaeldungTrainingb\006pro", + "to3" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + internal_static_baeldung_Course_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_baeldung_Course_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_baeldung_Course_descriptor, + new java.lang.String[] { "Id", "CourseName", "Student", }); + internal_static_baeldung_Student_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_baeldung_Student_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_baeldung_Student_descriptor, + new java.lang.String[] { "Id", "FirstName", "LastName", "Email", "Phone", }); + internal_static_baeldung_Student_PhoneNumber_descriptor = + internal_static_baeldung_Student_descriptor.getNestedTypes().get(0); + internal_static_baeldung_Student_PhoneNumber_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_baeldung_Student_PhoneNumber_descriptor, + new java.lang.String[] { "Number", "Type", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseController.java b/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseController.java new file mode 100644 index 0000000000..027f296fa2 --- /dev/null +++ b/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseController.java @@ -0,0 +1,19 @@ +package com.baeldung.protobuf; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.protobuf.BaeldungTraining.Course; + +@RestController +public class CourseController { + @Autowired + CourseRepository courseRepo; + + @RequestMapping("/courses/{id}") + Course customer(@PathVariable Integer id) { + return courseRepo.getCourse(id); + } +} diff --git a/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseRepository.java b/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseRepository.java new file mode 100644 index 0000000000..aa00b2a850 --- /dev/null +++ b/spring-protobuf/src/main/java/com/baeldung/protobuf/CourseRepository.java @@ -0,0 +1,17 @@ +package com.baeldung.protobuf; + +import java.util.Map; + +import com.baeldung.protobuf.BaeldungTraining.Course; + +public class CourseRepository { + Map courses; + + public CourseRepository (Map courses) { + this.courses = courses; + } + + public Course getCourse(int id) { + return courses.get(id); + } +} diff --git a/spring-protobuf/src/main/resources/baeldung.proto b/spring-protobuf/src/main/resources/baeldung.proto new file mode 100644 index 0000000000..20124c34c4 --- /dev/null +++ b/spring-protobuf/src/main/resources/baeldung.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +package baeldung; + +option java_package = "com.baeldung.protobuf"; +option java_outer_classname = "BaeldungTraining"; + +message Course { + int32 id = 1; + string course_name = 2; + repeated Student student = 3; +} + +message Student { + int32 id = 1; + string first_name = 2; + string last_name = 3; + string email = 4; + repeated PhoneNumber phone = 5; + + message PhoneNumber { + string number = 1; + PhoneType type = 2; + } + + enum PhoneType { + MOBILE = 0; + LANDLINE = 1; + } +} \ No newline at end of file diff --git a/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationTest.java b/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationTest.java new file mode 100644 index 0000000000..026fa04fa2 --- /dev/null +++ b/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationTest.java @@ -0,0 +1,76 @@ +package com.baeldung.protobuf; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.baeldung.protobuf.BaeldungTraining.Course; +import com.googlecode.protobuf.format.JsonFormat; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.containsString; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebIntegrationTest +public class ApplicationTest { + private static final String COURSE1_URL = "http://localhost:8080/courses/1"; + @Autowired + private RestTemplate restTemplate; + + @Test + public void whenUsingRestTemplate_thenSucceed() { + ResponseEntity course = restTemplate.getForEntity(COURSE1_URL, Course.class); + assertResponse(course.toString()); + } + + @Test + public void whenUsingHttpClient_thenSucceed() throws IOException { + InputStream responseStream = executeHttpRequest(COURSE1_URL); + String jsonOutput = convertProtobufMessageStreamToJsonString(responseStream); + assertResponse(jsonOutput); + } + + private InputStream executeHttpRequest(String url) throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet(url); + HttpResponse httpResponse = httpClient.execute(request); + return httpResponse.getEntity().getContent(); + } + + private String convertProtobufMessageStreamToJsonString(InputStream protobufStream) throws IOException { + JsonFormat jsonFormat = new JsonFormat(); + Course course = Course.parseFrom(protobufStream); + return jsonFormat.printToString(course); + } + + private void assertResponse(String response) { + assertThat(response, containsString("id")); + assertThat(response, containsString("course_name")); + assertThat(response, containsString("REST with Spring")); + assertThat(response, containsString("student")); + assertThat(response, containsString("first_name")); + assertThat(response, containsString("last_name")); + assertThat(response, containsString("email")); + assertThat(response, containsString("john.doe@baeldung.com")); + assertThat(response, containsString("richard.roe@baeldung.com")); + assertThat(response, containsString("jane.doe@baeldung.com")); + assertThat(response, containsString("phone")); + assertThat(response, containsString("number")); + assertThat(response, containsString("type")); + } +} \ No newline at end of file