Full documentation at immutables.org
@Value.Builder
record Person(String name, int age, String email) {}
// Use the generated builder
Person person = new PersonBuilder()
.name("Alice")
.age(30)
.email("[email protected]")
.build();More fancy example having copy-with methods generated, and style withUnaryOperator="with*"
@Value.Builder
record Person(String name, int age) implements WithPerson {
// Extend the generated PersonBuilder
static class Builder extends PersonBuilder {}
}
// Use your custom builder
var person = new Person.Builder()
.name("Bob")
.age(18)
.build();
person = person.withName("Bobby!")
.withAge(age -> age + 3);Minimal, classical style
@Value.Immutable
interface Book {
String isbn();
String title();
List<String> authors();
}
ImmutableBook book = ImmutableBook.builder()
.isbn("978-1-56619-909-4")
.title("The Elements of Style")
.addAuthors("William Strunk Jr.", "E.B. White.")
.build();"sandwich" style, with nested builder and extending With* interface
// Define abstract value type using interface, abstract class or annotation
@Value.Immutable
public interface ValueObject extends WithValueObject {
// WithValueObject is not yet generated, We extend With* to inherit `with*` method signatures
String name();
List<Integer> counts();
Optional<String> description();
class Builder extends ImmutableValueObject.Builder {}
// ImmutableValueObject.Builder will be generated and
// our builder will inherit and reexport methods as its own.
// Static nested Builder will inherit all the public method
// signatures of ImmutableValueObject.Builder
}
// Use generated immutable implementation and builder
var value = new ValueObject.Builder()
.name("Nameless")
.description("present")
.addCounts(1)
.addCounts(2)
.build();
value = value.withName("Doe");
//fetch values via accessors
List<Integer> counts = v.counts();
Optional<String> description = v.description();See releases tab for release history. Archived changelog for earlier releases.