-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBranchPattern.java
More file actions
76 lines (63 loc) · 1.52 KB
/
BranchPattern.java
File metadata and controls
76 lines (63 loc) · 1.52 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
package org.docopt;
import static org.docopt.Python.in;
import static org.docopt.Python.join;
import static org.docopt.Python.list;
import java.util.List;
/**
* Branch/inner node of a pattern tree.
*/
abstract class BranchPattern extends Pattern {
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((children == null) ? 0 : children.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (getClass() != obj.getClass()) {
return false;
}
final BranchPattern other = (BranchPattern) obj;
if (children == null) {
if (other.children != null) {
return false;
}
}
else if (!children.equals(other.children)) {
return false;
}
return true;
}
private final List<Pattern> children;
public BranchPattern(final List<? extends Pattern> children) {
this.children = list(children);
}
@Override
public String toString() {
return String.format("%s(%s)", getClass().getSimpleName(),
children.isEmpty() ? "" : join(", ", children));
}
@Override
protected final List<Pattern> flat(final Class<?>... types) {
if (in(getClass(), types)) {
return list((Pattern) this);
}
// >>> return sum([child.flat(*types) for child in self.children], [])
{
final List<Pattern> result = list();
for (final Pattern child : children) {
result.addAll(child.flat(types));
}
return result;
}
}
public List<Pattern> getChildren() {
return children;
}
}