-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEither.java
More file actions
49 lines (39 loc) · 1.1 KB
/
Either.java
File metadata and controls
49 lines (39 loc) · 1.1 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
package org.docopt;
import static org.docopt.Python.list;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
final class Either extends BranchPattern {
public Either(final List<? extends Pattern> children) {
super(children);
}
@Override
protected MatchResult match(final List<LeafPattern> left,
List<LeafPattern> collected) {
if (collected == null) {
collected = list();
}
final List<MatchResult> outcomes = list();
for (final Pattern pattern : getChildren()) {
final MatchResult m = pattern.match(left, collected);
if (m.matched()) {
outcomes.add(m);
}
}
if (!outcomes.isEmpty()) {
// >>> return min(outcomes, key=lambda outcome: len(outcome[1]))
{
return Collections.min(outcomes, new Comparator<MatchResult>() {
@Override
public int compare(final MatchResult o1,
final MatchResult o2) {
final Integer s1 = Integer.valueOf(o1.getLeft().size());
final Integer s2 = Integer.valueOf(o2.getLeft().size());
return s1.compareTo(s2);
}
});
}
}
return new MatchResult(false, left, collected);
}
}