Skip to content

Commit 7195a96

Browse files
committed
Add Tables.wrap method for Map as 1-column table
1 parent cc3cd12 commit 7195a96

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535
import java.util.Map;
3636
import java.util.Objects;
37+
import java.util.stream.Collectors;
3738

3839
/**
3940
* Utility methods for constructing tables.
@@ -46,6 +47,61 @@ private Tables() {
4647
// NB: Prevent instantiation of utility class.
4748
}
4849

50+
/**
51+
* Creates a single-column table wrapping a map. Each entry is one row of the
52+
* table.
53+
*
54+
* @param data The data to wrap. Each map entry is a row.
55+
* @param colHeader Header to use for the table's sole column. Pass null for
56+
* no column header.
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 Map<?, T> data,
61+
final String colHeader)
62+
{
63+
final List<String> rowHeaders = data.keySet().stream() //
64+
.map(k -> k == null ? null : k.toString()) //
65+
.collect(Collectors.toList());
66+
67+
return new ReadOnlyTable<T>() {
68+
69+
@Override
70+
public int getRowCount() {
71+
return data.size();
72+
}
73+
74+
@Override
75+
public String getRowHeader(final int row) {
76+
return rowHeaders.get(row);
77+
}
78+
79+
@Override
80+
public int size() {
81+
return 1;
82+
}
83+
84+
@Override
85+
public Column<T> get(final int col) {
86+
if (col != 0) //
87+
throw new IllegalArgumentException("Column out of range: " + col);
88+
89+
return new ColumnAccessor<T>(data, colHeader) {
90+
91+
@Override
92+
public int size() {
93+
return data.size();
94+
}
95+
96+
@Override
97+
public T get(final int index) {
98+
return data.get(rowHeaders.get(index));
99+
}
100+
};
101+
}
102+
};
103+
}
104+
49105
/**
50106
* Creates a single-column table wrapping a list. Each element is one row of
51107
* the table.

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@
4646
*/
4747
public class TablesTest {
4848

49+
@Test
50+
public void testWrapMap() {
51+
final Map<?, ?> info = map( //
52+
"Name", "Barack Obama", //
53+
"Birth date", "August 4, 1961", //
54+
"Birth place", "Honolulu, Hawaii" //
55+
);
56+
final String colHeader = "Stat";
57+
final Table<?, ?> table = Tables.wrap(info, colHeader);
58+
59+
// check table dimensions
60+
assertEquals(1, table.size());
61+
assertEquals(1, table.getColumnCount());
62+
assertEquals(info.size(), table.getRowCount());
63+
64+
// check row headers
65+
assertEquals("Name", table.getRowHeader(0));
66+
assertEquals("Birth date", table.getRowHeader(1));
67+
assertEquals("Birth place", table.getRowHeader(2));
68+
69+
// check direct data access
70+
assertEquals("Barack Obama", table.get(0, 0));
71+
assertEquals("August 4, 1961", table.get(0, 1));
72+
assertEquals("Honolulu, Hawaii", table.get(0, 2));
73+
74+
// check first column
75+
assertEquals("Stat", table.getColumnHeader(0));
76+
final Column<?> statColumn = table.get("Stat");
77+
assertEquals(3, statColumn.size());
78+
assertEquals("Barack Obama", statColumn.get(0));
79+
assertEquals("August 4, 1961", statColumn.get(1));
80+
assertEquals("Honolulu, Hawaii", statColumn.get(2));
81+
assertEquals(statColumn, table.get(0));
82+
}
83+
4984
@Test
5085
public void testWrapList() {
5186
final List<?> names = //

0 commit comments

Comments
 (0)