Skip to content

Commit 13bf71b

Browse files
committed
WIP
1 parent aef704c commit 13bf71b

16 files changed

+635
-2
lines changed
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 - 2016 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+
package org.scijava.ext;
32+
33+
import java.io.IOException;
34+
35+
import org.scijava.plugin.AbstractSingletonService;
36+
import org.scijava.plugin.Plugin;
37+
38+
@SuppressWarnings("rawtypes")
39+
@Plugin(type = ExternalizationService.class)
40+
public class DefaultExternalizationService extends AbstractSingletonService<Externalizer>
41+
implements ExternalizationService {
42+
43+
@Override
44+
public Class<Externalizer> getPluginType() {
45+
return Externalizer.class;
46+
}
47+
48+
@SuppressWarnings("unchecked")
49+
@Override
50+
public <I extends ExternalizerInput, O extends ExternalizerOutput, T> Externalizer<I, O, T> get(
51+
final Class<I> inputType, final Class<O> outputType, final T type) {
52+
53+
for (final Externalizer ext : getInstances()) {
54+
if (ext.canExternalize(inputType, outputType, type)) {
55+
return ext;
56+
}
57+
}
58+
59+
throw new IllegalStateException("No externalizer found for input type: " + inputType.getSimpleName()
60+
+ " Output type: " + outputType.getSimpleName());
61+
}
62+
63+
@SuppressWarnings("unchecked")
64+
@Override
65+
public <I extends ExternalizerInput, T> T read(final I in, final Class<T> type) throws IOException {
66+
return (T) in.read().read(in);
67+
}
68+
69+
@Override
70+
public <O extends ExternalizerOutput, T> void write(final Externalizer<?, O, T> ext, final O out, final T obj)
71+
throws IOException {
72+
out.write(ext);
73+
ext.write(out, obj);
74+
}
75+
76+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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+
package org.scijava.ext;
32+
33+
import java.io.IOException;
34+
35+
import org.scijava.service.Service;
36+
37+
public interface ExternalizationService extends Service {
38+
39+
<I extends ExternalizerInput, O extends ExternalizerOutput, T> Externalizer<I, O, T> get(final Class<I> inType,
40+
final Class<O> outType, final T obj);
41+
42+
<I extends ExternalizerInput, T> T read(final I in, final Class<T> type) throws IOException;
43+
44+
<O extends ExternalizerOutput, T> void write(final Externalizer<?, O, T> ext, final O out, final T obj)
45+
throws IOException;
46+
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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+
package org.scijava.ext;
32+
33+
import java.io.IOException;
34+
35+
import org.scijava.plugin.SingletonPlugin;
36+
37+
public interface Externalizer<R, W, O> extends SingletonPlugin {
38+
39+
boolean canExternalize(final Class<?> readerType, final Class<?> writerType, final Object type);
40+
41+
void write(final W writer, final O obj) throws IOException;
42+
43+
O read(final R reader) throws IOException;
44+
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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+
package org.scijava.ext;
32+
33+
public interface ExternalizerInput {
34+
<R, W, O> Externalizer<R, W, O> read();
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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+
package org.scijava.ext;
32+
33+
public interface ExternalizerOutput {
34+
<R, W, O> void write(Externalizer<R, W, O> ext);
35+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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+
package org.scijava.ext.obj;
32+
33+
import java.io.ObjectInput;
34+
import java.io.ObjectOutput;
35+
36+
import org.scijava.plugin.AbstractRichPlugin;
37+
38+
public abstract class AbstractObjectIOExt<O> extends AbstractRichPlugin implements ObjectIOExternalizer<O> {
39+
40+
@Override
41+
public boolean canExternalize(final Class<?> inputType, final Class<?> outputType, Object obj) {
42+
return inputType.isAssignableFrom(ObjectInput.class) && outputType.isAssignableFrom(ObjectOutput.class)
43+
&& canExternalize(obj);
44+
}
45+
46+
protected abstract boolean canExternalize(final Object obj);
47+
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ext.obj;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInput;
5+
import java.io.ObjectOutput;
6+
7+
import org.scijava.plugin.Plugin;
8+
9+
@Plugin(type = ObjectIOExternalizer.class)
10+
public class BooleanArrayObjectIOExtV0 extends AbstractObjectIOExt<boolean[]> {
11+
12+
@Override
13+
public void write(final ObjectOutput oos, final boolean[] obj) {
14+
try {
15+
oos.writeObject(obj);
16+
} catch (IOException e) {
17+
throw new RuntimeException(e);
18+
}
19+
}
20+
21+
@Override
22+
public boolean[] read(final ObjectInput ois) {
23+
try {
24+
return (boolean[]) ois.readObject();
25+
} catch (IOException | ClassNotFoundException e) {
26+
throw new RuntimeException(e);
27+
}
28+
}
29+
30+
@Override
31+
protected boolean canExternalize(Object obj) {
32+
return obj instanceof boolean[];
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ext.obj;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInput;
5+
import java.io.ObjectOutput;
6+
7+
import org.scijava.plugin.Plugin;
8+
9+
@Plugin(type = ObjectIOExternalizer.class)
10+
public class ByteArrayObjectIOExtV0 extends AbstractObjectIOExt<byte[]> {
11+
12+
@Override
13+
public void write(final ObjectOutput oos, final byte[] obj) {
14+
try {
15+
oos.writeObject(obj);
16+
} catch (IOException e) {
17+
throw new RuntimeException(e);
18+
}
19+
}
20+
21+
@Override
22+
public byte[] read(final ObjectInput ois) {
23+
try {
24+
return (byte[]) ois.readObject();
25+
} catch (IOException | ClassNotFoundException e) {
26+
throw new RuntimeException(e);
27+
}
28+
}
29+
30+
@Override
31+
protected boolean canExternalize(final Object obj) {
32+
return obj instanceof byte[];
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.scijava.ext.obj;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInput;
5+
import java.io.ObjectOutput;
6+
7+
import org.scijava.plugin.Plugin;
8+
9+
@Plugin(type = ObjectIOExternalizer.class)
10+
public class DoubleArrayObjectIOExtV0 extends AbstractObjectIOExt<double[]> {
11+
12+
@Override
13+
public void write(final ObjectOutput oos, final double[] obj) {
14+
try {
15+
oos.writeObject(obj);
16+
} catch (IOException e) {
17+
throw new RuntimeException(e);
18+
}
19+
}
20+
21+
@Override
22+
public double[] read(final ObjectInput ois) {
23+
try {
24+
return (double[]) ois.readObject();
25+
} catch (IOException | ClassNotFoundException e) {
26+
throw new RuntimeException(e);
27+
}
28+
}
29+
30+
@Override
31+
protected boolean canExternalize(Object obj) {
32+
return obj instanceof double[];
33+
}
34+
}

0 commit comments

Comments
 (0)