-
Notifications
You must be signed in to change notification settings - Fork 7
/
User.java
111 lines (85 loc) · 2.18 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.example.producer;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class User {
private String name = "";
private String region = "";
private final UUID uuid;
private UserState state = UserState.INITIALIZED;
private List<DomainEvent> changes = new ArrayList<>();
public User(UUID uuid, String region) {
this.uuid = uuid;
this.region = region;
}
public UUID getUuid() {
return uuid;
}
public String getName() {
return name;
}
public String getRegion() {
return region;
}
public List<DomainEvent> getChanges() {
return ImmutableList.copyOf(changes);
}
enum UserState {
INITIALIZED, CREATED, ACTIVATED, DEACTIVATED
}
void create() { //behavior
if (isCreated()) {
throw new IllegalStateException();
}
userCreated(new UserCreated(getUuid(), getRegion(), new Date()));
}
private User userCreated(UserCreated userCreated) {
state = UserState.CREATED; //state transition
changes.add(userCreated);
return this;
}
void activate() {
if (isActivated()) {
throw new IllegalStateException();
}
userActivated(new UserActivated(new Date()));
}
private User userActivated(UserActivated userActivated) {
state = UserState.ACTIVATED; //state transition
changes.add(userActivated);
return this;
}
void deactivate() {
if (isDeactivated()) {
throw new IllegalStateException();
}
userDeactivated(new UserDeactivated(new Date()));
}
private User userDeactivated(UserDeactivated userDeactivated) {
state = UserState.DEACTIVATED; //state transition
changes.add(userDeactivated);
return this;
}
void changeNameTo(String name) {
if (isDeactivated()) {
throw new IllegalStateException();
}
userNameChanged(new UserNameChanged(name, new Date()));
}
private User userNameChanged(UserNameChanged userNameChanged) {
name = userNameChanged.getNewName();
changes.add(userNameChanged);
return this;
}
boolean isCreated() {
return state.equals(UserState.CREATED);
}
boolean isActivated() {
return state.equals(UserState.ACTIVATED);
}
boolean isDeactivated() {
return state.equals(UserState.DEACTIVATED);
}
}