SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Report.java
1package umontreal.ssj.mcqmctools.anova;
2
3import java.util.*;
4
5public class Report {
6
7 protected static int labelWidth = 24;
8 protected static int indentation = 3;
9 protected static int floatPrecision = 4;
10 protected static int valueWidth = 0;
11
12 protected String label;
13 protected String value;
14 protected List<Report> subReports;
15
16 public static final String NEWLINE = System.getProperty("line.separator");
17
18 // static setup methods
19
25 public static void setLabelWidth(int labelWidth) {
26 Report.labelWidth = labelWidth;
27 }
28
29 public static void setIndentation(int indentation) {
30 Report.indentation = indentation;
31 }
32
33 public static void setFloatPrecision(int floatPrecision) {
34 Report.floatPrecision = floatPrecision;
35 }
36
42 public static void setValueWidth(int valueWidth) {
43 Report.valueWidth = valueWidth;
44 }
45
46 // constructors
47
48 public Report(String label, String value) {
49 this.label = label;
50 this.value = (value == null) ? null : String.format("%" + (valueWidth == 0 ? "" : valueWidth) + "s", value);
51 subReports = new ArrayList<Report>();
52 }
53
54 public Report(String label, int value) {
55 this(label, String.format("%d", value));
56 }
57
58 public Report(String label, double value) {
59 this(label, String.format("%." + floatPrecision + "g", value));
60 }
61
62 public Report(String label, int[] value) {
63 this(label, formatVector(value));
64 }
65
66 public Report(String label, double[] value) {
67 this(label, formatVector(value));
68 }
69
70 public Report(String label, boolean value) {
71 this(label, value ? "yes" : "no");
72 }
73
74 public Report(String label) {
75 this.label = label;
76 this.value = null;
77 subReports = new ArrayList<Report>();
78 }
79
80 public Report() {
81 this(null);
82 }
83
84 // add a sub-report
85
86 public void add(Report subReport) {
87 subReports.add(subReport);
88 }
89
90 // shortcut methods
91
92 public void add(String label, String value) {
93 add(new Report(label, value));
94 }
95
96 public void add(String label, int value) {
97 add(new Report(label, value));
98 }
99
100 public void add(String label, double value) {
101 add(new Report(label, value));
102 }
103
104 public void add(String label, boolean value) {
105 add(new Report(label, value));
106 }
107
108 public void add(String label, int[] value) {
109 add(new Report(label, value));
110 }
111
112 public void add(String label, double[] value) {
113 add(new Report(label, value));
114 }
115
116 public void add(String label) {
117 add(new Report(label));
118 }
119
120 // stringification methods
121
122 public String toString() {
123 return toString(0);
124 }
125
126 public String toString(int baseIndentation) {
127 String s = "";
128 if (label != null) {
129 if (baseIndentation > 0)
130 s += String.format("%" + baseIndentation + "s", "");
131 if (value != null)
132 s += String.format("%" + (labelWidth == 0 ? "" : labelWidth) + "s %s", label + ":", value);
133 else
134 s += "==> " + label;
135 s += NEWLINE;
136 }
137 if (!subReports.isEmpty()) {
138 for (Report report : subReports)
139 s += report.toString(baseIndentation + indentation);
140 }
141 return s;
142 }
143
144 protected static String formatVector(int[] value) {
145 String s = "";
146 for (int i = 0; i < value.length; i++) {
147 if (i > 0)
148 s += " ";
149 s += String.format("%d", value[i]);
150 }
151 return s;
152 }
153
154 protected static String formatVector(double[] value) {
155 String s = "";
156 for (int i = 0; i < value.length; i++) {
157 if (i > 0)
158 s += " ";
159 s += String.format("%." + floatPrecision + "g", value[i]);
160 }
161 return s;
162 }
163}
static void setLabelWidth(int labelWidth)
Set to <0 for left-aligned label with given width.
Definition Report.java:25
static void setValueWidth(int valueWidth)
Set to <0 for left-aligned value with given width.
Definition Report.java:42