-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathErrorHandler.java
More file actions
78 lines (65 loc) · 2.83 KB
/
Copy pathErrorHandler.java
File metadata and controls
78 lines (65 loc) · 2.83 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package subway.controller;
import subway.domain.Line;
import subway.domain.LineRepository;
import subway.domain.Station;
import subway.domain.StationRepository;
import java.util.Objects;
import java.util.regex.Pattern;
public class ErrorHandler {
private static final String STATION_NAME_END = "역";
private static final String LINE_NAME_END = "선";
private static final int NAME_MIN_LENGTH = 2;
private static final int LENGTH_TO_INDEX = 1;
public static void validateStationNameFormat(String inputData) {
if (inputData.length() < NAME_MIN_LENGTH
|| !Pattern.matches("^[0-9가-힣]*$", inputData)
|| !inputData.substring(inputData.length() - LENGTH_TO_INDEX).equals(STATION_NAME_END)) {
throw new IllegalArgumentException("format error");
}
}
public static void validateStationDuplicate(String inputData) {
if (StationRepository.isDuplicate(inputData)) {
throw new IllegalArgumentException("duplicate error");
}
}
public static void validateStationRegistered(String inputData) {
if (!StationRepository.isDuplicate(inputData)) {
throw new IllegalArgumentException("not registered error");
}
}
public static void validateLineNameFormat(String inputData) {
if (inputData.length() < NAME_MIN_LENGTH
|| !Pattern.matches("^[0-9가-힣]*$", inputData)
|| !inputData.substring(inputData.length() - LENGTH_TO_INDEX).equals(LINE_NAME_END)) {
throw new IllegalArgumentException("format error");
}
}
public static void validateLineDuplicate(String inputData) {
if (LineRepository.isDuplicate(inputData)) {
throw new IllegalArgumentException("duplicate error");
}
}
public static void validateLineRegistered(String inputData) {
if (!LineRepository.isDuplicate(inputData)) {
throw new IllegalArgumentException("not registered error");
}
}
public static void validateStationDuplicateInLine(Station targetStation, Line line) {
if (line.getSections().stream()
.anyMatch(station -> Objects.equals(station, targetStation))) {
throw new IllegalArgumentException("duplicate error");
}
}
public static void validateStationRegisteredInLine(Station targetStation, Line line) {
if (line.getSections().stream()
.noneMatch(station -> Objects.equals(station, targetStation))) {
throw new IllegalArgumentException("not registered error");
}
}
public static void validateOrder(String targetOrder, Line line) {
int order = Integer.parseInt(targetOrder);
if (order < 1 || order > line.getSections().size()) {
throw new IllegalArgumentException("inbound error");
}
}
}