Crate generates a variant of the builder pattern, where each constructor parameter is represented by a separate object. Each of these "step" objects is fully immutable and represents a partial invocation of the constructor.
Put the @Crate
annotation on a class. The class must have a non-private constructor.
@Crate
class Animal {
final String name;
final boolean good;
Animal(String name, boolean good) {
this.name = name;
this.good = good;
}
}
A class Animal_Crate
will be generated in the same package.
You can use the static Animal_Crate.buider()
method to obtain an instance.
If you'd also like a "normal" builder for updating, you may want to combine this with readable.
The following example shows how to add the usual
builder
and toBuilder
methods:
@Readable
@Crate
final class Animal {
final String name;
final int numberOfLegs;
Animal(String name, int numberOfLegs) {
this.name = name;
this.numberOfLegs = numberOfLegs;
}
static Animal_Crate builder() {
return Animal_Crate.builder();
}
Animal_Builder toBuilder() {
return Animal_Builder.builder(this);
}
}
If you'd rather have Animal
implemented by auto-value,
you need to use the @AutoCrate
annotation instead.
@AutoCrate
@AutoValue
abstract class Animal {
abstract String name();
abstract int numberOfLegs();
static Animal_Crate builder() {
return Animal_Crate.builder();
}
}
<dependency>
<groupId>com.github.h908714124</groupId>
<artifactId>crate</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>