Skip to content

Commit 8cb30e4

Browse files
committed
Change type of DefaultTableIOPlugin to Table
By typing to the Table interface instead of the GenericTable subinterface, we can support more different Table implementations.
1 parent 6066f0c commit 8cb30e4

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@
5656
*
5757
* @author Leon Yang
5858
*/
59-
@Plugin(type = IOPlugin.class, priority = Priority.LOW_PRIORITY)
60-
public class DefaultTableIOPlugin extends AbstractIOPlugin<GenericTable> {
59+
@SuppressWarnings("rawtypes")
60+
@Plugin(type = IOPlugin.class, priority = Priority.LOW)
61+
public class DefaultTableIOPlugin extends AbstractIOPlugin<Table> {
6162

6263
@Parameter
6364
private DataHandleService dataHandleService;
@@ -117,8 +118,8 @@ public class DefaultTableIOPlugin extends AbstractIOPlugin<GenericTable> {
117118
"rtf")));
118119

119120
@Override
120-
public Class<GenericTable> getDataType() {
121-
return GenericTable.class;
121+
public Class<Table> getDataType() {
122+
return Table.class;
122123
}
123124

124125
@Override
@@ -262,7 +263,7 @@ public GenericTable open(final String source) throws IOException {
262263
}
263264

264265
@Override
265-
public void save(final GenericTable table, final String destination)
266+
public void save(final Table table, final String destination)
266267
throws IOException
267268
{
268269
// FIXME Assumes FileLocation
@@ -271,12 +272,14 @@ public void save(final GenericTable table, final String destination)
271272
try (final DataHandle<Location> handle = //
272273
dataHandleService.create(dstLocation))
273274
{
274-
final boolean writeRH = this.writeRowHeaders && table.getRowCount() > 0 &&
275-
IntStream.range(0, table.getRowCount()).allMatch(row -> table
276-
.getRowHeader(row) != null);
277-
final boolean writeCH = this.writeColHeaders && table
278-
.getColumnCount() > 0 && table.stream().allMatch(col -> col
279-
.getHeader() != null);
275+
final boolean writeRH = this.writeRowHeaders && //
276+
table.getRowCount() > 0 && //
277+
IntStream.range(0, table.getRowCount()).allMatch(row -> table
278+
.getRowHeader(row) != null);
279+
final boolean writeCH = this.writeColHeaders && //
280+
table.getColumnCount() > 0 && //
281+
IntStream.range(0, table.getColumnCount()).allMatch(col -> table
282+
.getColumnHeader(col) != null);
280283

281284
final StringBuilder sb = new StringBuilder();
282285
// write column headers

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
*
6161
* @author Leon Yang
6262
*/
63+
@SuppressWarnings("rawtypes")
6364
public class DefaultTableIOPluginTest {
6465

6566
private static final Context ctx = new Context();
@@ -95,7 +96,7 @@ public void testParser() {
9596
"123.000\t-123.000\t123.000\t123.000\t0.000\n" +
9697
"0.000\t1234567890.099\tNaN\t-Infinity\t0.000\n";
9798

98-
final IOPlugin<GenericTable> tableIO = ctx.service(IOService.class)
99+
final IOPlugin<Table> tableIO = ctx.service(IOService.class)
99100
.getInstance(DefaultTableIOPlugin.class);
100101
try {
101102
final Function<String, Double> parser = Double::valueOf;
@@ -106,7 +107,7 @@ public void testParser() {
106107
"cornerText", "parser", "formatter" }, new Object[] { true, true, false,
107108
true, "\t", "\n", "\"", "\\", parser, formatter });
108109

109-
final GenericTable table = openTable(tableSource, tableIO);
110+
final Table table = openTable(tableSource, tableIO);
110111
assertTableEquals(colHeaders, rowHeaders, content, table);
111112
assertEquals(expected, saveTable(table, tableIO));
112113
}
@@ -140,7 +141,7 @@ public void testQuote() {
140141
"'should\tnot,break',unnecessary_quotes,should,break\r\n" +
141142
"'some,empty,cells','','',''\r\n";
142143

143-
final IOPlugin<GenericTable> tableIO = ctx.service(IOService.class)
144+
final IOPlugin<Table> tableIO = ctx.service(IOService.class)
144145
.getInstance(DefaultTableIOPlugin.class);
145146
try {
146147
setValues(tableIO, new String[] { "readColHeaders", "writeColHeaders",
@@ -149,7 +150,7 @@ public void testQuote() {
149150
true, " ", "\r\n", '\'', "CORNER_TEXT", Function.identity(), Function
150151
.identity() });
151152

152-
final GenericTable table = openTable(tableSource, tableIO);
153+
final Table table = openTable(tableSource, tableIO);
153154
assertTableEquals(colHeaders, rowHeaders, content, table);
154155

155156
setValues(tableIO, new String[] { "separator" }, new Object[] { ',' });
@@ -181,10 +182,10 @@ public void testSmallTables() {
181182
final Double[][] content = { { 3.1415926 } };
182183
final Double[][] emptyContent = { {} };
183184

184-
final IOPlugin<GenericTable> tableIO = ctx.service(IOService.class)
185+
final IOPlugin<Table> tableIO = ctx.service(IOService.class)
185186
.getInstance(DefaultTableIOPlugin.class);
186187
try {
187-
GenericTable table;
188+
Table table;
188189
String expected;
189190
final Function<String, Double> parser = Double::valueOf;
190191
final Function<Double, String> formatter = val -> String.format("%.3f",
@@ -238,7 +239,7 @@ public void testSmallTables() {
238239

239240
@Test(expected = IOException.class)
240241
public void testOpenNonExist() throws IOException {
241-
final IOPlugin<GenericTable> tableIO = ctx.service(IOService.class)
242+
final IOPlugin<Table> tableIO = ctx.service(IOService.class)
242243
.getInstance(DefaultTableIOPlugin.class);
243244
tableIO.open("fake.csv");
244245
}
@@ -250,7 +251,7 @@ public void testOpenNonExist() throws IOException {
250251
*/
251252
private void assertTableEquals(final String[] colHeaders,
252253
final String[] rowHeaders, final Object[][] content,
253-
final GenericTable table)
254+
final Table table)
254255
{
255256
assertEquals(colHeaders.length, table.getColumnCount());
256257
assertEquals(rowHeaders.length, table.getRowCount());
@@ -265,11 +266,11 @@ private void assertTableEquals(final String[] colHeaders,
265266
}
266267
}
267268

268-
private GenericTable openTable(final String tableSource,
269-
final IOPlugin<GenericTable> tableIO) throws IOException
269+
private Table openTable(final String tableSource,
270+
final IOPlugin<Table> tableIO) throws IOException
270271
{
271272
final DataHandleService dataHandleService = ctx.service(DataHandleService.class);
272-
GenericTable result;
273+
Table result;
273274
File tempFile = File.createTempFile("openTest", ".txt");
274275
tempFiles.add(tempFile);
275276
try (DataHandle<Location> destHandle = dataHandleService.create(new FileLocation(tempFile))) {
@@ -279,8 +280,8 @@ private GenericTable openTable(final String tableSource,
279280
return result;
280281
}
281282

282-
private String saveTable(final GenericTable table,
283-
final IOPlugin<GenericTable> tableIO) throws IOException
283+
private String saveTable(final Table table,
284+
final IOPlugin<Table> tableIO) throws IOException
284285
{
285286
final DataHandleService dataHandleService = ctx.service(DataHandleService.class);
286287
String result;

0 commit comments

Comments
 (0)