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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 | package Torello.Browser;
import static Torello.Browser.CDPTypes.*;
import Torello.Java.StrCSV;
import Torello.Java.StrPrint;
import Torello.Java.UnreachableError;
import Torello.Java.Function.IntTFunction;
import Torello.Java.Function.IntIntTFunc;
class ValSwitch
{
private static final IntTFunction<String, String> STR_QUOTES =
(int a, String s) -> '\"' + s + '\"';
private static final IntIntTFunc<String, String> STR_QUOTES2 =
(int a, int b, String s) -> '\"' + s + '\"';
static void print(
final ParamRec rec,
final Object o,
final byte ctab
)
{
boolean is2DArray = false;
final String s;
// This switch operates on CTAB ("Computed Type As Byte") constants.
//
// The intent is to avoid repeated reflection, repeated instanceof-chains,
// and repeated String-based type comparisons during recursive formatting.
//
// Nearly all printable CDP value categories reduce to one of these compact
// byte constants.
switch (ctab)
{
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// One of the fields inside the class is, itself, another CDP class
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// A "Nested" CDP-Type
case CDP_TYPE:
rec.sb.append(CDPToString.toString(new ParamRec((BaseType) o, rec)));
return;
// A Nested array of one particular CDP-Type
case CDP_TYPE_ARRAY_1D:
@SuppressWarnings({"rawtypes", "unchecked"})
final BaseType[] btArr = (BaseType<?>[]) o;
CDP_TYPE_ARRAY_1D(rec, btArr);
return;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Uber-Simple Types
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case PRIMITIVE_BOOLEAN:
case BOXED_BOOLEAN:
case PRIMITIVE_INT:
case BOXED_INTEGER:
case NUMBER:
s = o.toString();
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Basic Arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case INT_ARRAY_1D:
// toCSV(int[] arr, BiIntFunction<String> toString, Integer maxLength)
s = '[' + StrCSV.toCSV((int[]) o, null, null) + ']';
break;
case BOOLEAN_ARRAY_1D:
// toCSV(boolean[] arr, IntBoolFunction<String> toString, Integer maxLength)
s = '[' + StrCSV.toCSV((boolean[]) o, null, null) + ']';
break;
case NUMBER_ARRAY_1D:
// toCSV(T[] tArr, boolean trim, boolean printNulls, Integer maxLength)
s = '[' + StrCSV.toCSV((Number[]) o, true, true, null) + ']';
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Basic 2-D Arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case INT_ARRAY_2D:
// toCSV(int[][] arr, TriIntFunc<String> toString, IntPredicate keepRow,
// boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
s = StrCSV.toCSV(
(int[][]) o, // int[][] arr
null, // TriIntFunc<String> toString
null, // IntPredicate keepRow
true, // boolean separateLines
rec.MAX_LINE_LEN, // Integer maxLengthInner
8 // Integer maxLengthOuter
);
is2DArray = true;
break;
case BOOLEAN_ARRAY_2D:
// toCSV(boolean[][] arr, IntIntBoolFunc<String> toString, IntPredicate keepRow,
// boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
s = StrCSV.toCSV(
(boolean[][]) o, // boolean[][] arr
null, // IntIntBoolFunc<String> toString
null, // IntPredicate keepRow
true, // boolean separateLines
rec.MAX_LINE_LEN, // Integer maxLengthInner
8 // Integer maxLengthOuter
);
is2DArray = true;
break;
case NUMBER_ARRAY_2D :
// toCSV(T[][] tArr, IntIntTFunc<? super T,String> toString, IntPredicate keepRow,
// boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
s = StrCSV.toCSV(
(Object[][]) o, // T[][] tArr
null, // IntIntTFunc<? super T,String> toString
null, // IntPredicate keepRow
true, // boolean separateLines
rec.MAX_LINE_LEN, // Integer maxLengthInner
8 // Integer maxLengthOuter
);
is2DArray = true;
break;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// String & String-Arrays
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
case STRING:
s = '\"' + o.toString() + '\"';
break;
case STRING_ARRAY_1D:
// toCSV(T[] tArr, IntTFunction<? super T,String> toString, boolean printNulls,
// Integer maxLength)
s = '[' + StrCSV.toCSV((String[]) o, STR_QUOTES, true, null) + ']';
break;
case STRING_ARRAY_2D:
// toCSV(T[][] tArr, IntIntTFunc<? super T,String> toString, IntPredicate keepRow,
// boolean separateLines, Integer maxLengthInner, Integer maxLengthOuter)
s = StrCSV.toCSV(
(String[][]) o, // T[][] tArr
STR_QUOTES2, // IntIntTFunc<? super T,String> toString
null, // IntPredicate keepRow
true, // boolean separateLines
rec.MAX_LINE_LEN, // Integer maxLengthInner,
8 // Integer maxLengthOuter
);
is2DArray = true;
break;
case RAW_JSON_VALUE:
s = o.toString();
break;
default: throw new UnreachableError();
}
final String ctas = '(' + CDPTypes.types.get(ctab) + ')';
if (is2DArray) rec.sb.append(ctas + s);
else
{
// abbrev(String s, int abbrevPos, boolean escapeNewLines, String abbrevStr,
// int maxLength)
final String notTooMuch = StrPrint.abbrev(
s + ' ' + ctas, // String s
500, // int abbrevPos
true, // boolean escapeNewLines
" ... [Shortened for Brevity] ... ", // String abbrevStr
1000 // int maxLength
);
rec.sb
.append(
// wrapToIndentationPlus(String s, int firstLineLen, int lineLen, int extraSpaces)
StrPrint.wrapToIndentationPlus(
notTooMuch, // s
rec.VAL_PRINT_LEN, // firstLineLen
rec.MAX_LINE_LEN, // lineLen
rec.CURRENT_INDENT + 4 // extraSpaces
))
.append('\n');
}
}
// NOTE: This is not that efficient (creating a new ParamRec for each value in the array)
// BUT: There are very few of 'BaseType[]' that exist in CDP, and they are rarely used.
private static void CDP_TYPE_ARRAY_1D(final ParamRec rec, final BaseType<?>[] btArr)
{
rec.sb.append("Array of size " + btArr.length + ":\n");
int i = 0;
for (final BaseType<?> bt : btArr)
rec.sb.append(
rec.INDENT + "arr[" + (i++) + "] " +
CDPToString.toString(new ParamRec(bt, rec))
);
}
}
|