Last active
August 29, 2015 13:56
Java, immutability and builders - PersonBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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