Flu(ent) (Buil)der generated by an annotation processor.
This force to build an object by giving the needed data in specific order.
See https://dzone.com/articles/fluent-builder-pattern
Mark a class for wich you want a fluent builder to be generated.
Change the generated builder class name. Empty actually use the target class name appending 'Builder' to it. Remember that's a class name, the first letter will be upper case. You can use '$' that will be replaced by the target class name.
Change the generated creator class name. Empty actually use the target class name appending 'Creator' to it. Remember that's a class name, the first letter will be upper case. You can use '$' that will be replaced by the target class name.
For the chaining, FluderProcessor generating multiple intermediate interfaces. This can put a prefix behind these interfaces. Remember that's an interface name, the first letter will be upper case.
Change the finisher 'build()' call to your own.
Remember that's a method name, the first letter will be lower case.
Change the initial 'getInstance' call to your own. Remember that's a method name, the first letter will be lower case.
Not already implemented
Ignore the field
Rename the setter, setContent can be transformed to setBody with @Name("body") on the 'content' field.
An @Optional field can be defined in any order (after the required fields). You can give a default value via @Optional or the one defined at the field initialisation will be kept.
Change/force the definition order of the fields. If you use it, you must specify it for each fields.
Transient/final fields are ignored.
The builder use reflection API to build a private field
When you make a builder, you generally hide the possibility to instanciate the target object without this Builder. Then the private constructor is accessed via the reflection API.
The builder can handle some javax.validation annotations
This will add a check in the setter to ensure that it's a non nullable value that will be set. It throws an IllegalArgumentException when passing a null value when building.
@Override
public EmailFrom setTo(java.lang.String in){
if(in==null){
throw new IllegalArgumentException("setTo can't be called with a null parameter. The target field is marked as @Nonnull");
}
this.to = in;
return this;
}
This will add a check in the setter to ensure that it's a non empty value that will be set. It throws an IllegalArgumentException when passing an empty value when building.
@Override
public EmailFrom setTo(java.lang.String in){
if(in==null){
throw new IllegalArgumentException("setTo can't be called with a null parameter. The target field is marked as @Nonnull");
}
this.to = in;
return this;
}
This Email
@Buildable
public class Email {
@Nonnull
@Order(0)
String to;
@NotNull
@Order(2)
String subject;
@Order(3)
@Name("body")
String content;
@Optional("\"bcc-default\"")
String bcc;
@Optional
String cc;
@Order(1)
private String from;
@Ignore
private Date date;
transient Object notInBuilder;
//Hide me forcing the builder usage
private Email(){
super();
}
}
Can now be created like this
Email email = EmailBuilder.getInstance()
.setTo("to")
.setFrom("from")
.setSubject("subject")
.setBody("content")
//.setBcc("bcc")
//.setCc("cc")
.build();