|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2020 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.io; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +import org.scijava.io.location.FileLocation; |
| 35 | +import org.scijava.io.location.Location; |
| 36 | +import org.scijava.plugin.HandlerService; |
| 37 | +import org.scijava.service.SciJavaService; |
| 38 | + |
| 39 | +/** |
| 40 | + * Interface for high-level data I/O: opening and saving data of a specific type. |
| 41 | + * |
| 42 | + * @author Curtis Rueden |
| 43 | + * @author Deborah Schmidt |
| 44 | + */ |
| 45 | +public interface TypedIOService<D> extends HandlerService<Location, IOPlugin<D>>, |
| 46 | + SciJavaService |
| 47 | +{ |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets the most appropriate {@link IOPlugin} for opening data from the given |
| 51 | + * location. |
| 52 | + */ |
| 53 | + default IOPlugin<D> getOpener(final String source) { |
| 54 | + return getOpener(new FileLocation(source)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets the most appropriate {@link IOPlugin} for opening data from the given |
| 59 | + * location. |
| 60 | + */ |
| 61 | + default IOPlugin<D> getOpener(Location source) { |
| 62 | + for (final IOPlugin<D> handler : getInstances()) { |
| 63 | + if (handler.supportsOpen(source)) return handler; |
| 64 | + } |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets the most appropriate {@link IOPlugin} for saving data to the given |
| 70 | + * location. |
| 71 | + */ |
| 72 | + default IOPlugin<D> getSaver(final D data, final String destination) { |
| 73 | + return getSaver(data, new FileLocation(destination)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets the most appropriate {@link IOPlugin} for saving data to the given |
| 78 | + * location. |
| 79 | + */ |
| 80 | + default IOPlugin<D> getSaver(D data, Location destination) { |
| 81 | + for (final IOPlugin<?> handler : getInstances()) { |
| 82 | + if (handler.supportsSave(data, destination)) { |
| 83 | + return (IOPlugin<D>) handler; |
| 84 | + } |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Loads data from the given source. For extensibility, the nature of the |
| 91 | + * source is left intentionally general, but two common examples include file |
| 92 | + * paths and URLs. |
| 93 | + * <p> |
| 94 | + * The opener to use is automatically determined based on available |
| 95 | + * {@link IOPlugin}s; see {@link #getOpener(String)}. |
| 96 | + * </p> |
| 97 | + * |
| 98 | + * @param source The source (e.g., file path) from which to data should be |
| 99 | + * loaded. |
| 100 | + * @return An object representing the loaded data, or null if the source is |
| 101 | + * not supported. |
| 102 | + * @throws IOException if something goes wrong loading the data. |
| 103 | + */ |
| 104 | + D open(String source) throws IOException; |
| 105 | + |
| 106 | + /** |
| 107 | + * Loads data from the given location. |
| 108 | + * <p> |
| 109 | + * The opener to use is automatically determined based on available |
| 110 | + * {@link IOPlugin}s; see {@link #getOpener(Location)}. |
| 111 | + * </p> |
| 112 | + * |
| 113 | + * @param source The location from which to data should be loaded. |
| 114 | + * @return An object representing the loaded data, or null if the source is |
| 115 | + * not supported. |
| 116 | + * @throws IOException if something goes wrong loading the data. |
| 117 | + */ |
| 118 | + D open(Location source) throws IOException; |
| 119 | + |
| 120 | + /** |
| 121 | + * Saves data to the given destination. The nature of the destination is left |
| 122 | + * intentionally general, but the most common example is a file path. |
| 123 | + * <p> |
| 124 | + * The saver to use is automatically determined based on available |
| 125 | + * {@link IOPlugin}s; see {@link #getSaver(Object, String)}. |
| 126 | + * </p> |
| 127 | + * |
| 128 | + * @param data The data to be saved to the destination. |
| 129 | + * @param destination The destination (e.g., file path) to which data should |
| 130 | + * be saved. |
| 131 | + * @throws IOException if something goes wrong saving the data. |
| 132 | + */ |
| 133 | + void save(D data, String destination) throws IOException; |
| 134 | + |
| 135 | + /** |
| 136 | + * Saves data to the given location. |
| 137 | + * <p> |
| 138 | + * The saver to use is automatically determined based on available |
| 139 | + * {@link IOPlugin}s; see {@link #getSaver(Object, Location)}. |
| 140 | + * </p> |
| 141 | + * |
| 142 | + * @param data The data to be saved to the destination. |
| 143 | + * @param destination The destination location to which data should be saved. |
| 144 | + * @throws IOException if something goes wrong saving the data. |
| 145 | + */ |
| 146 | + void save(D data, Location destination) throws IOException; |
| 147 | + |
| 148 | + boolean canOpen(String source); |
| 149 | + |
| 150 | + boolean canOpen(Location source); |
| 151 | + |
| 152 | + boolean canSave(D data, String destination); |
| 153 | + |
| 154 | + boolean canSave(D data, Location destination); |
| 155 | + |
| 156 | + // -- HandlerService methods -- |
| 157 | + |
| 158 | + @Override |
| 159 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 160 | + default Class<IOPlugin<D>> getPluginType() { |
| 161 | + return (Class) IOPlugin.class; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + default Class<Location> getType() { |
| 166 | + return Location.class; |
| 167 | + } |
| 168 | +} |
0 commit comments