@@ -0,0 +1,67 @@
1+package app.arguments;
2+
3+import org.jetbrains.annotations.NotNull;
4+
5+import java.util.List;
6+import java.util.function.Predicate;
7+
8+public class SimpleParsable<Self> implements Parsable {
9+ @NotNull
10+ private SimpleParser<Self> parseValue;
11+
12+ public Self value;
13+
14+ private final @NotNull Predicate<@NotNull Self> predicate;
15+ private final Self def;
16+ private final @NotNull String message;
17+
18+ @NotNull
19+ @Override
20+ public Pair<String, List<String>> parse(@NotNull List<String> arguments) {
21+ Pair<@NotNull Self, @NotNull List<@NotNull String>> result = parseValue.apply(arguments);
22+
23+ if (result != null) {
24+ value = result.fst;
25+
26+ if (predicate.test(value)) {
27+ return new Pair<>(null, result.snd);
28+ }
29+ }
30+
31+ if (def != null) {
32+ value = def;
33+
34+ if (predicate.test(value)) {
35+ return new Pair<>(null, arguments);
36+ }
37+ }
38+
39+ return new Pair<>(message, arguments);
40+ }
41+
42+ public SimpleParsable(
43+ @NotNull SimpleParser<Self> parseValue,
44+ Self def,
45+ @NotNull Predicate<@NotNull Self> predicate,
46+ @NotNull String message) {
47+ this.parseValue = parseValue;
48+ this.predicate = predicate;
49+ this.def = def;
50+ this.message = message;
51+ }
52+
53+ public SimpleParsable(@NotNull SimpleParser<Self> parseValue,
54+ @NotNull Predicate<@NotNull Self> predicate,
55+ @NotNull String message) {
56+ this(parseValue, null, predicate, message);
57+ }
58+
59+
60+ public SimpleParsable(@NotNull SimpleParser<Self> parseValue, Self def, @NotNull String message) {
61+ this(parseValue, def, (i) -> true, message);
62+ }
63+
64+ public SimpleParsable(@NotNull SimpleParser<Self> parseValue, @NotNull String message) {
65+ this(parseValue, null, (i) -> true, message);
66+ }
67+}