Skip to content

Commit 9bf6dc4

Browse files
committed
Add I/O service
This general-purpose, non-image-specific service migrated from ImageJ: https://github.com/imagej/imagej/tree/imagej-2.0.0-beta-7.8/core/core/src/main/java/imagej/io
1 parent 25c3d9b commit 9bf6dc4

File tree

7 files changed

+526
-0
lines changed

7 files changed

+526
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.plugin.AbstractHandlerPlugin;
37+
38+
/**
39+
* Abstract base class for {@link IOPlugin}s.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public abstract class AbstractIOPlugin<D> extends AbstractHandlerPlugin<String>
44+
implements IOPlugin<D>
45+
{
46+
47+
// -- IOPlugin methods --
48+
49+
@Override
50+
public boolean supportsOpen(final String source) {
51+
return false;
52+
}
53+
54+
@Override
55+
public boolean supportsSave(final String destination) {
56+
return false;
57+
}
58+
59+
@Override
60+
public boolean supportsSave(final Object data, final String destination) {
61+
return supportsSave(destination) && getDataType().isInstance(data);
62+
}
63+
64+
@Override
65+
public D open(final String source) throws IOException {
66+
throw new UnsupportedOperationException();
67+
}
68+
69+
@Override
70+
public void save(final D data, final String destination) throws IOException {
71+
throw new UnsupportedOperationException();
72+
}
73+
74+
// -- Typed methods --
75+
76+
@Override
77+
public boolean supports(final String descriptor) {
78+
return supportsOpen(descriptor) || supportsSave(descriptor);
79+
}
80+
81+
@Override
82+
public Class<String> getType() {
83+
return String.class;
84+
}
85+
86+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.event.EventService;
37+
import org.scijava.log.LogService;
38+
import org.scijava.plugin.AbstractHandlerService;
39+
import org.scijava.plugin.Parameter;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.service.Service;
42+
43+
/**
44+
* Default implementation of {@link IOService}.
45+
*
46+
* @author Curtis Rueden
47+
*/
48+
@Plugin(type = Service.class)
49+
public final class DefaultIOService
50+
extends AbstractHandlerService<String, IOPlugin<?>> implements IOService
51+
{
52+
53+
@Parameter
54+
private LogService log;
55+
56+
@Parameter
57+
private EventService eventService;
58+
59+
// -- IOService methods --
60+
61+
@Override
62+
public IOPlugin<?> getOpener(final String source) {
63+
for (final IOPlugin<?> handler : getInstances()) {
64+
if (handler.supportsOpen(source)) return handler;
65+
}
66+
return null;
67+
}
68+
69+
@Override
70+
public <D> IOPlugin<D> getSaver(final D data, final String destination) {
71+
for (final IOPlugin<?> handler : getInstances()) {
72+
if (handler.supportsSave(data, destination)) {
73+
@SuppressWarnings("unchecked")
74+
final IOPlugin<D> typedHandler = (IOPlugin<D>) handler;
75+
return typedHandler;
76+
}
77+
}
78+
return null;
79+
}
80+
81+
@Override
82+
public Object open(final String source) throws IOException {
83+
return getOpener(source).open(source);
84+
}
85+
86+
@Override
87+
public void save(final Object data, final String destination)
88+
throws IOException
89+
{
90+
getSaver(data, destination).save(data, destination);
91+
}
92+
93+
// -- HandlerService methods --
94+
95+
@Override
96+
@SuppressWarnings({ "rawtypes", "unchecked" })
97+
public Class<IOPlugin<?>> getPluginType() {
98+
return (Class) IOPlugin.class;
99+
}
100+
101+
@Override
102+
public Class<String> getType() {
103+
return String.class;
104+
}
105+
106+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.plugin.HandlerPlugin;
37+
import org.scijava.plugin.Plugin;
38+
39+
/**
40+
* A plugin which extends an application's I/O capabilities.
41+
* <p>
42+
* I/O plugins discoverable at runtime must implement this interface and be
43+
* annotated with @{@link Plugin} with attribute {@link Plugin#type()} =
44+
* {@link IOPlugin}.class. While it possible to create an I/O plugin merely by
45+
* implementing this interface, it is encouraged to instead extend
46+
* {@link AbstractIOPlugin}, for convenience.
47+
* </p>
48+
*
49+
* @author Curtis Rueden
50+
* @see Plugin
51+
* @see IOService
52+
*/
53+
public interface IOPlugin<D> extends HandlerPlugin<String> {
54+
55+
/** The type of data opened and/or saved by the plugin. */
56+
Class<D> getDataType();
57+
58+
/** Checks whether the I/O plugin can open data from the given source. */
59+
boolean supportsOpen(String source);
60+
61+
/** Checks whether the I/O plugin can save data to the given destination. */
62+
boolean supportsSave(String destination);
63+
64+
/**
65+
* Checks whether the I/O plugin can save the given data to the specified
66+
* destination.
67+
*/
68+
boolean supportsSave(Object data, String destination);
69+
70+
/** Opens data from the given source. */
71+
D open(String source) throws IOException;
72+
73+
/** Saves the given data to the specified destination. */
74+
void save(D data, String destination) throws IOException;
75+
76+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.plugin.HandlerService;
37+
import org.scijava.service.SciJavaService;
38+
39+
/**
40+
* Interface for data I/O operations: opening and saving data.
41+
*
42+
* @author Curtis Rueden
43+
*/
44+
public interface IOService extends HandlerService<String, IOPlugin<?>>,
45+
SciJavaService
46+
{
47+
48+
/**
49+
* Gets the most appropriate {@link IOPlugin} for opening data from the given
50+
* source.
51+
*/
52+
IOPlugin<?> getOpener(String source);
53+
54+
/**
55+
* Gets the most appropriate {@link IOPlugin} for saving data to the given
56+
* destination.
57+
*/
58+
<D> IOPlugin<D> getSaver(D data, String destination);
59+
60+
/**
61+
* Loads data from the given source. For extensibility, the nature of the
62+
* source is left intentionally general, but two common examples include file
63+
* paths and URLs.
64+
* <p>
65+
* The opener to use is automatically determined based on available
66+
* {@link IOPlugin}s; see {@link #getOpener(String)}.
67+
* </p>
68+
*
69+
* @param source The source (e.g., file path) from which to data should be
70+
* loaded.
71+
* @return An object representing the loaded data, or null if the source is
72+
* not supported.
73+
* @throws IOException if something goes wrong loading the data.
74+
*/
75+
Object open(String source) throws IOException;
76+
77+
/**
78+
* Saves data to the given destination. The nature of the destination is left
79+
* intentionally general, but the most common example is a file path.
80+
* <p>
81+
* The saver to use is automatically determined based on available
82+
* {@link IOPlugin}s; see {@link #getSaver(Object, String)}.
83+
* </p>
84+
*
85+
* @param data The data to be saved to the destination.
86+
* @param destination The destination (e.g., file path) to which data should
87+
* be saved.
88+
* @throws IOException if something goes wrong saving the data.
89+
*/
90+
void save(Object data, String destination) throws IOException;
91+
92+
}

0 commit comments

Comments
 (0)