Skip to content

Commit a85b2c0

Browse files
committed
Use Location, not String, in the I/O API
This is much more elegant. It is also a very breaking change.
1 parent 91b4168 commit a85b2c0

File tree

12 files changed

+90
-88
lines changed

12 files changed

+90
-88
lines changed

src/main/java/org/scijava/io/AbstractIOPlugin.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,47 +40,47 @@
4040
*
4141
* @author Curtis Rueden
4242
*/
43-
public abstract class AbstractIOPlugin<D> extends AbstractHandlerPlugin<String>
44-
implements IOPlugin<D>
43+
public abstract class AbstractIOPlugin<D> extends
44+
AbstractHandlerPlugin<Location> implements IOPlugin<D>
4545
{
4646

4747
// -- IOPlugin methods --
4848

4949
@Override
50-
public boolean supportsOpen(final String source) {
50+
public boolean supportsOpen(final Location source) {
5151
return false;
5252
}
5353

5454
@Override
55-
public boolean supportsSave(final String destination) {
55+
public boolean supportsSave(final Location destination) {
5656
return false;
5757
}
5858

5959
@Override
60-
public boolean supportsSave(final Object data, final String destination) {
60+
public boolean supportsSave(final Object data, final Location destination) {
6161
return supportsSave(destination) && getDataType().isInstance(data);
6262
}
6363

6464
@Override
65-
public D open(final String source) throws IOException {
65+
public D open(final Location source) throws IOException {
6666
throw new UnsupportedOperationException();
6767
}
6868

6969
@Override
70-
public void save(final D data, final String destination) throws IOException {
70+
public void save(final D data, final Location destination) throws IOException {
7171
throw new UnsupportedOperationException();
7272
}
7373

7474
// -- Typed methods --
7575

7676
@Override
77-
public boolean supports(final String descriptor) {
78-
return supportsOpen(descriptor) || supportsSave(descriptor);
77+
public boolean supports(final Location location) {
78+
return supportsOpen(location) || supportsSave(location);
7979
}
8080

8181
@Override
82-
public Class<String> getType() {
83-
return String.class;
82+
public Class<Location> getType() {
83+
return Location.class;
8484
}
8585

8686
}

src/main/java/org/scijava/io/DefaultIOService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
*/
5050
@Plugin(type = Service.class)
5151
public final class DefaultIOService
52-
extends AbstractHandlerService<String, IOPlugin<?>> implements IOService
52+
extends AbstractHandlerService<Location, IOPlugin<?>> implements IOService
5353
{
5454

5555
@Parameter
@@ -61,15 +61,15 @@ public final class DefaultIOService
6161
// -- IOService methods --
6262

6363
@Override
64-
public IOPlugin<?> getOpener(final String source) {
64+
public IOPlugin<?> getOpener(final Location source) {
6565
for (final IOPlugin<?> handler : getInstances()) {
6666
if (handler.supportsOpen(source)) return handler;
6767
}
6868
return null;
6969
}
7070

7171
@Override
72-
public <D> IOPlugin<D> getSaver(final D data, final String destination) {
72+
public <D> IOPlugin<D> getSaver(D data, Location destination) {
7373
for (final IOPlugin<?> handler : getInstances()) {
7474
if (handler.supportsSave(data, destination)) {
7575
@SuppressWarnings("unchecked")
@@ -81,7 +81,7 @@ public <D> IOPlugin<D> getSaver(final D data, final String destination) {
8181
}
8282

8383
@Override
84-
public Object open(final String source) throws IOException {
84+
public Object open(final Location source) throws IOException {
8585
final IOPlugin<?> opener = getOpener(source);
8686
if (opener == null) return null; // no appropriate IOPlugin
8787

@@ -93,7 +93,7 @@ public Object open(final String source) throws IOException {
9393
}
9494

9595
@Override
96-
public void save(final Object data, final String destination)
96+
public void save(final Object data, final Location destination)
9797
throws IOException
9898
{
9999
final IOPlugin<Object> saver = getSaver(data, destination);
@@ -112,8 +112,8 @@ public Class<IOPlugin<?>> getPluginType() {
112112
}
113113

114114
@Override
115-
public Class<String> getType() {
116-
return String.class;
115+
public Class<Location> getType() {
116+
return Location.class;
117117
}
118118

119119
}

src/main/java/org/scijava/io/DefaultRecentFileService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ public void initialize() {
178178

179179
@EventHandler
180180
protected void onEvent(final IOEvent event) {
181-
add(event.getDescriptor());
181+
final Location loc = event.getLocation();
182+
if (!(loc instanceof FileLocation)) return;
183+
final FileLocation fileLoc = (FileLocation) loc;
184+
add(fileLoc.getFile().getPath());
182185
}
183186

184187
// -- Helper methods --

src/main/java/org/scijava/io/IOPlugin.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@
5050
* @see Plugin
5151
* @see IOService
5252
*/
53-
public interface IOPlugin<D> extends HandlerPlugin<String> {
53+
public interface IOPlugin<D> extends HandlerPlugin<Location> {
5454

5555
/** The type of data opened and/or saved by the plugin. */
5656
Class<D> getDataType();
5757

58-
/** Checks whether the I/O plugin can open data from the given source. */
59-
boolean supportsOpen(String source);
58+
/** Checks whether the I/O plugin can open data from the given location. */
59+
boolean supportsOpen(Location source);
6060

61-
/** Checks whether the I/O plugin can save data to the given destination. */
62-
boolean supportsSave(String destination);
61+
/** Checks whether the I/O plugin can save data to the given location. */
62+
boolean supportsSave(Location destination);
63+
64+
/** Opens data from the given location. */
65+
D open(Location source) throws IOException;
6366

6467
/**
6568
* Checks whether the I/O plugin can save the given data to the specified
66-
* destination.
69+
* location.
6770
*/
68-
boolean supportsSave(Object data, String destination);
69-
70-
/** Opens data from the given source. */
71-
D open(String source) throws IOException;
71+
boolean supportsSave(Object data, Location destination);
7272

73-
/** Saves the given data to the specified destination. */
74-
void save(D data, String destination) throws IOException;
73+
/** Saves the given data to the specified location. */
74+
void save(D data, Location destination) throws IOException;
7575

7676
}

src/main/java/org/scijava/io/IOService.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,52 +43,47 @@
4343
* @see DataHandleService
4444
* @see Location
4545
*/
46-
public interface IOService extends HandlerService<String, IOPlugin<?>>,
46+
public interface IOService extends HandlerService<Location, IOPlugin<?>>,
4747
SciJavaService
4848
{
4949

5050
/**
5151
* Gets the most appropriate {@link IOPlugin} for opening data from the given
52-
* source.
52+
* location.
5353
*/
54-
IOPlugin<?> getOpener(String source);
54+
IOPlugin<?> getOpener(Location source);
5555

5656
/**
5757
* Gets the most appropriate {@link IOPlugin} for saving data to the given
58-
* destination.
58+
* location.
5959
*/
60-
<D> IOPlugin<D> getSaver(D data, String destination);
60+
<D> IOPlugin<D> getSaver(D data, Location destination);
6161

6262
/**
63-
* Loads data from the given source. For extensibility, the nature of the
64-
* source is left intentionally general, but two common examples include file
65-
* paths and URLs.
63+
* Loads data from the given location.
6664
* <p>
6765
* The opener to use is automatically determined based on available
68-
* {@link IOPlugin}s; see {@link #getOpener(String)}.
66+
* {@link IOPlugin}s; see {@link #getOpener(Location)}.
6967
* </p>
7068
*
71-
* @param source The source (e.g., file path) from which to data should be
72-
* loaded.
69+
* @param source The location from which to data should be loaded.
7370
* @return An object representing the loaded data, or null if the source is
7471
* not supported.
7572
* @throws IOException if something goes wrong loading the data.
7673
*/
77-
Object open(String source) throws IOException;
74+
Object open(Location source) throws IOException;
7875

7976
/**
80-
* Saves data to the given destination. The nature of the destination is left
81-
* intentionally general, but the most common example is a file path.
77+
* Saves data to the given location.
8278
* <p>
8379
* The saver to use is automatically determined based on available
84-
* {@link IOPlugin}s; see {@link #getSaver(Object, String)}.
80+
* {@link IOPlugin}s; see {@link #getSaver(Object, Location)}.
8581
* </p>
8682
*
8783
* @param data The data to be saved to the destination.
88-
* @param destination The destination (e.g., file path) to which data should
89-
* be saved.
84+
* @param destination The destination location to which data should be saved.
9085
* @throws IOException if something goes wrong saving the data.
9186
*/
92-
void save(Object data, String destination) throws IOException;
87+
void save(Object data, Location destination) throws IOException;
9388

9489
}

src/main/java/org/scijava/io/console/OpenArgument.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.scijava.console.AbstractConsoleArgument;
3838
import org.scijava.console.ConsoleArgument;
3939
import org.scijava.display.DisplayService;
40+
import org.scijava.io.FileLocation;
4041
import org.scijava.io.IOService;
4142
import org.scijava.log.LogService;
4243
import org.scijava.plugin.Parameter;
@@ -69,7 +70,7 @@ public void handle(final LinkedList<String> args) {
6970
final String source = args.removeFirst();
7071

7172
try {
72-
final Object o = ioService.open(source);
73+
final Object o = ioService.open(new FileLocation(source));
7374
displayService.createDisplay(o);
7475
}
7576
catch (IOException exc) {

src/main/java/org/scijava/io/event/DataOpenedEvent.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,17 @@
3131

3232
package org.scijava.io.event;
3333

34+
import org.scijava.io.Location;
35+
3436
/**
35-
* An event indicating that data has been opened from a source.
37+
* An event indicating that data has been opened from a location.
3638
*
3739
* @author Curtis Rueden
3840
*/
3941
public class DataOpenedEvent extends IOEvent {
4042

41-
public DataOpenedEvent(final String source, final Object data) {
42-
super(source, data);
43-
}
44-
45-
// -- DataOpenedEvent methods --
46-
47-
/** Gets the source from which data was opened. */
48-
public String getSource() {
49-
return getDescriptor();
43+
public DataOpenedEvent(final Location location, final Object data) {
44+
super(location, data);
5045
}
5146

5247
}

src/main/java/org/scijava/io/event/DataSavedEvent.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,17 @@
3131

3232
package org.scijava.io.event;
3333

34+
import org.scijava.io.Location;
35+
3436
/**
3537
* An event indicating that data has been saved to a destination.
3638
*
3739
* @author Curtis Rueden
3840
*/
3941
public class DataSavedEvent extends IOEvent {
4042

41-
public DataSavedEvent(final String destination, final Object data) {
43+
public DataSavedEvent(final Location destination, final Object data) {
4244
super(destination, data);
4345
}
4446

45-
// -- DataSavedEvent methods --
46-
47-
/** Gets the destination to which data was saved. */
48-
public String getDestination() {
49-
return getDescriptor();
50-
}
51-
5247
}

src/main/java/org/scijava/io/event/IOEvent.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.io.event;
3333

3434
import org.scijava.event.SciJavaEvent;
35+
import org.scijava.io.Location;
3536

3637
/**
3738
* An event indicating that I/O (e.g., opening or saving) has occurred.
@@ -40,20 +41,20 @@
4041
*/
4142
public abstract class IOEvent extends SciJavaEvent {
4243

43-
/** The data descriptor (source or destination). */
44-
private final String descriptor;
44+
/** The data location (source or destination). */
45+
private final Location location;
4546

4647
/** The data for which I/O took place. */
4748
private final Object data;
4849

49-
public IOEvent(final String descriptor, final Object data) {
50-
this.descriptor = descriptor;
50+
public IOEvent(final Location location, final Object data) {
51+
this.location = location;
5152
this.data = data;
5253
}
5354

54-
/** Gets the data descriptor (source or destination). */
55-
public String getDescriptor() {
56-
return descriptor;
55+
/** Gets the data location (source or destination). */
56+
public Location getLocation() {
57+
return location;
5758
}
5859

5960
/** Gets the data for which I/O took place. */
@@ -65,7 +66,8 @@ public Object getData() {
6566

6667
@Override
6768
public String toString() {
68-
return super.toString() + "\n\tdescriptor = " + data + "\n\tdata = " + data;
69+
return super.toString() + "\n\tlocation = " + location + "\n\tdata = " +
70+
data;
6971
}
7072

7173
}

src/main/java/org/scijava/script/io/ScriptIOPlugin.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import java.io.IOException;
3535

3636
import org.scijava.io.AbstractIOPlugin;
37+
import org.scijava.io.FileLocation;
3738
import org.scijava.io.IOPlugin;
39+
import org.scijava.io.Location;
3840
import org.scijava.plugin.Parameter;
3941
import org.scijava.plugin.Plugin;
4042
import org.scijava.script.ScriptService;
@@ -59,13 +61,16 @@ public Class<String> getDataType() {
5961
}
6062

6163
@Override
62-
public boolean supportsOpen(final String source) {
64+
public boolean supportsOpen(final Location source) {
6365
if (scriptService == null) return false; // no service for opening scripts
64-
return scriptService.canHandleFile(source);
66+
// TODO: Update ScriptService to use Location instead of File.
67+
if (!(source instanceof FileLocation)) return false;
68+
final FileLocation loc = (FileLocation) source;
69+
return scriptService.canHandleFile(loc.getFile());
6570
}
6671

6772
@Override
68-
public String open(final String source) throws IOException {
73+
public String open(final Location source) throws IOException {
6974
if (scriptService == null) return null; // no service for opening scripts
7075
// TODO: Use the script service to open the file in the script editor.
7176
return null;

0 commit comments

Comments
 (0)