Skip to content

TypedIOService interface #396

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

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
140 changes: 140 additions & 0 deletions src/main/java/org/scijava/io/AbstractTypedIOService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2020 SciJava developers.
* %%
* 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.io;

import org.scijava.io.location.Location;
import org.scijava.io.location.LocationService;
import org.scijava.plugin.AbstractHandlerService;
import org.scijava.plugin.Parameter;

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

/**
* Abstract base class for typed {@link IOPlugin}s.
*
* @author Curtis Rueden
* @author Deborah Schmidt
*/
public abstract class AbstractTypedIOService<D> extends AbstractHandlerService<Location, IOPlugin<D>> implements TypedIOService<D>
{

@Parameter
private LocationService locationService;

@Parameter
private IOService ioService;

@Override
public D open(String source) throws IOException {
try {
return open(locationService.resolve(source));
} catch (URISyntaxException e) {
throw new IOException(e);
}
}

@Override
public D open(Location source) throws IOException {
IOPlugin<?> opener = ioService().getOpener(source);
try {
Class<D> ignored = (Class<D>) opener.getDataType();
return (D) opener.open(source);
}
catch(ClassCastException e) {
throw new UnsupportedOperationException("No compatible opener found.");
}
}

@Override
public void save(D data, String destination) throws IOException {
try {
save(data, locationService.resolve(destination));
} catch (URISyntaxException e) {
throw new IOException(e);
}
}

@Override
public void save(D data, Location destination) throws IOException {
IOPlugin<D> saver = ioService().getSaver(data, destination);
if (saver != null) {
saver.save(data, destination);
}
else {
throw new UnsupportedOperationException("No compatible saver found.");
}
}

@Override
public boolean canOpen(String source) {
try {
return canOpen(locationService.resolve(source));
} catch (URISyntaxException e) {
return false;
}
}

@Override
public boolean canOpen(Location source) {
IOPlugin<?> opener = ioService().getOpener(source);
if (opener == null) return false;
try {
Class<D> ignored = (Class<D>) (opener.getDataType());
return true;
} catch(ClassCastException e) {
return false;
}
}

@Override
public boolean canSave(D data, String source) {
try {
return canSave(data, locationService.resolve(source));
} catch (URISyntaxException e) {
return false;
}
}

@Override
public boolean canSave(D data, Location destination) {
IOPlugin<D> saver = ioService.getSaver(data, destination);
if (saver == null) return false;
return saver.supportsSave(destination);
}

protected LocationService locationService() {
return locationService;
}

protected IOService ioService() {
return ioService;
}
}
168 changes: 168 additions & 0 deletions src/main/java/org/scijava/io/TypedIOService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2020 SciJava developers.
* %%
* 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.io;

import java.io.IOException;

import org.scijava.io.location.FileLocation;
import org.scijava.io.location.Location;
import org.scijava.plugin.HandlerService;
import org.scijava.service.SciJavaService;

/**
* Interface for high-level data I/O: opening and saving data of a specific type.
*
* @author Curtis Rueden
* @author Deborah Schmidt
*/
public interface TypedIOService<D> extends HandlerService<Location, IOPlugin<D>>,
SciJavaService
{

/**
* Gets the most appropriate {@link IOPlugin} for opening data from the given
* location.
*/
default IOPlugin<D> getOpener(final String source) {
return getOpener(new FileLocation(source));
}

/**
* Gets the most appropriate {@link IOPlugin} for opening data from the given
* location.
*/
default IOPlugin<D> getOpener(Location source) {
for (final IOPlugin<D> handler : getInstances()) {
if (handler.supportsOpen(source)) return handler;
}
return null;
}

/**
* Gets the most appropriate {@link IOPlugin} for saving data to the given
* location.
*/
default IOPlugin<D> getSaver(final D data, final String destination) {
return getSaver(data, new FileLocation(destination));
}

/**
* Gets the most appropriate {@link IOPlugin} for saving data to the given
* location.
*/
default IOPlugin<D> getSaver(D data, Location destination) {
for (final IOPlugin<?> handler : getInstances()) {
if (handler.supportsSave(data, destination)) {
return (IOPlugin<D>) handler;
}
}
return null;
}

/**
* Loads data from the given source. For extensibility, the nature of the
* source is left intentionally general, but two common examples include file
* paths and URLs.
* <p>
* The opener to use is automatically determined based on available
* {@link IOPlugin}s; see {@link #getOpener(String)}.
* </p>
*
* @param source The source (e.g., file path) from which to data should be
* loaded.
* @return An object representing the loaded data, or null if the source is
* not supported.
* @throws IOException if something goes wrong loading the data.
*/
D open(String source) throws IOException;

/**
* Loads data from the given location.
* <p>
* The opener to use is automatically determined based on available
* {@link IOPlugin}s; see {@link #getOpener(Location)}.
* </p>
*
* @param source The location from which to data should be loaded.
* @return An object representing the loaded data, or null if the source is
* not supported.
* @throws IOException if something goes wrong loading the data.
*/
D open(Location source) throws IOException;

/**
* Saves data to the given destination. The nature of the destination is left
* intentionally general, but the most common example is a file path.
* <p>
* The saver to use is automatically determined based on available
* {@link IOPlugin}s; see {@link #getSaver(Object, String)}.
* </p>
*
* @param data The data to be saved to the destination.
* @param destination The destination (e.g., file path) to which data should
* be saved.
* @throws IOException if something goes wrong saving the data.
*/
void save(D data, String destination) throws IOException;

/**
* Saves data to the given location.
* <p>
* The saver to use is automatically determined based on available
* {@link IOPlugin}s; see {@link #getSaver(Object, Location)}.
* </p>
*
* @param data The data to be saved to the destination.
* @param destination The destination location to which data should be saved.
* @throws IOException if something goes wrong saving the data.
*/
void save(D data, Location destination) throws IOException;

boolean canOpen(String source);

boolean canOpen(Location source);

boolean canSave(D data, String destination);

boolean canSave(D data, Location destination);

// -- HandlerService methods --

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
default Class<IOPlugin<D>> getPluginType() {
return (Class) IOPlugin.class;
}

@Override
default Class<Location> getType() {
return Location.class;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/scijava/text/io/DefaultTextIOService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2020 SciJava developers.
* %%
* 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.text.io;

import org.scijava.io.AbstractTypedIOService;
import org.scijava.plugin.Plugin;
import org.scijava.service.Service;

/**
* Default {@link TextIOService} implementation for opening and saving text data
*
* @author Deborah Schmidt
*/
@Plugin(type = Service.class)
public class DefaultTextIOService extends AbstractTypedIOService<String> implements TextIOService {
}
40 changes: 40 additions & 0 deletions src/main/java/org/scijava/text/io/TextIOService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2020 SciJava developers.
* %%
* 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.text.io;

import org.scijava.io.TypedIOService;

/**
* {@link TypedIOService} for opening and saving text data
*
* @author Deborah Schmidt
*/
public interface TextIOService extends TypedIOService<String> {
}
Loading