forked from deephaven/report-generation-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.java
More file actions
365 lines (319 loc) · 9.8 KB
/
Functions.java
File metadata and controls
365 lines (319 loc) · 9.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
/*
* Copyright 2020 Deephaven Data Labs
*
* 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.
*/
package io.deephaven.plugins.report;
import java.lang.reflect.Array;
import java.time.Instant;
import java.util.Arrays;
/** A collection of static helper functions to aid in the creation of {@link Report reports}. */
public class Functions {
/**
* Returns the {@link io.deephaven.plugins.report.Functions.NonStatic} instance. Allows the caller
* to bring the scope into a variable.
*
* <p>{@code def reports = io.deephaven.plugins.report.Functions.nonStatic()}
*
* @return the non-static instance
*/
public static NonStatic nonStatic() {
return NonStatic.INSTANCE;
}
// --------------------------------------------------------------------------------------------
/**
* Equivalent to {@link Report#of(String, Item)}.
*
* @param title the title
* @param item the item
* @return the report
*/
public static Report report(String title, Item<?> item) {
return Report.of(title, item);
}
/**
* Equivalent to {@link Report#of(String, Item, Instant)}.
*
* @param title the title
* @param item the item
* @param timestamp the timestamp
* @return the report
*/
public static Report report(String title, Item<?> item, Instant timestamp) {
return Report.of(title, item, timestamp);
}
/**
* Equivalent to {@link Text#of(String)}.
*
* @param text the text
* @return the item
*/
public static Text text(String text) {
return Text.of(text);
}
/**
* Equivalent to {@link TableLocal#of(com.illumon.iris.db.tables.Table)}.
*
* @param table the table
* @return the table item
*/
public static TableLocal table(com.illumon.iris.db.tables.Table table) {
return TableLocal.of(table);
}
/**
* Equivalent to {@link TablePQ#of(PQ, String)}.
*
* @param pq the pq
* @param tableName the table name
* @return the table pq
*/
public static TablePQ table(PQ pq, String tableName) {
return TablePQ.of(pq, tableName);
}
/**
* Equivalent to {@link FigureLocal#of(com.illumon.iris.db.plot.Figure)}.
*
* @param figure the figure
* @return the figure item
*/
public static FigureLocal figure(com.illumon.iris.db.plot.Figure figure) {
return FigureLocal.of(figure);
}
/**
* Equivalent to {@link FigurePQ#of(PQ, String)}.
*
* @param pq the pq
* @param figureName the figure name
* @return the figure pq
*/
public static FigurePQ figure(PQ pq, String figureName) {
return FigurePQ.of(pq, figureName);
}
/**
* Equivalent to {@code Group.builder().addItems(items).build()}.
*
* @param items the items
* @return the group item
*/
public static Group group(Item<?>... items) {
return Group.builder().addItems(items).build();
}
/**
* Equivalent to {@link PQName#of(String, String)}.
*
* @param owner the pq owner
* @param name the pq name
* @return the pq
*/
public static PQName pq(String owner, String name) {
return PQName.of(owner, name);
}
/**
* Equivalent to {@link PQSerialId#of(long)}.
*
* @param serialId the serial id
* @return the pq
*/
public static PQSerialId pq(long serialId) {
return PQSerialId.of(serialId);
}
// --------------------------------------------------------------------------------------------
public static <T extends Item<?>> T item(T item) {
return item;
}
/**
* Coerces the array of objects into an {@link Group}, where each item is further coerced.
*
* @param objects the objects
* @return the group
*/
public static Group item(Object... objects) {
return item(Arrays.asList(objects));
}
/**
* Coerces the iterable of objects into an {@link Group}, where each item is further coerced.
*
* @param objects the objects
* @return the group
*/
public static Group item(Iterable<?> objects) {
final Group.Builder builder = Group.builder();
for (Object o : objects) {
builder.addItems(item(o));
}
return builder.build();
}
/**
* Coerces an object into an {@link Item}.
*
* <p>In order, if the {@code object} is an instance of:
*
* <ol>
* <li>an {@link Item}, the item is returned
* <li>a {@link CharSequence}, an appropriate {@link Text} is returned
* <li>a {@link com.illumon.iris.db.tables.Table}, an appropriate {@link TableLocal} is returned
* <li>a {@link com.illumon.iris.db.plot.Figure}, an appropriate {@link FigureLocal} is returned
* <li>an {@link Iterable} or array, a {@link Group} is returned where each item is further
* coerced
* </ol>
*
* If none of the above conditions are met, an {@link IllegalArgumentException} will be thrown.
*
* @param object the object to coerce
* @return the item
*/
public static Item<?> item(Object object) {
// note: Report is *not* an Item, and should not be adapted.
if (object instanceof Item) {
return (Item<?>) object;
}
if (object instanceof CharSequence) {
return text(((CharSequence) object).toString());
}
if (object instanceof com.illumon.iris.db.tables.Table) {
return table((com.illumon.iris.db.tables.Table) object);
}
if (object instanceof com.illumon.iris.db.plot.Figure) {
return figure((com.illumon.iris.db.plot.Figure) object);
}
if (object instanceof Iterable) {
return item((Iterable<?>) object);
}
if (object.getClass().isArray()) {
final Group.Builder builder = Group.builder();
final int L = Array.getLength(object);
for (int i = 0; i < L; ++i) {
builder.addItems(item(Array.get(object, i)));
}
return builder.build();
}
throw new IllegalArgumentException(
String.format(
"Object of type '%s' is not adaptable into a Report item", object.getClass()));
}
/**
* A coerced equivalent of {@link #report(String, Item)}.
*
* @param title the title
* @param item the item
* @return the report
*/
public static Report report(String title, Object item) {
return report(title, item(item));
}
/**
* A coerced equivalent of {@link #report(String, Item)}.
*
* @param title the title
* @param items the items
* @return the report
*/
public static Report report(String title, Object... items) {
return report(title, item(items));
}
/**
* Coerces the object and attaches a name.
*
* @param name the name to attach
* @param object the object
* @return the new item
*/
public static Item<?> named(String name, Object object) {
return item(object).withName(name);
}
/**
* Coerces the objects and attaches a name.
*
* @param name the name
* @param objects the objects
* @return the new item
*/
public static Item<?> named(String name, Object... objects) {
return item(objects).withName(name);
}
/**
* Coerces the objects and attaches an attribute.
*
* @param object the object
* @param key the attribute key
* @param value the attribute value
* @return the new item
*/
public static Item<?> attr(Object object, String key, Object value) {
return item(object).withAttribute(key, value);
}
// --------------------------------------------------------------------------------------------
/** Should not be instantiated. */
private Functions() {}
/** A wrapper that presents the static functions of {@link Functions} as non-static methods. */
public enum NonStatic {
INSTANCE;
public Report report(String title, Item<?> item) {
return Functions.report(title, item);
}
public Report report(String title, Item<?> item, Instant timestamp) {
return Functions.report(title, item, timestamp);
}
public Text text(String text) {
return Functions.text(text);
}
public TableLocal table(com.illumon.iris.db.tables.Table table) {
return Functions.table(table);
}
public TablePQ table(PQ pq, String tableName) throws Exception {
return Functions.table(pq, tableName);
}
public FigureLocal figure(com.illumon.iris.db.plot.Figure figure) {
return Functions.figure(figure);
}
public FigurePQ figure(PQ pq, String figureName) {
return Functions.figure(pq, figureName);
}
public Group group(Item<?>... items) {
return Functions.group(items);
}
public <T extends Item<?>> T item(T item) {
return Functions.item(item);
}
public Group item(Iterable<Object> items) {
return Functions.item(items);
}
public Group item(Object... objects) {
return Functions.item(objects);
}
public Item<?> item(Object object) {
return Functions.item(object);
}
public Report report(String name, Object item) {
return Functions.report(name, item);
}
public Report report(String name, Object... items) {
return Functions.report(name, items);
}
public Item<?> named(String name, Object item) {
return Functions.named(name, item);
}
public Item<?> named(String name, Object... items) {
return Functions.named(name, items);
}
public Item<?> attr(Object item, String key, Object value) {
return Functions.attr(item, key, value);
}
public PQName pq(String owner, String name) {
return Functions.pq(owner, name);
}
public PQSerialId pq(long serialId) {
return Functions.pq(serialId);
}
}
}