blob: c1c5c7b3a450b669a52ef50eacbeb79759249278 (
plain)
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
145
146
147
148
149
150
151
152
|
package bjc.utils.ioutils.format;
import java.io.*;
import java.util.*;
import bjc.utils.ioutils.ReportWriter;
/**
* Represents an enclosed group of decrees.
*
* This is used for all of the decrees that take another format string as part
* of their body.
*
* @author Ben Culkin
*/
public class GroupDecree implements Iterable<ClauseDecree>, Decree {
/**
* The decree that opened this group.
*/
public SimpleDecree opening;
/**
* The decree that closed this group.
*/
public SimpleDecree closing;
/**
* The clauses that make up the body of this group.
*/
public List<ClauseDecree> body;
/**
* Create a new group decree with no values.
*/
public GroupDecree() {
body = new ArrayList<>();
}
/**
* Create a new group decree with a given body.
*
* @param children
* The decrees that form the body of the group.
*/
public GroupDecree(ClauseDecree... children) {
this();
for (ClauseDecree child : children) body.add(child);
}
/**
* Create a new group decree with all of the fields filled out.
*
* @param opening
* The decree opening the group.
*
* @param closing
* The decree closing the group.
*
* @param children
* The decree making up the body of the group.
*/
public GroupDecree(SimpleDecree opening, SimpleDecree closing, ClauseDecree... children) {
this(children);
this.opening = opening;
this.closing = closing;
}
/**
* Add a decree to this group.
*
* @param child
* The decree to add to the group.
*/
public void addChild(ClauseDecree child) {
body.add(child);
}
/**
* Get the first clause from the group.
*
* @return The first clause in the group
*/
public ClauseDecree clause() {
return body.get(0);
}
/**
* Get a specific clause from the group.
*
* @param idx
* The index of the clause to get.
*
* @return The clause at that index.
*/
public ClauseDecree clause(int idx) {
return body.get(idx);
}
/**
* Get the body of the first clause.
*
* @return The decrees that make up the body of the first clause.
*/
public List<SimpleDecree> unwrap() {
return body.get(0).body;
}
@Override
public String toString() {
try (ReportWriter writer = new ReportWriter()) {
String open = "<null>";
String close = "<null>";
if (opening != null) open = opening.toString();
if (closing != null) close = closing.toString();
writer.writef("GroupDecree (opening %s) (closing %s)\n", open, close);
writer.indent();
int idx = 0;
for (ClauseDecree clause : body) {
writer.writef("Clause %d:", idx++);
clause.toReportWriter(writer);
}
writer.dedent();
return writer.toString();
} catch (IOException ioex) {
return "<IOEXCEPTION>";
}
// return String.format("GroupDecree [opening=%s, closing=%s, body=%s]",
// opening, closing, body);
}
@Override
public Iterator<ClauseDecree> iterator() {
return body.iterator();
}
/**
* Get the number of clauses in this group.
*
* @return The number of clauses in the group.
*/
public int size() {
return body.size();
}
}
|