I want to make a small change, deep in tree of Java protocol buffer objects.
I can use the .getBuilder()
method to make a new object that is a clone of an old one with some changes.
When this is done at a deep level, the code becomes ugly:
Quux.Builder quuxBuilder = foo.getBar().getBaz().getQuux().toBuilder()
Baz.Builder bazBuilder = foo.getBar().getBaz().toBuilder()
Bar.Builder barBuilder = foo.getBar().toBuilder()
Foo.Builder fooBuilder = foo.toBuilder()
quuxBuilder.setNewThing(newThing);
bazBuilder.setQuux(quuxBuilder);
barBuilder.setBaz(bazBuilder);
fooBuilder.setBar(barBuilder);
Foo newFoo = fooBuilder.build();
(This is just 4 levels, I'm routinely dealing with 5-8 levels.)
Is there a better way?
The protocol buffer compiler produces Java output when invoked with the --java_out= command-line flag. The parameter to the --java_out= option is the directory where you want the compiler to write your Java output. For each . proto file input, the compiler creates a wrapper .
proto file are simple: you add a message for each data structure you want to serialize, then specify a name and a type for each field in the message. Here is the . proto file that defines your messages, addressbook. proto .
Creating an Instance of Protobuf Defined Messages We can easily use a generated code to create Java instance of a Person class: String email = "[email protected]"; int id = new Random(). nextInt(); String name = "Michael Program"; String number = "01234567890"; AddressBookProtos. Person person = AddressBookProtos.
Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).
This tutorial provides a basic Java programmer's introduction to working with protocol buffers. By walking through creating a simple example application, it shows you how to Define message formats in a .proto file. Use the protocol buffer compiler. Use the Java protocol buffer API to write and read messages.
Buffer class inherits the following methods from class java.lang.Object such as clone (), finalize (), getClass (), hashCode (), notify (), notifyAll (), toString (), wait (). Now, moving on to the methods of Buffer class is as follows as shown alphabetically in the tabular format shown below: This method returns this buffer’s capacity.
The message classes generated by the protocol buffer compiler are all immutable. Once a message object is constructed, it cannot be modified, just like a Java String. To construct a message, you must first construct a builder, set any fields you want to set to your chosen values, then call the builder's build () method.
As the previous code listing demonstrates, a "builder" is used to populate the immutable instance of the class generated by Protocol Buffers. With a reference to this instance, I can now easily write the contents of the instance out in Protocol Buffers' binary form using the method toByteArray () on that instance as shown in the next code listing.
Another option is (I think; it's been a while):
Foo.Builder fooBuilder = foo.toBuilder();
fooBuilder.getBarBuilder().getBazBuilder().getQuuxBuilder()
.setNewThing(newThing);
newFoo = fooBuilder.build();
Note that this isn't any more efficient; you're still making copies of foo, bar, baz, and quux.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With