-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathParticipants.java
45 lines (35 loc) · 1.44 KB
/
Participants.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
package domain;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
public record Participants(List<Participant> participants) {
public Participants {
validateParticipants(participants);
}
public static Participants fromPeople(List<Person> people) {
List<Participant> participants = IntStream.range(0, people.size())
.mapToObj(i -> new Participant(people.get(i), i))
.toList();
return new Participants(participants);
}
private void validateParticipants(List<Participant> participants) {
int participantsCount = participants.size();
Set<Participant> deduplicateParticipants = Set.copyOf(participants);
int deduplicateParticipantsCount = deduplicateParticipants.size();
if (participantsCount != deduplicateParticipantsCount) {
throw new IllegalArgumentException("중복된 이름의 참여자는 참여할 수 없습니다.");
}
}
public int getParticipantsCount() {
return participants.size();
}
public Participant getParticipantByIndex(int index) {
return participants.get(index);
}
public Participant getParticipantByName(Person person) {
return participants.stream()
.filter(participant -> participant.getParticipantName().equals(person.name()))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}