-
Notifications
You must be signed in to change notification settings - Fork 311
/
User.java
69 lines (61 loc) · 1.44 KB
/
User.java
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
public class User {
/**
* Fields
*/
private String firstName;
private String lastName;
private int age;
/**
* Constructor with Builder
* It will call make() and create default Constructor
* @param builder builder
*/
private User(final Builder builder) {
firstName = builder.firstName;
lastName = builder.lastName;
age = builder.age;
}
/**
* It will pass all three args and also check
* validation for firstName as defined create()
* @param firstName firstName
* @param lastName lastName
* @param age age
*/
private User(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/**
* Builder class to call it directly.
*/
public static class Builder {
private String firstName;
private String lastName;
private int age;
public Builder setFirstName(final String firstName) {
this.firstName = firstName;
return this;
}
public Builder setLastName(final String lastName) {
this.lastName = lastName;
return this;
}
public Builder setAge(final int age) {
this.age = age;
return this;
}
public User make() {
return new User(this);
}
public User create() {
User user = new User(firstName, lastName, age);
if (user.firstName.isEmpty()) {
throw new IllegalStateException(
"First name can not be empty!");
}
return user;
}
}
}