Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a small change to a Java protocol buffers object

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?

like image 380
fadedbee Avatar asked Mar 23 '15 11:03

fadedbee


People also ask

How do I create a Java proto file?

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 .

What is .proto file in Java?

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 .

How do you make Protobuf?

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.

What is a protocol buffer message?

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).

How do I use a protocol buffer in Java?

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.

What are the methods of buffer class in Java?

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.

How do I modify a message in a protocol buffer?

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.

What is the purpose of a builder in Protocol Buffers?

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.


1 Answers

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.

like image 200
Kenton Varda Avatar answered Oct 23 '22 00:10

Kenton Varda