-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLottoNumbers.java
More file actions
51 lines (42 loc) · 1.73 KB
/
LottoNumbers.java
File metadata and controls
51 lines (42 loc) · 1.73 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
package step2.domain;
import java.util.*;
import java.util.stream.Collectors;
import static step2.domain.LottoNumberGenerator.NUMBER_COUNT;
public class LottoNumbers {
private final List<LottoNumber> numbers;
public LottoNumbers(List<LottoNumber> numbers) {
validate(numbers);
this.numbers = Collections.unmodifiableList(
numbers.stream().sorted().collect(Collectors.toList()));
}
public static LottoNumbers from(List<Integer> numbers) {
List<LottoNumber> lottoNumbers = numbers.stream()
.map(LottoNumber::from)
.collect(Collectors.toList());
return new LottoNumbers(lottoNumbers);
}
public static LottoNumbers fromText(String text) {
List<Integer> numbers = Arrays.stream(text.split(","))
.map(String::trim)
.map(Integer::parseInt)
.collect(Collectors.toList());
return from(numbers);
}
private void validate(List<LottoNumber> numbers) {
if (numbers == null || numbers.size() != NUMBER_COUNT) {
throw new IllegalArgumentException("숫자가 빈값이거나, 개수가 맞지 않습니다.");
}
Set<LottoNumber> unique = new HashSet<>(numbers);
if (unique.size() != numbers.size()) {
throw new IllegalArgumentException("중복된 숫자가 존재합니다.");
}
}
public List<LottoNumber> numbers() {
return numbers;
}
public LottoRank lottoRank(LottoNumbers winningNumbers) {
Set<LottoNumber> winningSet = new HashSet<>(winningNumbers.numbers());
long matchCount = numbers.stream().filter(winningSet::contains).count();
return LottoRank.fromMatch((int) matchCount);
}
}