Skip to content

Instantly share code, notes, and snippets.

@robbypelssers
Last active August 29, 2015 13:56
Java, immutability and builders - PersonBuilder.java
package com.pelssers.domain;
public class PersonBuilder {
private String firstName;
private String lastName;
/**
* @param person the source person
* @return a new personbuilder object with all properties from person copied
*/
public static PersonBuilder from(Person person) {
return
new PersonBuilder()
.withFirstName(person.getFirstName())
.withLastName(person.getLastName());
}
public Person build() {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
return person;
}
public PersonBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public PersonBuilder withLastName(String lastName) {
this.lastName = lastName;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment