Skip to content

Instantly share code, notes, and snippets.

@samuelcatalano-zz
Created November 28, 2018 19:40
Show Gist options
  • Save samuelcatalano-zz/f02a9272c766a188599e155fedfdf7f5 to your computer and use it in GitHub Desktop.
Save samuelcatalano-zz/f02a9272c766a188599e155fedfdf7f5 to your computer and use it in GitHub Desktop.
How to create a Builder
public class ConsumidorBuilder {
private Consumidor consumidor;
public ConsumidorBuilder() {
this.consumidor = new Consumidor();
}
public ConsumidorBuilder idExterno(String idExterno) {
this.consumidor.setIdExterno(idExterno);
return this;
}
public ConsumidorBuilder nome(String nome) {
this.consumidor.setNome(nome);
return this;
}
public ConsumidorBuilder pais(String pais) {
this.consumidor.setPais(pais);
return this;
}
public ConsumidorBuilder email(String email) {
this.consumidor.setEmail(email);
return this;
}
public ConsumidorBuilder telefones(List<String> telefones) {
this.consumidor.setTelefones(telefones);
return this;
}
public ConsumidorBuilder telefone(String telefone) {
if (this.consumidor.getTelefones().isEmpty()) {
this.consumidor.setTelefones(new ArrayList<>());
}
this.consumidor.getTelefones().add(telefone);
return this;
}
public ConsumidorBuilder documentos(List<Documento> documentos) {
if(consumidor.getDocumentos() == null){
consumidor.setDocumentos(new ArrayList<>());
}
this.consumidor.getDocumentos().addAll(documentos);
return this;
}
public ConsumidorBuilder documento(Documento documento) {
if(consumidor.getDocumentos() == null){
consumidor.setDocumentos(new ArrayList<>());
}
this.consumidor.getDocumentos().add(documento);
return this;
}
public ConsumidorBuilder nascimento(LocalDate nascimento) {
this.consumidor.setNascimento(nascimento.format(DateTimeFormatter.ISO_LOCAL_DATE));
return this;
}
public ConsumidorBuilder tipo(ConsumidorTipo tipo) {
this.consumidor.setTipo(tipo.code());
return this;
}
public Consumidor build() {
return consumidor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment