forked from woowacourse/java-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.java
More file actions
48 lines (40 loc) · 1.68 KB
/
Copy pathCommandLine.java
File metadata and controls
48 lines (40 loc) · 1.68 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
package chess.controller;
import java.util.List;
public class CommandLine {
private static final String START = "start";
private static final String MOVE = "move";
private static final String STATUS = "status";
private static final String END = "end";
private static final int ZERO_ARGUMENT_SIZE = 1;
private static final int TWO_ARGUMENT_SIZE = 3;
private static final int SOURCE_INDEX = 1;
private static final int TARGET_INDEX = 3;
private static final int MAIN_COMMAND_INDEX = 0;
private static final List<String> VALID_COMMANDS = List.of(START, MOVE, END, STATUS);
private final List<String> tokens;
public CommandLine(final List<String> tokens) {
this.validate(tokens);
this.tokens = tokens;
}
private void validate(final List<String> tokens) {
String command = tokens.get(MAIN_COMMAND_INDEX);
if (!VALID_COMMANDS.contains(command)) {
throw new IllegalArgumentException("잘못된 명령어입니다.");
}
if (!command.equals(MOVE) && tokens.size() != ZERO_ARGUMENT_SIZE) {
throw new IllegalArgumentException(command + "는 인자를 입력할 수 없습니다.");
}
if (command.equals(MOVE) && tokens.size() != TWO_ARGUMENT_SIZE) {
throw new IllegalArgumentException(command + "는 인자를 2개만 가질 수 있습니다.");
}
}
public String getCommand() {
return this.tokens.get(MAIN_COMMAND_INDEX);
}
public List<String> getArguments() {
if (this.tokens.size() != TWO_ARGUMENT_SIZE) {
return List.of();
}
return this.tokens.subList(SOURCE_INDEX, TARGET_INDEX);
}
}