-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathDataTable.java
More file actions
545 lines (502 loc) · 16.8 KB
/
Copy pathDataTable.java
File metadata and controls
545 lines (502 loc) · 16.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2019 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.annotations.Label;
import org.osgl.$;
import org.osgl.util.*;
import java.lang.reflect.*;
import java.util.*;
/**
* A `DataTable` contains a list of heading (column labels) plus
* a list of data rows.
* <p>
* The sequence of data fields in each data row matches the corresponding
* sequence of field labels in heading list.
* <p>
* **Note**
* 1. `DataTable` does data processing in memory, it is not suitable
* for huge amount of data.
* 2. `DataTable` does not suit big sparse matrix unless heading is
* provided with {@link PropertySpec} or the row data type is a concrete
* POJO. In case sparse matrix row is supplied with {@link Map} or
* {@link org.osgl.util.AdaptiveMap}, `DataTable` will try to probe at
* most 10 first rows to find out the heading fields(keys). Thus if
* the first 10 rows has missing columns, they will not be able to output
*/
public class DataTable implements Iterable {
public static final Keyword HTML_TABLE = Keyword.of("html-table");
public static final String KEY_FIELD = "_field";
/**
* A pseudo column key for the entire data object
* - used with data table of {@link org.osgl.Lang#isSimpleType(Class) simple types}.
*/
public static final String KEY_THIS = "_this_";
private List<String> colKeys;
private Iterable rows;
private int rowCount;
private boolean isMap;
private boolean isAdaptiveMap;
private boolean isTranspose;
private Object firstRow;
private Set<String> rightAlignCols;
// key: col key; val: label
private C.Map<String, String> labelLookup;
// key: label, val: col key
private C.Map<String, String> reverseLabelLookup;
// the transpose table of this table
private transient volatile DataTable transpose;
// used to build transpose table
private DataTable() {
}
/**
* Construct a `DataTable` with data.
* <p>
* This constructor will call `{@link ThreadLocal#get()}` on
* {@link PropertySpec#currentSpec} to get the current
* PropertySpec meta info.
*
* @param data the data used to populate the data table
*/
public DataTable(Object data) {
init(data, PropertySpec.currentSpec.get());
}
/**
* Construct a `DataTable` with data and {@link PropertySpec.MetaInfo column spec}.
*
* @param data the data used to populate the data table
* @param colSpec the {@link PropertySpec.MetaInfo} specifies column headings
*/
public DataTable(Object data, PropertySpec.MetaInfo colSpec) {
init(data, colSpec);
}
/**
* Return number of rows in the data table.
* <p>
* If {@link #rows} is an iterable and cannot be cast to a collection, then
* it returns `-1`, meaning row number cannot be count.
*
* @return the number of rows in the table
*/
public int rowCount() {
return rowCount;
}
/**
* Return number of columns in the data table.
*
* @return the number of columns.
*/
public int colCount() {
return colKeys.size();
}
/**
* Return {@link Iterator} of {@link #rows}.
*
* @return the row iterator.
*/
@Override
public Iterator iterator() {
return rows.iterator();
}
/**
* Get the heading row.
*
* @return the heading row
*/
public List<String> heading() {
if (isTranspose) {
return colKeys;
}
List<String> heading = new ArrayList<>();
List<String> colKeys = new ArrayList(this.colKeys);
if (colKeys.remove("id")) {
String idLabel = labelLookup.get("id");
if (null != idLabel) {
heading.add(idLabel);
} else {
heading.add("id");
}
}
for (String colKey : colKeys) {
String label = labelLookup.get(colKey);
heading.add(null != label ? label : colKey);
}
return heading;
}
/**
* Get column value on a row by col index.
*
* @param row the row object
* @param colIndex the column index
* @return the value of the column on the row
*/
public Object val(Object row, int colIndex) {
String colKey = colKeys.get(colIndex);
return $.getProperty(row, colKey);
}
/**
* Get column value on a row by label.
*
* @param row the row object
* @param label the column label
* @return the value of the column on the row
*/
public Object val(Object row, String label) {
if (KEY_THIS == label) {
return row;
}
String colKey = label;
if (!isTranspose) {
colKey = reverseLabelLookup.get(label);
if (null == colKey) {
colKey = label;
}
}
return $.getProperty(row, colKey);
}
/**
* Get a transpose table of this table.
*
* @return a transpose table of this table.
*/
public DataTable transpose() {
if (null == transpose) {
synchronized (this) {
if (null == transpose) {
transpose = buildTranspose();
}
}
}
return transpose;
}
/**
* Check if a label is right aligned column.
* @param label the label to check
* @return `true` if the column of the label should be right aligned.
*/
public boolean isRightAligned(String label) {
if (isTranspose) {
return false;
}
String key = reverseLabelLookup.get(label);
if (null == key) {
key = label;
}
return rightAlignCols.contains(key);
}
/**
* Reports if this data table is a transposed table.
*
* @return `true` if this table is transposed, or `false` otherwise.
*/
public boolean isTransposed() {
return isTranspose;
}
private DataTable buildTranspose() {
E.illegalArgumentIf(rowCount < 0, "transpose table not applied to table does not have row count.");
DataTable transpose = new DataTable();
transpose = isTranspose() ? buildReverseTranspose(transpose) : buildTranspose(transpose);
transpose.labelLookup = this.labelLookup;
transpose.reverseLabelLookup = this.reverseLabelLookup;
transpose.isTranspose = !this.isTranspose;
return transpose;
}
private DataTable buildReverseTranspose(DataTable target) {
target.colKeys = buildReverseTransposeColKeys();
target.rowCount = this.colKeys.size() - 1;
target.rows = buildReverseTransposeRows(target.rowCount);
return target;
}
private DataTable buildTranspose(DataTable target) {
target.colKeys = buildTransposeColKeys();
target.rowCount = this.colKeys.size();
target.rows = buildTransposeRows();
return target;
}
private List<String> buildReverseTransposeColKeys() {
List<String> colKeys = new ArrayList<>(this.rowCount);
for (Object row : rows) {
colKeys.add(S.string($.getProperty(row, KEY_FIELD)));
}
return colKeys;
}
private List<String> buildTransposeColKeys() {
List<String> colKeys = new ArrayList<>(this.rowCount + 1);
colKeys.add(KEY_FIELD);
for (int i = 0; i < this.rowCount; ++i) {
colKeys.add("_r" + i);
}
return colKeys;
}
private Map<String, Object> buildTransposeRow(String colKey) {
Map<String, Object> row = new HashMap<>(this.rowCount + 1);
String label = labelLookup.get(colKey);
if (null == label) {
label = colKey;
}
row.put(KEY_FIELD, label);
int rowId = 0;
for (Object data : this.rows) {
row.put("_r" + rowId++, $.getProperty(data, colKey));
}
return row;
}
private Iterable buildTransposeRows() {
List<Map<String, Object>> rows = new ArrayList<>();
for (String key : colKeys) {
rows.add(buildTransposeRow(key));
}
return rows;
}
private Iterable buildReverseTransposeRows(int targetRowCount) {
List<Map<String, Object>> rows = new ArrayList<>(targetRowCount);
for (int i = 0; i < targetRowCount; ++i) {
String key = "_r" + i;
rows.add(buildReverseTransposeRow(key));
}
return rows;
}
private Map<String, Object> buildReverseTransposeRow(String key) {
Map<String, Object> row = new HashMap<>();
for (Object data : this.rows) {
String targetKey = $.getProperty(data, KEY_FIELD);
Object targetVal = $.getProperty(data, key);
row.put(targetKey, targetVal);
}
return row;
}
private boolean isTranspose() {
return isTranspose;
}
private void init(Object data, PropertySpec.MetaInfo colSpec) {
initRightAlignCols();
initRows(data);
initHeading(data, colSpec);
}
private void initRightAlignCols() {
this.rightAlignCols = new HashSet<>();
}
private void initRows(Object data) {
if (data instanceof Collection) {
Collection<?> col = $.cast(data);
rowCount = col.size();
rows = col;
} else if (data instanceof Iterable) {
Iterable iterable = $.cast(data);
if (iterable.iterator() == iterable) {
initRows(C.list(iterable));
return;
}
firstRow = iterable.iterator().hasNext() ? iterable.iterator().next() : null;
rows = $.cast(data);
rowCount = -1;
} else {
rows = C.list(data);
rowCount = 1;
firstRow = data;
}
if (rowCount > 0) {
firstRow = rows.iterator().next();
}
if (null != firstRow) {
isMap = firstRow instanceof Map;
isAdaptiveMap = !isMap && firstRow instanceof AdaptiveMap;
}
}
// pre-condition - `initRows(Object)` method must be called to setup the context
// for exploring output fields in case heading is not specified with `PropertySpec`
private void initHeading(Object data, PropertySpec.MetaInfo colSpec) {
Set<String> excludes = C.Set();
boolean headingLoaded = false;
boolean colSpecPresented = null != colSpec;
if (colSpecPresented) {
ActContext<?> context = ActContext.Base.currentContext();
setLabelLookup(colSpec.labelMapping(context));
excludes = colSpec.excludedFields(context);
if ($.not(excludes)) {
colKeys = colSpec.outputFields(context);
if ($.bool(colKeys)) {
headingLoaded = true;
}
}
colSpecPresented = $.bool(colKeys) || $.bool(excludes);
}
if (null == labelLookup) {
setLabelLookup(C.<String, String>newMap());
}
// explore data rows to probe fields
E.illegalArgumentIf(0 == rowCount, "Unable to probe table heading: no data found");
int max = Math.min(rowCount, 10); // probe at most 10 rows of data for labels
Set<String> keys;
if (isPojo()) {
if (null == firstRow) {
firstRow = rows.iterator().next();
}
keys = keysOf(firstRow, colSpecPresented);
} else {
keys = new TreeSet<>();
if (max < 0) {
max = 10;
}
for (Object row : rows) {
if (--max < 0) break;
if (isMap) {
keys.addAll(keysOf((Map) row));
} else if (isAdaptiveMap) {
keys.addAll(keysOf(((AdaptiveMap) row).asMap()));
} else {
assert false;
}
}
}
if (!headingLoaded) {
keys.removeAll(excludes);
colKeys = C.list(keys);
}
}
private boolean isNumeric(Object v) {
if (null == v) {
return false;
}
if (v instanceof Number) {
return true;
}
if (v instanceof $.Var) {
v = (($.Var) v).get();
return isNumeric(v);
}
if (v instanceof Const) {
v = ((Const) v).get();
return isNumeric(v);
}
return false;
}
private Set<String> keysOf(Map<?, ?> row) {
Set<String> set = new HashSet<>();
for (Map.Entry entry: row.entrySet()) {
String key = S.string(entry.getKey());
set.add(key);
if (rightAlignCols.contains(key)) {
continue;
}
Object v = entry.getValue();
if (isNumeric(v)) {
rightAlignCols.add(key);
}
}
return set;
}
private boolean isNumeric(Class<?> type, Type genericType) {
if (isNumeric(type)) {
return true;
}
if ($.Var.class.isAssignableFrom(type) || Const.class.isAssignableFrom(type)) {
if (genericType instanceof ParameterizedType) {
ParameterizedType pt = $.cast(genericType);
Type[] tArgs = pt.getActualTypeArguments();
if (null != tArgs && tArgs.length > 0) {
Type t = tArgs[0];
if (t instanceof Class) {
return isNumeric((Class<?>) t);
}
}
}
}
return false;
}
private boolean isNumeric(Class<?> ft) {
if ($.isSimpleType(ft)) {
ft = $.wrapperClassOf(ft);
}
return Number.class.isAssignableFrom(ft);
}
private Set<String> keysOf(Object pojo, boolean propSpecPresented) {
Class<?> type = pojo.getClass();
Set<String> keys = propSpecPresented ? new LinkedHashSet<String>() : new TreeSet<String>();
if ($.isSimpleType(type)) {
keys.add(KEY_THIS);
return keys;
}
// Check all public fields
for (Field f : type.getFields()) {
if (Modifier.isStatic(f.getModifiers())) {
continue;
}
String fn = f.getName();
keys.add(fn);
if (!labelLookup.containsKey(fn)) {
Label label = f.getAnnotation(Label.class);
if (null != label) {
addLabelLookup(fn, label.value());
}
}
Class<?> ft = f.getType();
Type gft = f.getGenericType();
if (isNumeric(ft, gft)) {
rightAlignCols.add(fn);
}
}
// Check all getters
for (Method m : type.getMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
continue;
}
if (m.getParameterTypes().length > 0) {
continue;
}
if (m.getReturnType().equals(Void.class)) {
continue;
}
String mn = m.getName();
if (S.eq("getClass", mn)) {
continue;
}
if (mn.length() < 4 || !mn.startsWith("get") || !Character.isUpperCase(mn.charAt(3))) {
continue;
}
S.Buffer buf = S.buffer(Character.toLowerCase(mn.charAt(3)));
buf.a(mn.substring(4));
String key = buf.toString();
keys.add(key);
if (!labelLookup.containsKey(key)) {
Label label = m.getAnnotation(Label.class);
if (null != label) {
addLabelLookup(key, label.value());
}
}
Class<?> mt = m.getReturnType();
Type gmt = m.getGenericReturnType();
if (isNumeric(mt, gmt)) {
rightAlignCols.add(key);
}
}
return keys;
}
private boolean isPojo() {
return !isMap && !isAdaptiveMap;
}
private void setLabelLookup(Map<String, String> lookup) {
this.labelLookup = C.newMap(lookup);
this.reverseLabelLookup = this.labelLookup.flipped();
}
private void addLabelLookup(String colKey, String label) {
this.labelLookup.put(colKey, label);
this.reverseLabelLookup.put(label, colKey);
}
}