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 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 ""; } // 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 = ""; 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(); } }