-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathLineRepository.java
More file actions
41 lines (32 loc) · 1.14 KB
/
Copy pathLineRepository.java
File metadata and controls
41 lines (32 loc) · 1.14 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
package subway.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import subway.message.ErrorMessage;
public class LineRepository {
private static final List<Line> lines = new ArrayList<>();
public static List<Line> lines() {
return Collections.unmodifiableList(lines);
}
public static void validateLineNameDuplicate(String lineName) throws IllegalArgumentException {
if (lineNameExists(lineName)) {
throw new IllegalArgumentException(
ErrorMessage.LINE_REPOSITORY_LINE_ALREADY_EXIST.toString()
);
}
}
private static boolean lineNameExists(String name) {
return lines.stream().anyMatch(line -> line.getName().equals(name));
}
public static void addLine(Line line) throws IllegalArgumentException {
validateLineNameDuplicate(line.getName());
lines.add(line);
}
public static boolean deleteLineByName(String name) {
return lines.removeIf(line -> Objects.equals(line.getName(), name));
}
public static void deleteAll() {
lines.clear();
}
}