-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTables.java
More file actions
477 lines (400 loc) · 11.7 KB
/
Tables.java
File metadata and controls
477 lines (400 loc) · 11.7 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
/*
* #%L
* Table structures for SciJava.
* %%
* Copyright (C) 2012 - 2022 Board of Regents of the University of
* Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.table;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Utility methods for constructing tables.
*
* @author Curtis Rueden
*/
public final class Tables {
private Tables() {
// NB: Prevent instantiation of utility class.
}
/**
* Creates a single-column table wrapping a map. Each entry is one row of the
* table.
*
* @param data The data to wrap. Each map entry is a row.
* @param colHeader Header to use for the table's sole column. Pass null for
* no column header.
* @param <T> The type of data in each cell of the table.
* @return A {@link Table} object wrapping the data structure.
*/
public static <T> Table<Column<T>, T> wrap(final Map<?, T> data,
final String colHeader)
{
final List<String> rowHeaders = data.keySet().stream() //
.map(k -> k == null ? null : k.toString()) //
.collect(Collectors.toList());
return new ReadOnlyTable<T>() {
@Override
public int getRowCount() {
return data.size();
}
@Override
public String getRowHeader(final int row) {
return rowHeaders.get(row);
}
@Override
public int size() {
return 1;
}
@Override
public Column<T> get(final int col) {
if (col != 0) //
throw new IllegalArgumentException("Column out of range: " + col);
return new ColumnAccessor<T>(data, colHeader) {
@Override
public int size() {
return data.size();
}
@Override
public T get(final int index) {
return data.get(rowHeaders.get(index));
}
};
}
};
}
/**
* Creates a single-column table wrapping a list. Each element is one row of
* the table.
*
* @param data The data to wrap. Each list element is a row.
* @param colHeader Header to use for the table's sole column. Pass null for
* no column header.
* @param rowHeaders List of row header labels. Pass null for no row headers.
* @param <T> The type of data in each cell of the table.
* @return A {@link Table} object wrapping the data structure.
*/
public static <T> Table<Column<T>, T> wrap(final List<T> data,
final String colHeader, final List<String> rowHeaders)
{
return new ReadOnlyTable<T>() {
@Override
public int getRowCount() {
return data.size();
}
@Override
public String getRowHeader(final int row) {
if (rowHeaders == null || rowHeaders.size() < row) return null;
return rowHeaders.get(row);
}
@Override
public int size() {
return 1;
}
@Override
public Column<T> get(final int col) {
if (col != 0) //
throw new IllegalArgumentException("Column out of range: " + col);
return new ColumnAccessor<T>(data, colHeader) {
@Override
public int size() {
return data.size();
}
@Override
public T get(final int index) {
return data.get(index);
}
};
}
};
}
/**
* Creates a table wrapping a list of maps. Each map is one row of the table.
* Map keys are column names; map values are cell data.
* <p>
* Note that columns are inferred from the first map/row of the table, not
* unioned across all rows.
* </p>
*
* @param data The data to wrap. Each list element is a row; maps go from
* column name to data.
* @param rowHeaders List of row header labels. Pass null for no row headers.
* @param <T> The type of data in each cell of the table.
* @return A {@link Table} object wrapping the data structure.
*/
public static <T> Table<Column<T>, T> wrap(
final List<? extends Map<?, T>> data, final List<String> rowHeaders)
{
if (data.isEmpty()) throw new IllegalArgumentException("Cannot wrap an empty list");
return new ReadOnlyTable<T>() {
@Override
public int getRowCount() {
return data.size();
}
@Override
public String getRowHeader(final int row) {
if (rowHeaders == null || rowHeaders.size() < row) return null;
return rowHeaders.get(row);
}
@Override
public int size() {
return data.get(0).size();
}
@Override
public Column<T> get(final int col) {
return new ColumnAccessor<T>(data, null) {
private boolean initialized;
private String colHeader;
@Override
public String getHeader() {
if (!initialized) {
int c = 0;
for (final Object key : data.get(0).keySet()) {
if (col == c++) {
colHeader = key == null ? null : key.toString();
break;
}
}
initialized = true;
}
return colHeader;
}
@Override
public int size() {
return data.size();
}
@Override
public T get(final int index) {
return data.get(index).get(getHeader());
}
};
}
};
}
// -- Internal methods --
/**
* Gets the column index corresponding to the given header, throwing an
* exception if no such column exists.
*/
static int colIndex(final Table<?, ?> table, final String colHeader) {
final int colIndex = table.getColumnIndex(colHeader);
if (colIndex < 0) //
throw new IllegalArgumentException("No such column: " + colHeader);
return colIndex;
}
/** Throws an exception if the given column(s) are out of bounds. */
static void checkCol(final Table<?, ?> table, final int col,
final int count)
{
check("column", col, count, table.getColumnCount());
}
/** Throws an exception if the given row(s) are out of bounds. */
static void checkRow(final Table<?, ?> table, final int row,
final int count)
{
check("row", row, count, table.getRowCount());
}
/**
* Generics-friendly helper method for {@link Table#set(int, int, Object)} and
* {@link Table#set(String, int, Object)}.
*/
static <U> void assign(final Column<U> column, final int row,
final Object value)
{
if (value != null && !column.getType().isInstance(value)) {
throw new IllegalArgumentException("value of type " + value.getClass() +
" is not a " + column.getType());
}
@SuppressWarnings("unchecked")
final U typedValue = (U) value;
column.set(row, typedValue);
}
// -- Helper methods --
/** Throws an exception if the given values are out of bounds. */
private static void check(final String name, final int index, final int count,
final int bound)
{
final int last = index + count - 1;
if (index >= 0 && last < bound) return;
if (count <= 1) {
throw new IndexOutOfBoundsException("Invalid " + name + ": " + index);
}
throw new IndexOutOfBoundsException("Invalid " + name + "s: " + index +
" - " + last);
}
private static UnsupportedOperationException readOnlyException() {
return new UnsupportedOperationException("Wrapped table is read-only");
}
// -- Helper classes --
/** Read-only version of a {@link Table}. */
private static interface ReadOnlyTable<T> extends Table<Column<T>, T> {
@Override
default void setColumnCount(final int colCount) {
throw readOnlyException();
}
@Override
default List<Column<T>> insertColumns(final int col, final int count) {
throw readOnlyException();
}
@Override
default void setRowCount(final int rowCount) {
throw readOnlyException();
}
@Override
default void removeRows(int row, int count) {
throw readOnlyException();
}
@Override
default void setRowHeader(int row, String header) {
throw readOnlyException();
}
@Override
default void set(String colHeader, int row, T value) {
throw readOnlyException();
}
@Override
default boolean add(final Column<T> column) {
throw readOnlyException();
}
@Override
default boolean remove(final Object column) {
throw readOnlyException();
}
@Override
default boolean addAll(Collection<? extends Column<T>> c) {
throw readOnlyException();
}
@Override
default boolean addAll(int col, Collection<? extends Column<T>> c) {
throw readOnlyException();
}
@Override
default boolean removeAll(Collection<?> c) {
throw readOnlyException();
}
@Override
default boolean retainAll(Collection<?> c) {
throw readOnlyException();
}
@Override
default void clear() {
throw readOnlyException();
}
@Override
default Column<T> set(int col, Column<T> column) {
throw readOnlyException();
}
@Override
default void add(int col, Column<T> column) {
throw readOnlyException();
}
@Override
default Column<T> remove(int col) {
throw readOnlyException();
}
}
/** Read-only version of a {@link Column}. */
private static interface ReadOnlyColumn<T> extends Column<T> {
@Override
default boolean add(final T e) {
throw readOnlyException();
}
@Override
default boolean remove(Object o) {
throw readOnlyException();
}
@Override
default boolean addAll(Collection<? extends T> c) {
throw readOnlyException();
}
@Override
default boolean addAll(int index, Collection<? extends T> c) {
throw readOnlyException();
}
@Override
default boolean removeAll(Collection<?> c) {
throw readOnlyException();
}
@Override
default boolean retainAll(Collection<?> c) {
throw readOnlyException();
}
@Override
default void clear() {
throw readOnlyException();
}
@Override
default T set(int index, T element) {
throw readOnlyException();
}
@Override
default void add(int index, T element) {
throw readOnlyException();
}
@Override
default T remove(int index) {
throw readOnlyException();
}
@Override
default void setHeader(String header) {
throw readOnlyException();
}
@Override
default void setSize(int size) {
throw readOnlyException();
}
}
private static abstract class ColumnAccessor<T> implements ReadOnlyColumn<T> {
private final Object data;
private final String colHeader;
private ColumnAccessor(final Object data, final String colHeader) {
this.data = data;
this.colHeader = colHeader;
}
@Override
public String getHeader() {
return colHeader;
}
@Override
public Class<T> getType() {
// TODO: Consider whether this is terrible.
throw new UnsupportedOperationException();
}
@Override
public int hashCode() {
return getHeader().hashCode();
}
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof ColumnAccessor)) return false;
final ColumnAccessor<?> other = ((ColumnAccessor<?>) obj);
if (data != other.data) return false;
return Objects.equals(getHeader(), other.getHeader());
}
}
}