Builders in their current form suffer from a potential misuse where calling a builder setter after calling .build() may alter the state of a previously-create "target" object.
Consider:
var b = new CollectionConfig.Builder("CarelessCollection");
b.properties(Property.text("text_a"));
var cfg = b.build();
// Somewhere later in the code
b.properties(Property.text("text_b"));
client.collections.create(cfg);
CarelessCollection is created with 2 properties, despite what you might expect.
Here's another good illustration of this point: #399 (comment)
We should:
- In all constructors that accept a Builder, wrap all mutable fields (list / map) in their unmodifiable views:
Collections.unmodifiableList etc.
- Check
.build() is only called once per object. Sth like this.