-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupValidator.java
More file actions
144 lines (120 loc) · 3.48 KB
/
Copy pathGroupValidator.java
File metadata and controls
144 lines (120 loc) · 3.48 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package io.github.elkin.commandline;
import io.github.elkin.commandline.exception.ValidationException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class GroupValidator implements Validator {
private final Set<Integer> activeGroupIds;
private final List<Group> groups;
private int groupId;
public GroupValidator() {
activeGroupIds = new HashSet<>();
// groupId = 0; not needed, by default
groups = new ArrayList<>();
}
public Group addGroup() {
return addGroup("");
}
public Group addGroup(String name) {
Objects.requireNonNull(name);
Group group = new Group(name, groupId++);
groups.add(group);
return group;
}
@Override
public void validate(List<Argument> arguments, List<Option> options, List<Flag> flags) {
assert arguments != null;
assert options != null;
assert flags != null;
if (activeGroupIds.size() > 1) {
StringBuilder result = new StringBuilder(
"Options/flags from different groups can't be used together:")
.append(System.lineSeparator());
for (Integer gid : activeGroupIds) {
Group group = groups.get(gid);
result.append(group);
result.append(System.lineSeparator());
}
throw new ValidationException(result.toString());
}
}
public class Group {
private final String name;
private final int id;
private final List<Option> options;
private final List<Flag> flags;
private Group(String name, int id) {
assert name != null;
assert id >= 0;
this.name = name;
this.id = id;
options = new ArrayList<>();
flags = new ArrayList<>();
}
public Group addFlag(Flag flag) {
Objects.requireNonNull(flag);
flag.setConsumer(
flag.consumer().andThen(f -> {
activeGroupIds.add(id);
flags.add(flag);
}));
return this;
}
public Group addFlags(Flag... flags) {
Objects.requireNonNull(flags);
for (Flag flag : flags) {
addFlag(flag);
}
return this;
}
public Group addOption(Option option) {
Objects.requireNonNull(option);
option.setConsumer(
option.consumer().andThen(o -> {
activeGroupIds.add(id);
options.add(option);
}));
return this;
}
public Group addOptions(Option... options) {
Objects.requireNonNull(options);
for (Option option : options) {
addOption(option);
}
return this;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("Group ");
if (name.isEmpty()) {
result.append(id);
} else {
result.append(name);
}
result.append(' ');
for (Iterator<Option> iter = options.iterator(); iter.hasNext(); ) {
Option option = iter.next();
String description = String.join("|", option.prefixes());
result.append(description);
if (iter.hasNext()) {
result.append(", ");
}
}
if (!flags.isEmpty()) {
result.append(", ");
}
for (Iterator<Flag> iter = flags.iterator(); iter.hasNext(); ) {
Flag flag = iter.next();
String description = String.join("|", flag.prefixes());
result.append(description);
if (iter.hasNext()) {
result.append(", ");
}
}
return result.toString();
}
}
}