Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO: use Location, TypedIOService #17

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research.</li

<!-- NB: Deploy releases to the SciJava Maven repository. -->
<releaseProfiles>deploy-to-scijava</releaseProfiles>
<scijava-common.version>2.84.0</scijava-common.version>
</properties>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/scijava/table/io/ColumnTableIOOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ColumnTableIOOptions formatter(Function<Object, String> formatter) {
return setValue(formatterKey, formatter);
}

ColumnTableIOOptions parser(Function<String, Object> parser) {
ColumnTableIOOptions parser(Function<String, ?> parser) {
return setValue(parserKey, parser);
}

Expand All @@ -57,7 +57,7 @@ public Function<Object, String> formatter() {
return getValueOrDefault(formatterKey, String::valueOf);
}

public Function<String, Object> parser() {
public Function<String, ?> parser() {
return getValueOrDefault(parserKey, String::valueOf);
}
}
Expand Down
51 changes: 21 additions & 30 deletions src/main/java/org/scijava/table/io/DefaultTableIOService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,41 @@
package org.scijava.table.io;

import java.io.IOException;
import java.net.URISyntaxException;

import org.scijava.io.AbstractTypedIOService;
import org.scijava.io.IOPlugin;
import org.scijava.io.IOService;
import org.scijava.io.location.Location;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.table.Table;

@Plugin(type = Service.class)
public class DefaultTableIOService extends AbstractService implements
public class DefaultTableIOService extends AbstractTypedIOService<Table<?, ?>> implements
TableIOService
{

@Parameter
private IOService ioService;

@Override
public boolean canOpen(String source) {
IOPlugin<?> opener = ioService.getOpener(source);
public boolean canOpen(Location source) {
IOPlugin<?> opener = ioService().getOpener(source);
if (opener == null) return false;
return Table.class.isAssignableFrom(opener.getDataType());
}

@Override
public boolean canSave(Table<?, ?> table, String destination) {
IOPlugin<Table<?, ?>> saver = ioService.getSaver(table, destination);
if (saver == null) return false;
return saver.supportsSave(destination);
}

@Override
public Table<?, ?> open(String source) throws IOException {
IOPlugin<?> opener = ioService.getOpener(source);
if (opener != null && Table.class.isAssignableFrom(opener.getDataType())) {
return (Table<?, ?>) opener.open(source);
public Table<?, ?> open(String source, TableIOOptions options) throws IOException {
try {
return open(locationService().resolve(source), options);
} catch (URISyntaxException e) {
throw new IOException(e);
}
throw new UnsupportedOperationException("No compatible opener found.");
}

@Override
public Table<?, ?> open(String source, TableIOOptions options) throws IOException {
IOPlugin<?> opener = ioService.getOpener(source);
public Table<?, ?> open(Location source, TableIOOptions options) throws IOException {
IOPlugin<?> opener = ioService().getOpener(source);
if (opener != null && Table.class.isAssignableFrom(opener.getDataType())
&& TableIOPlugin.class.isAssignableFrom(opener.getClass())) {
return ((TableIOPlugin)opener).open(source, options);
Expand All @@ -82,24 +74,23 @@ public boolean canSave(Table<?, ?> table, String destination) {
}

@Override
public void save(Table<?, ?> table, String destination) throws IOException {
IOPlugin<Table<?, ?>> saver = ioService.getSaver(table, destination);
if (saver != null) {
saver.save(table, destination);
}
else {
throw new UnsupportedOperationException("No compatible saver found.");
public void save(Table<?, ?> table, String destination, TableIOOptions options) throws IOException {
try {
save(table, locationService().resolve(destination), options);
} catch (URISyntaxException e) {
throw new IOException(e);
}
}

@Override
public void save(Table<?, ?> table, String destination, TableIOOptions options) throws IOException {
IOPlugin<Table> saver = ioService.getSaver(table, destination);
public void save(Table<?, ?> table, Location destination, TableIOOptions options) throws IOException {
IOPlugin<Table> saver = ioService().getSaver(table, destination);
if (saver != null && TableIOPlugin.class.isAssignableFrom(saver.getClass())) {
((TableIOPlugin)saver).save(table, destination, options);
}
else {
throw new UnsupportedOperationException("No compatible saver found.");
}
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/scijava/table/io/TableIOOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public TableIOOptions guessParser(boolean guessParser) {
/**
* @param parser Parser to use when converting table entries into data objects.
*/
public TableIOOptions parser(Function<String, Object> parser) {
public TableIOOptions parser(Function<String, ?> parser) {
guessParser(false);
return setValue(parserKey, parser);
}
Expand Down Expand Up @@ -188,7 +188,7 @@ public TableIOOptions columnFormatter(int column, Function<Object, String> forma
return this;
}

private Function<String, Object> getParser(Class<?> type) {
private Function<String, ?> getParser(Class<?> type) {
if(type.equals(String.class)) return String::valueOf;
if(type.equals(Double.class)) return Double::valueOf;
if(type.equals(Float.class)) return Float::valueOf;
Expand Down Expand Up @@ -266,7 +266,7 @@ public boolean guessParser() {
/**
* @return Parser to use when converting table entries into data objects.
*/
public Function<String, Object> parser() {
public Function<String, ?> parser() {
return getValueOrDefault(parserKey, String::valueOf);
}

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/org/scijava/table/io/TableIOPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

package org.scijava.table.io;

import org.scijava.io.AbstractIOPlugin;
import org.scijava.io.IOPlugin;
import org.scijava.io.location.Location;
import org.scijava.table.Table;

import java.io.IOException;
Expand All @@ -40,32 +41,30 @@
*
* @author Deborah Schmidt
*/
public class TableIOPlugin extends AbstractIOPlugin<Table> {
public interface TableIOPlugin extends IOPlugin<Table> {

@Override
public Table<?, ?> open(String source) throws IOException {
default Table<?, ?> open(Location source) throws IOException {
return open(source, new TableIOOptions());
}

/** Opens data from the given source. */
@SuppressWarnings("unused")
public Table<?, ?> open(final String source, final TableIOOptions options) throws IOException {
default Table<?, ?> open(final Location source, final TableIOOptions options) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void save(Table data, String destination) throws IOException {
default void save(Table data, Location destination) throws IOException {
save(data, destination, new TableIOOptions());
}

/** Saves the given data to the specified destination. */
@SuppressWarnings("unused")
public void save(final Table<?, ?> data, final String destination, final TableIOOptions options) throws IOException {
default void save(final Table<?, ?> data, final Location destination, final TableIOOptions options) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public Class<Table> getDataType() {
default Class<Table> getDataType() {
return Table.class;
}
}
21 changes: 14 additions & 7 deletions src/main/java/org/scijava/table/io/TableIOService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@

import java.io.IOException;

import org.scijava.io.IOService;
import org.scijava.io.TypedIOService;
import org.scijava.io.location.Location;
import org.scijava.service.SciJavaService;
import org.scijava.table.Table;

public interface TableIOService extends SciJavaService {
public interface TableIOService extends TypedIOService<Table<?, ?>> {

boolean canOpen(String source);

boolean canSave(Table<?, ?> table, String destination);

Table<?, ?> open(String source) throws IOException;
@Override
default Table<?, ?> open(Location source) throws IOException {
return open(source, TableIOOptions.options());
}
Table<?, ?> open(String source, TableIOOptions options) throws IOException;
Table<?, ?> open(Location source, TableIOOptions options) throws IOException;

void save(Table<?, ?> table, String destination) throws IOException;
@Override
default void save(Table<?, ?> table, Location destination) throws IOException {
save(table, destination, TableIOOptions.options());
}
void save(Table<?, ?> table, String destination, TableIOOptions options) throws IOException;
void save(Table<?, ?> table, Location destination, TableIOOptions options) throws IOException;
}
14 changes: 8 additions & 6 deletions src/test/java/org/scijava/table/io/TableIOServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.junit.Before;
import org.junit.Test;
import org.scijava.Context;
import org.scijava.io.AbstractIOPlugin;
import org.scijava.io.IOPlugin;
import org.scijava.io.location.Location;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;
import org.scijava.table.DefaultGenericTable;
Expand Down Expand Up @@ -107,24 +109,24 @@ public void testTableIOServiceWithOptions() {
}

@SuppressWarnings("rawtypes")
public static class FakeTableIOPlugin extends TableIOPlugin {
public static class FakeTableIOPlugin extends AbstractIOPlugin<Table> implements TableIOPlugin {

@Override
public boolean supportsOpen(String loc) {
return loc.endsWith("fakeTable");
public boolean supportsOpen(Location loc) {
return loc.getName().endsWith("fakeTable");
}

@Override
public boolean supportsSave(String loc) {
return loc.endsWith("fakeTable");
public boolean supportsSave(Location loc) {
return loc.getName().endsWith("fakeTable");
}

/**
* This method creates a fake table for the purpose of testing the propagation of options.
* It creates a row and a column with header names based on the {@param options}.
*/
@Override
public Table open(String loc, TableIOOptions options) {
public Table open(Location loc, TableIOOptions options) {
DefaultGenericTable table = new DefaultGenericTable();
if(options.values.readColumnHeaders()) {
table.appendColumn(String.valueOf(options.values.columnDelimiter()));
Expand Down