Skip to content

Commit 8288800

Browse files
committed
Add Tables.wrap method for lists of vanilla data
If you have a list, each element of the list can be treated as a row in a table with a single column. This method enables that sort of wrapping.
1 parent 32a0c17 commit 8288800

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-table</artifactId>
13-
<version>0.3.1-SNAPSHOT</version>
13+
<version>0.4.0-SNAPSHOT</version>
1414

1515
<name>SciJava Table</name>
1616
<description>Table structures for SciJava.</description>

src/main/java/org/scijava/table/Tables.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,84 @@ private Tables() {
4646
// NB: Prevent instantiation of utility class.
4747
}
4848

49+
/**
50+
* Creates a single-column table wrapping a list. Each element is one row of
51+
* the table.
52+
*
53+
* @param data The data to wrap. Each list element is a row.
54+
* @param colHeader Header to use for the table's sole column. Pass null for
55+
* no column header.
56+
* @param rowHeaders List of row header labels. Pass null for no row headers.
57+
* @param <T> The type of data in each cell of the table.
58+
* @return A {@link Table} object wrapping the data structure.
59+
*/
60+
public static <T> Table<Column<T>, T> wrap(final List<T> data,
61+
final String colHeader, final List<String> rowHeaders)
62+
{
63+
return new ReadOnlyTable<T>() {
64+
65+
@Override
66+
public int getRowCount() {
67+
return data.size();
68+
}
69+
70+
@Override
71+
public String getRowHeader(final int row) {
72+
if (rowHeaders == null || rowHeaders.size() < row) return null;
73+
return rowHeaders.get(row);
74+
}
75+
76+
@Override
77+
public int size() {
78+
return 1;
79+
}
80+
81+
@Override
82+
public Column<T> get(final int col) {
83+
if (col != 0) //
84+
throw new IllegalArgumentException("Column out of range: " + col);
85+
class ColumnAccessor implements ReadOnlyColumn<T> {
86+
87+
private final Object tableData = data;
88+
89+
@Override
90+
public String getHeader() {
91+
return colHeader;
92+
}
93+
94+
@Override
95+
public int size() {
96+
return data.size();
97+
}
98+
99+
@Override
100+
public Class<T> getType() {
101+
// TODO: Consider whether this is terrible.
102+
throw new UnsupportedOperationException();
103+
}
104+
105+
@Override
106+
public T get(final int index) {
107+
return data.get(index);
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
return getHeader().hashCode();
113+
}
114+
115+
@Override
116+
public boolean equals(final Object obj) {
117+
if (!(obj instanceof ColumnAccessor)) return false;
118+
final ColumnAccessor other = ((ColumnAccessor) obj);
119+
return data == other.tableData;
120+
}
121+
}
122+
return new ColumnAccessor();
123+
}
124+
};
125+
}
126+
49127
/**
50128
* Creates a table wrapping a list of maps. Each map is one row of the table.
51129
* Map keys are column names; map values are cell data.

src/test/java/org/scijava/table/TablesTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,37 @@
4747
public class TablesTest {
4848

4949
@Test
50-
public void testWrap() {
50+
public void testWrapList() {
51+
final List<?> names = //
52+
Arrays.asList("Phoebe", "Jezebel", "Daphne", "Fiona");
53+
final String colHeader = "Name";
54+
final List<String> rowHeaders = Arrays.asList("A", "B", "C", "D");
55+
final Table<?, ?> table = Tables.wrap(names, colHeader, rowHeaders);
56+
57+
// check table dimensions
58+
assertEquals(1, table.size());
59+
assertEquals(1, table.getColumnCount());
60+
assertEquals(4, table.getRowCount());
61+
62+
// check row headers
63+
for (int r = 0; r < rowHeaders.size(); r++)
64+
assertEquals(rowHeaders.get(r), table.getRowHeader(r));
65+
66+
// check direct data access
67+
for (int i = 0; i < names.size(); i++)
68+
assertEquals(names.get(i), table.get(0, i));
69+
70+
// check first column
71+
assertEquals("Name", table.getColumnHeader(0));
72+
final Column<?> nameColumn = table.get("Name");
73+
assertEquals(4, nameColumn.size());
74+
for (int i = 0; i < names.size(); i++)
75+
assertEquals(names.get(i), nameColumn.get(i));
76+
assertEquals(nameColumn, table.get(0));
77+
}
78+
79+
@Test
80+
public void testWrapMaps() {
5181
final List<Map<Object, Object>> towns = Arrays.asList( //
5282
map("Town", "Shanghai", "Population", 24_256_800), //
5383
map("Town", "Karachi", "Population", 23_500_000), //

0 commit comments

Comments
 (0)