Skip to content

Commit e93357f

Browse files
committed
Add a converter plugin for Nil objects
1 parent 30e655c commit e93357f

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2019 SciJava Ops 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.ops.types;
31+
32+
import java.lang.reflect.Type;
33+
34+
import org.scijava.Priority;
35+
import org.scijava.convert.AbstractConverter;
36+
import org.scijava.convert.Converter;
37+
import org.scijava.plugin.Plugin;
38+
import org.scijava.ops.types.Nil;
39+
import org.scijava.util.Types;
40+
41+
/**
42+
* {@link Converter} implementation for handling {@link Nil} values.
43+
*
44+
* @author Curtis Rueden
45+
*/
46+
@Plugin(type = Converter.class, priority = Priority.VERY_HIGH)
47+
public class NilConverter extends AbstractConverter<Nil<?>, Object> {
48+
49+
@Override
50+
public boolean canConvert(final Object src, final Type dest) {
51+
return src instanceof Nil && dest != null;
52+
}
53+
54+
@Override
55+
public boolean canConvert(final Object src, final Class<?> dest) {
56+
return src instanceof Nil && dest != null;
57+
}
58+
59+
@Override
60+
public boolean canConvert(final Class<?> src, final Class<?> dest) {
61+
return Nil.class.isAssignableFrom(src) && dest != null;
62+
}
63+
64+
@Override
65+
public Object convert(final Object src, final Type dest) {
66+
if (!(src instanceof Nil)) return null;
67+
final Nil<?> nil = (Nil<?>) src;
68+
69+
// special case for conversion to Nil of another type
70+
final Class<?> destClass = Types.raw(dest);
71+
if (destClass == Nil.class) {
72+
// convert to target Nil type, preserving the source's callbacks
73+
return Nil.of(((Nil<?>) dest).getType(), src);
74+
}
75+
76+
// Return a proxy object of the destination type,
77+
// with callbacks from the source Nil.
78+
// NB: The proxy might not _actually_ be of the requested type,
79+
// although it will implement all the same interfaces as that type.
80+
// CTR TODO: Consider whether to fail the conversion in this case.
81+
return Nil.of(dest, nil).proxy();
82+
}
83+
84+
@Override
85+
public Class<Object> getOutputType() {
86+
return Object.class;
87+
}
88+
89+
@Override
90+
@SuppressWarnings({ "rawtypes", "unchecked" })
91+
public Class<Nil<?>> getInputType() {
92+
return (Class) Nil.class;
93+
}
94+
95+
@Override
96+
public <T> T convert(final Object src, final Class<T> dest) {
97+
final Type type = dest;
98+
@SuppressWarnings("unchecked")
99+
final T result = (T) convert(src, type);
100+
return result;
101+
}
102+
103+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2019 SciJava Ops 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.ops.types;
31+
32+
import static org.junit.Assert.assertTrue;
33+
34+
import java.lang.reflect.Type;
35+
import java.util.List;
36+
import java.util.Map;
37+
import java.util.Observer;
38+
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.Test;
42+
import org.scijava.Context;
43+
import org.scijava.convert.ConvertService;
44+
import org.scijava.convert.Converter;
45+
import org.scijava.util.Types;
46+
47+
/**
48+
* Tests {@link NilConverter}.
49+
*
50+
* @author Curtis Rueden
51+
*/
52+
public class NilConverterTest {
53+
54+
private ConvertService convert;
55+
56+
/** Sets up a SciJava context and injects needed services. */
57+
@Before
58+
public void setUp() {
59+
convert = new Context(ConvertService.class).service(ConvertService.class);
60+
}
61+
62+
/**
63+
* Disposes of the {@link Context} that was initialized in {@link #setUp()}.
64+
*/
65+
@After
66+
public synchronized void cleanUp() {
67+
if (convert != null) {
68+
convert.context().dispose();
69+
convert = null;
70+
}
71+
}
72+
73+
@Test
74+
public void testCanConvert() {
75+
final Nil<?> nil = Nil.of(Observer.class);
76+
assertCanConvert(nil, Observer.class);
77+
assertCanConvert(nil, List.class);
78+
assertCanConvert(nil, new Nil<Map<?, ?>>() {}.getType());
79+
// TODO: Enable after ConvertService is rewritten to fully
80+
// support generic types. Right now, CastingConverter steals this.
81+
// assertCanConvert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType());
82+
}
83+
84+
@SuppressWarnings("cast")
85+
@Test
86+
public void testConvert() {
87+
final Nil<?> nil = Nil.of(Observer.class);
88+
assertConvert(nil, Observer.class);
89+
assertConvert(nil, List.class);
90+
assertConvert(nil, new Nil<Map<?, ?>>() {}.getType());
91+
// TODO: Enable after ConvertService is rewritten to fully
92+
// support generic types. Right now, CastingConverter steals this.
93+
// assertConvert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType());
94+
final Converter<?, ?> converter = convert.getHandler(nil, Observer.class);
95+
assertTrue(converter instanceof NilConverter);
96+
// TODO: Enable after Nil proxying is improved to support non-interfaces.
97+
// assertTrue(converter.convert(nil, String.class) instanceof String);
98+
assertTrue(converter.convert(nil, List.class) instanceof List);
99+
assertTrue(converter.convert(nil, new Nil<Map<?, ?>>() {}.getType()) instanceof Map);
100+
// TODO: Enable after ConvertService is rewritten to fully
101+
// support generic types. Right now, CastingConverter steals this.
102+
// assertTrue(converter.convert(nil, new Nil<Nil<Map<?, ?>>>() {}.getType()) instanceof Nil);
103+
}
104+
105+
private void assertCanConvert(final Nil<?> nil, final Type destType) {
106+
final Converter<?, ?> converter = convert.getHandler(nil, destType);
107+
assertTrue(converter instanceof NilConverter);
108+
assertTrue(converter.canConvert(nil, destType));
109+
}
110+
111+
private void assertConvert(final Nil<?> nil, final Type destType) {
112+
final Converter<?, ?> converter = convert.getHandler(nil, destType);
113+
assertTrue(converter instanceof NilConverter);
114+
final Object o = converter.convert(nil, destType);
115+
assertTrue(Types.raw(destType).isAssignableFrom(o.getClass()));
116+
}
117+
118+
}

0 commit comments

Comments
 (0)