-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerson.java
More file actions
96 lines (75 loc) · 2.37 KB
/
Person.java
File metadata and controls
96 lines (75 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package example.builder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
public class Person {
private String firstName;
private String middleName;
private String lastName;
private int age;
// default constructor so the builder can create an empty version
private Person() {
}
private Person(Person person) {
this.firstName = person.getFirstName();
this.middleName = person.getMiddleName();
this.lastName = person.getLastName();
this.age = person.getAge();
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public static Builder getBuilder() {
return new Builder();
}
public static final class Builder {
private final Person person = new Person();
public Builder firstName(String firstName) {
this.person.firstName = firstName;
return this;
}
public Builder middleName(String middleName) {
this.person.middleName = middleName;
return this;
}
public Builder lastName(String lastName) {
this.person.lastName = lastName;
return this;
}
public Builder age(int age) {
this.person.age = age;
return this;
}
public Builder name(Name name) {
this.person.firstName = name.getFirstName();
this.person.middleName = name.getMiddleName();
this.person.lastName = name.getLastName();
return this;
}
//a new person is created here so that if a method in this instance of the Builder is called it will not change the value of the Person being returned.
public Person build() {
return new Person(person);
}
}
}