forked from woowacourse-precourse/java-baseball-6
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseballNumber.java
More file actions
47 lines (38 loc) · 1.44 KB
/
BaseballNumber.java
File metadata and controls
47 lines (38 loc) · 1.44 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
package baseball.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class BaseballNumber {
public static final Integer BASEBALL_LENGTH = 3;
private final String REGEXP_PATTERN_INTEGER = String.format("[1-9]{%d}", BASEBALL_LENGTH);
private final List<Integer> numbers;
public BaseballNumber(String input) {
validate(input);
this.numbers = Stream.of(input.split("")).mapToInt(Integer::parseInt).boxed().toList();
}
private void validate(String input) {
isCorrectLength(input);
isInteger(input);
isDuplicate(input);
}
private void isCorrectLength(String input) {
if (input.length() != BASEBALL_LENGTH) {
throw new IllegalArgumentException("입력의 길이가 알맞지 않습니다.");
}
}
private void isInteger(String input) {
if (!Pattern.matches(REGEXP_PATTERN_INTEGER, input)) {
throw new IllegalArgumentException("입력이 정수로 이루어지지 않았습니다.");
}
}
private void isDuplicate(String input) {
if (Stream.of(input.split("")).collect(Collectors.toSet()).size() < BASEBALL_LENGTH) {
throw new IllegalArgumentException("입력 중 중복이 있습니다.");
}
}
public List<Integer> getNumbers() {
return new ArrayList<>(this.numbers);
}
}