-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLIUtils.java
67 lines (60 loc) · 2.58 KB
/
CLIUtils.java
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
import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.ParameterException;
import java.util.List;
public class CLIUtils {
public static <T> T getLast(List<T> list) {
return list.get(list.size() - 1);
}
public static class GradebookFileNameValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
if (!value.matches("[a-zA-Z_.]+")) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
public static class KeyValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
if (!value.matches("[a-fA-F0-9]{64}")) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
public static class AssignmentNameValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
if (!value.matches("[a-zA-Z0-9]+")) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
public static class NonNegativeIntegerValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
try {
int n = Integer.parseInt(value);
if (n < 0) {
throw new ParameterException("Parameter " + name + " is invalid");
}
} catch (NumberFormatException ignored) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
public static class AssignmentWeightValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
try {
double n = Double.parseDouble(value);
if (!(n >= 0.0 && n <= 1.0)) {
throw new ParameterException("Parameter " + name + " is invalid");
}
} catch (NumberFormatException ignored) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
public static class StudentNameValidator implements IParameterValidator {
public void validate(String name, String value) throws ParameterException {
if (!value.matches("[a-zA-Z]+")) {
throw new ParameterException("Parameter " + name + " is invalid");
}
}
}
}