summaryrefslogtreecommitdiff
path: root/clformat/src/main/java/bjc/utils/ioutils/format/ClauseDecree.java
blob: db38ecae11b08918ef14d04dd9dd33e047a1de45 (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
package bjc.utils.ioutils.format;

import java.io.*;
import java.util.*;

import bjc.utils.ioutils.ReportWriter;

/**
 * A decree that represents a single clause in a {@link GroupDecree}.
 *
 * Has a list of decrees for a body, and then a single decree as the
 * 'terminator' if this was a terminated clause.
 *
 * @author Ben Culkin
 */
public class ClauseDecree implements Decree {
	/**
	 * The decrees that make up the body of this clause.
	 */
	public List<SimpleDecree> body;

	/**
	 * The decree that terminated this clause.
	 */
	public SimpleDecree terminator;

	/**
	 * Create a new blank clause decree.
	 *
	 */
	public ClauseDecree() {
		body = new ArrayList<>();
	}

	/**
	 * Create a new clause decree with specific contents.
	 *
	 * @param children
	 *                 The decrees to form the body of the clause.
	 */
	public ClauseDecree(SimpleDecree... children) {
		this();

		for (SimpleDecree child : children) body.add(child);
	}

	/**
	 * Create a new clause with both a body and a terminator.
	 *
	 * @param term
	 *                 The decree that terminates the clause.
	 *
	 * @param children
	 *                 The decrees that form the body of the clause.
	 */
	public ClauseDecree(SimpleDecree term, SimpleDecree... children) {
		this(children);

		this.terminator = term;
	}

	/**
	 * Add a decree to this clause.
	 *
	 * @param child
	 *              The decree to add to this clause.
	 */
	public void addChild(SimpleDecree child) {
		body.add(child);
	}

	@Override
	public String toString() {
		try (ReportWriter writer = new ReportWriter()) {
			toReportWriter(writer);

			return writer.toString();
		} catch (IOException ioex) {
			return "<IOEXCEPTION>";
		}
		// return String.format("ClauseDecree [body=%s, terminator=%s]", body,
		// terminator);
	}
	
	/**
	 * Write the string version of this decree to a report writer.
	 * @param writer The report write to write to.
	 * @throws IOException If something goes wrong
	 */
	public void toReportWriter(ReportWriter writer) throws IOException {
		String term = "<null>";
		if (terminator != null) term = terminator.toString();

		writer.writef("ClauseDecree (terminator %s)", term);
		writer.indent();
		writer.write("\n");

		int idx = 0;
		for (SimpleDecree kid : body) 
			writer.writef("Child %d: %s\n", idx, kid.toString());

		writer.dedent();
	}
}