-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLottoNumbers.java
More file actions
55 lines (44 loc) · 1.62 KB
/
LottoNumbers.java
File metadata and controls
55 lines (44 loc) · 1.62 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
package com.nextstep.camp.lotto.domain.vo;
import java.util.*;
import java.util.stream.Collectors;
import com.nextstep.camp.lotto.domain.exception.LottoNumberDuplicatedException;
import com.nextstep.camp.lotto.domain.exception.LottoNumbersSizeException;
public class LottoNumbers {
private final List<LottoNumber> numbers;
public static final int LOTTO_NUMBERS_SIZE = 6;
private LottoNumbers(List<Integer> rawNumbers) {
validate(rawNumbers);
this.numbers = rawNumbers.stream()
.map(LottoNumber::of)
.sorted(Comparator.comparingInt(LottoNumber::getValue))
.collect(Collectors.toList());
}
private static void validate(List<Integer> rawNumbers) {
if (rawNumbers.size() != LOTTO_NUMBERS_SIZE) {
throw new LottoNumbersSizeException();
}
if (new HashSet<>(rawNumbers).size() != LOTTO_NUMBERS_SIZE) {
throw new LottoNumberDuplicatedException();
}
}
public static LottoNumbers of(List<Integer> rawNumbers) {
return new LottoNumbers(rawNumbers);
}
public int countMatch(LottoNumbers winningNumbers) {
return (int) numbers.stream()
.filter(winningNumbers::contains)
.count();
}
public boolean contains(LottoNumber number) {
return numbers.contains(number);
}
public List<LottoNumber> getNumbers() {
return numbers;
}
@Override
public String toString() {
return numbers.stream()
.map(n -> String.valueOf(n.getValue()))
.collect(Collectors.joining(", ", "[", "]"));
}
}