# Android - Protobuf Samples
###### tags: `Android` `Protobuf`
# Reference
### [Language Guide (proto3)](https://developers.google.com/protocol-buffers/docs/proto3)
### [How to add a int array in protobuf message](https://stackoverflow.com/questions/7408203/how-to-add-a-int-array-in-protobuf-message)
### [Repeated fields for lists and arrays](https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/protobuf-repeated)
C#
You specify lists in Protocol Buffer (Protobuf)
by using the ``repeated`` prefix keyword.
The following example shows how to create a list:
```
message Person {
// Other fields elided
repeated string aliases = 8;
}
```
In the generated code, ``repeated`` fields are represented by
__read-only properties__ of the ``Google.Protobuf.Collections.RepeatedField<T>`` type
rather than any of the built-in .NET collection types.
This type implements all the standard .NET collection interfaces,
such as ``IList<T>`` and ``IEnumerable<T>``.
So you can use __LINQ queries__ or __convert it to an array or a list easily__.
The ``RepeatedField<T>`` type includes the code required
to serialize and deserialize the list to the binary wire format.
---
# 1. Sample1 - Nested enum
```
syntax = "proto3";
option java_package = "com.shalu.android.datastore";
option java_multiple_files = true;
message UserSettings {
enum BgColor {
COLOR_WHITE = 0;
COLOR_BLACK = 1;
}
BgColor bgColor = 1;
}
```
```
public final class UserSettings extends
com.google.protobuf.GeneratedMessageLite<UserSettings, UserSettings.Builder>
implements
// @@protoc_insertion_point(message_implements:UserSettings)
UserSettingsOrBuilder {
private UserSettings() {}
public enum BgColor implements com.google.protobuf.Internal.EnumLite {
/**
* <code>COLOR_WHITE = 0;</code>
*/
COLOR_WHITE(0),
/**
* <code>COLOR_BLACK = 1;</code>
*/
COLOR_BLACK(1),
UNRECOGNIZED(-1),
;
private final int value;
private BgColor(int value) {
this.value = value;
}
}
public static final int BGCOLOR_FIELD_NUMBER = 1;
private int bgColor_;
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @return The enum numeric value on the wire for bgColor.
*/
@java.lang.Override
public int getBgColorValue() {
return bgColor_;
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @param value The enum numeric value on the wire for bgColor to set.
*/
private void setBgColorValue(int value) {
bgColor_ = value;
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @param value The bgColor to set.
*/
private void setBgColor(com.shalu.android.datastore.UserSettings.BgColor value) {
bgColor_ = value.getNumber();
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
*/
private void clearBgColor() {
bgColor_ = 0;
}
public static final class Builder extends
com.google.protobuf.GeneratedMessageLite.Builder<com.shalu.android.datastore.UserSettings, Builder>
implements
// @@protoc_insertion_point(builder_implements:UserSettings)
com.shalu.android.datastore.UserSettingsOrBuilder {
// Construct using com.shalu.android.datastore.UserSettings.newBuilder()
private Builder() {
super(DEFAULT_INSTANCE);
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @return The enum numeric value on the wire for bgColor.
*/
@java.lang.Override
public int getBgColorValue() {
return instance.getBgColorValue();
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @param value The bgColor to set.
* @return This builder for chaining.
*/
public Builder setBgColorValue(int value) {
copyOnWrite();
instance.setBgColorValue(value);
return this;
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @return The bgColor.
*/
@java.lang.Override
public com.shalu.android.datastore.UserSettings.BgColor getBgColor() {
return instance.getBgColor();
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @param value The enum numeric value on the wire for bgColor to set.
* @return This builder for chaining.
*/
public Builder setBgColor(com.shalu.android.datastore.UserSettings.BgColor value) {
copyOnWrite();
instance.setBgColor(value);
return this;
}
/**
* <code>.UserSettings.BgColor bgColor = 1;</code>
* @return This builder for chaining.
*/
public Builder clearBgColor() {
copyOnWrite();
instance.clearBgColor();
return this;
}
}
}
```
---
# 2. Sample2 - Two classes
```
syntax = "proto3";
//package tutorial;
option java_package = "com.elyeproj.demosimpleprotobuf.tutorial";
option java_multiple_files = true; // seems to need the setting, but i don't know why?
message Person {
string name = 1;
int32 id = 2;
string email = 3;
string phone = 4;
}
// ===
//option java_package = "com.vladsonkin.protodatastore.proto";
//option java_multiple_files = true;
message FilterPreference {
string serial = 1;
string color = 2;
string manufacturer = 3;
string type = 4;
string stolenLocation = 5;
}
```
```
public final class Person extends com.google.protobuf.GeneratedMessageLite<Person, Person.Builder>
implements
// @@protoc_insertion_point(message_implements:Person)
PersonOrBuilder {
private Person() {
name_ = "";
email_ = "";
phone_ = "";
}
public static final int NAME_FIELD_NUMBER = 1;
private java.lang.String name_;
/**
* <code>string name = 1;</code>
* @return The name.
*/
@java.lang.Override
public java.lang.String getName() {
return name_;
}
/**
* <code>string name = 1;</code>
* @param value The name to set.
*/
private void setName(java.lang.String value) {
value.getClass();
name_ = value;
}
public static final int ID_FIELD_NUMBER = 2;
private int id_;
/**
* <code>int32 id = 2;</code>
* @return The id.
*/
@java.lang.Override
public int getId() {
return id_;
}
/**
* <code>int32 id = 2;</code>
* @param value The id to set.
*/
private void setId(int value) {
id_ = value;
}
public static final int EMAIL_FIELD_NUMBER = 3;
private java.lang.String email_;
/**
* <code>string email = 3;</code>
* @return The email.
*/
@java.lang.Override
public java.lang.String getEmail() {
return email_;
}
/**
* <code>string email = 3;</code>
* @param value The email to set.
*/
private void setEmail(java.lang.String value) {
value.getClass();
email_ = value;
}
public static final int PHONE_FIELD_NUMBER = 4;
private java.lang.String phone_;
/**
* <code>string phone = 4;</code>
* @return The phone.
*/
@java.lang.Override
public java.lang.String getPhone() {
return phone_;
}
/**
* <code>string phone = 4;</code>
* @param value The phone to set.
*/
private void setPhone(java.lang.String value) {
value.getClass();
phone_ = value;
}
public static final class Builder extends com.google.protobuf.GeneratedMessageLite.Builder<com.elyeproj.demosimpleprotobuf.tutorial.Person, Builder> implements
// @@protoc_insertion_point(builder_implements:Person)
com.elyeproj.demosimpleprotobuf.tutorial.PersonOrBuilder {
}
}
```
```
public final class FilterPreference
extends com.google.protobuf.GeneratedMessageLite<FilterPreference, FilterPreference.Builder>
implements
// @@protoc_insertion_point(message_implements:FilterPreference)
FilterPreferenceOrBuilder {
private FilterPreference() {
serial_ = "";
color_ = "";
manufacturer_ = "";
type_ = "";
stolenLocation_ = "";
}
public static final int SERIAL_FIELD_NUMBER = 1;
private java.lang.String serial_;
/**
* <code>string serial = 1;</code>
*
* @return The serial.
*/
@java.lang.Override
public java.lang.String getSerial() {
return serial_;
}
/**
* <code>string serial = 1;</code>
*
* @param value The serial to set.
*/
private void setSerial(java.lang.String value) {
value.getClass();
serial_ = value;
}
public static final int COLOR_FIELD_NUMBER = 2;
private java.lang.String color_;
/**
* <code>string color = 2;</code>
*
* @return The color.
*/
@java.lang.Override
public java.lang.String getColor() {
return color_;
}
/**
* <code>string color = 2;</code>
*
* @param value The color to set.
*/
private void setColor(java.lang.String value) {
value.getClass();
color_ = value;
}
public static final int MANUFACTURER_FIELD_NUMBER = 3;
private java.lang.String manufacturer_;
/**
* <code>string manufacturer = 3;</code>
*
* @return The manufacturer.
*/
@java.lang.Override
public java.lang.String getManufacturer() {
return manufacturer_;
}
/**
* <code>string manufacturer = 3;</code>
*
* @param value The manufacturer to set.
*/
private void setManufacturer(java.lang.String value) {
value.getClass();
manufacturer_ = value;
}
public static final int TYPE_FIELD_NUMBER = 4;
private java.lang.String type_;
/**
* <code>string type = 4;</code>
*
* @return The type.
*/
@java.lang.Override
public java.lang.String getType() {
return type_;
}
/**
* <code>string type = 4;</code>
*
* @param value The type to set.
*/
private void setType(java.lang.String value) {
value.getClass();
type_ = value;
}
public static final int STOLENLOCATION_FIELD_NUMBER = 5;
private java.lang.String stolenLocation_;
/**
* <code>string stolenLocation = 5;</code>
*
* @return The stolenLocation.
*/
@java.lang.Override
public java.lang.String getStolenLocation() {
return stolenLocation_;
}
/**
* <code>string stolenLocation = 5;</code>
*
* @param value The stolenLocation to set.
*/
private void setStolenLocation(java.lang.String value) {
value.getClass();
stolenLocation_ = value;
}
public static final class Builder
extends com.google.protobuf.GeneratedMessageLite.Builder<com.elyeproj.demosimpleprotobuf.tutorial.FilterPreference, Builder>
implements
// @@protoc_insertion_point(builder_implements:FilterPreference)
com.elyeproj.demosimpleprotobuf.tutorial.FilterPreferenceOrBuilder {
// Construct using com.elyeproj.demosimpleprotobuf.tutorial.FilterPreference.newBuilder()
private Builder() {
super(DEFAULT_INSTANCE);
}
}
}
```
---
# 3. Sample3 - Nested classes with ``List``
```
syntax = "proto3";
option java_package = "com.shalu.android.datastore";
option java_multiple_files = true;
message SearchResponse {
message Result {
string url = 1;
string title = 2;
repeated string snippets = 3;
}
repeated Result results = 1;
}
```
``repeated`` is used to define a ``List`` of some types!!
```
public final class SearchResponse
extends com.google.protobuf.GeneratedMessageLite<SearchResponse, SearchResponse.Builder>
// @@protoc_insertion_point(message_implements:SearchResponse)
implements SearchResponseOrBuilder {
private SearchResponse() { results_ = emptyProtobufList(); }
public static final int RESULTS_FIELD_NUMBER = 1;
private com.google.protobuf.Internal.ProtobufList<com.shalu.android.datastore.SearchResponse.Result> results_;
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
@java.lang.Override
public java.util.List<com.shalu.android.datastore.SearchResponse.Result> getResultsList() { return results_; }
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
@java.lang.Override
public int getResultsCount() { return results_.size(); }
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
@java.lang.Override
public com.shalu.android.datastore.SearchResponse.Result getResults(int index) { return results_.get(index); }
private void ensureResultsIsMutable() {
com.google.protobuf.Internal.ProtobufList<com.shalu.android.datastore.SearchResponse.Result> tmp = results_;
if (!tmp.isModifiable()) {
results_ =
com.google.protobuf.GeneratedMessageLite.mutableCopy(tmp);
}
}
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void setResults(int index, com.shalu.android.datastore.SearchResponse.Result value) {
value.getClass();
ensureResultsIsMutable();
results_.set(index, value);
}
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void addResults(com.shalu.android.datastore.SearchResponse.Result value) {
value.getClass();
ensureResultsIsMutable();
results_.add(value);
}
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void addResults(int index, com.shalu.android.datastore.SearchResponse.Result value) {
value.getClass();
ensureResultsIsMutable();
results_.add(index, value);
}
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void addAllResults(java.lang.Iterable<? extends com.shalu.android.datastore.SearchResponse.Result> values) {
ensureResultsIsMutable();
com.google.protobuf.AbstractMessageLite.addAll(values, results_);
}
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void clearResults() { results_ = emptyProtobufList(); }
/**
* <code>repeated .SearchResponse.Result results = 1;</code>
*/
private void removeResults(int index) {
ensureResultsIsMutable();
results_.remove(index);
}
///
public static final class Result
extends com.google.protobuf.GeneratedMessageLite<Result, Result.Builder>
// @@protoc_insertion_point(message_implements:SearchResponse.Result)
implements ResultOrBuilder {
private Result() {
url_ = "";
title_ = "";
snippets_ = com.google.protobuf.GeneratedMessageLite.emptyProtobufList();
}
public static final int URL_FIELD_NUMBER = 1;
private java.lang.String url_;
/**
* <code>string url = 1;</code>
* @return The url.
*/
@java.lang.Override
public java.lang.String getUrl() { return url_; }
/**
* <code>string url = 1;</code>
* @param value The url to set.
*/
private void setUrl(java.lang.String value) {
value.getClass();
url_ = value;
}
public static final int TITLE_FIELD_NUMBER = 2;
private java.lang.String title_;
/**
* <code>string title = 2;</code>
* @return The title.
*/
@java.lang.Override
public java.lang.String getTitle() { return title_; }
/**
* <code>string title = 2;</code>
* @param value The title to set.
*/
private void setTitle(java.lang.String value) {
value.getClass();
title_ = value;
}
public static final int SNIPPETS_FIELD_NUMBER = 3;
private com.google.protobuf.Internal.ProtobufList<java.lang.String> snippets_;
/**
* <code>repeated string snippets = 3;</code>
* @return A list containing the snippets.
*/
@java.lang.Override
public java.util.List<java.lang.String> getSnippetsList() { return snippets_; }
/**
* <code>repeated string snippets = 3;</code>
* @return The count of snippets.
*/
@java.lang.Override
public int getSnippetsCount() { return snippets_.size(); }
/**
* <code>repeated string snippets = 3;</code>
* @param index The index of the element to return.
* @return The snippets at the given index.
*/
@java.lang.Override
public java.lang.String getSnippets(int index) { return snippets_.get(index); }
private void ensureSnippetsIsMutable() {
com.google.protobuf.Internal.ProtobufList<java.lang.String> tmp = snippets_;
if (!tmp.isModifiable()) {
snippets_ = com.google.protobuf.GeneratedMessageLite.mutableCopy(tmp);
}
}
/**
* <code>repeated string snippets = 3;</code>
* @param index The index to set the value at.
* @param value The snippets to set.
*/
private void setSnippets(int index, java.lang.String value) {
value.getClass();
ensureSnippetsIsMutable();
snippets_.set(index, value);
}
/**
* <code>repeated string snippets = 3;</code>
* @param value The snippets to add.
*/
private void addSnippets(java.lang.String value) {
value.getClass();
ensureSnippetsIsMutable();
snippets_.add(value);
}
/**
* <code>repeated string snippets = 3;</code>
* @param values The snippets to add.
*/
private void addAllSnippets(java.lang.Iterable<java.lang.String> values) {
ensureSnippetsIsMutable();
com.google.protobuf.AbstractMessageLite.addAll(values, snippets_);
}
/**
* <code>repeated string snippets = 3;</code>
*/
private void clearSnippets() {
snippets_ = com.google.protobuf.GeneratedMessageLite.emptyProtobufList();
}
}
}
```