Skip to content

Commit 009313f

Browse files
committed
Add capacity for type aliasing module inputs
Added methods ot the Converter class for identifying the Input and Output classes that can be converted between, and the populateInputs method - allowing Converters the potential to point consumers to potential input objects that can certainly be converted to appropriate output objects. Added API to the ConvertService to take advantage of the new Converter methods - aggregating the potential input objects from converters matching a queried output type.
1 parent dc87299 commit 009313f

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

src/main/java/org/scijava/convert/AbstractConverter.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,44 @@
3131

3232
package org.scijava.convert;
3333

34+
import java.util.Set;
35+
3436
import org.scijava.plugin.AbstractHandlerPlugin;
3537

3638
/**
37-
* Abstract superclass for {@link Converter} plugins. Performs
38-
* appropriate dispatching of {@link #canConvert(ConversionRequest)} and
39+
* Abstract superclass for {@link Converter} plugins. Performs appropriate
40+
* dispatching of {@link #canConvert(ConversionRequest)} and
3941
* {@link #convert(ConversionRequest)} calls based on the actual state of the
4042
* given {@link ConversionRequest}.
4143
* <p>
4244
* Note that the {@link #supports(ConversionRequest)} method is overridden as
4345
* well, to delegate to the appropriate {@link #canConvert}.
4446
* </p>
47+
* <p>
48+
* NB: by default, the {@link #populateInputs(Set)} method has a dummy
49+
* implementation. Effectively, this is opt-in behavior. If a subclass of this
50+
* would like to declare automatic mappings between input and output conversion
51+
* types, this method can be overridden.
52+
* </p>
4553
*
4654
* @author Mark Hiner
4755
*/
4856
public abstract class AbstractConverter extends
4957
AbstractHandlerPlugin<ConversionRequest> implements Converter
5058
{
5159

60+
// -- Fields --
61+
62+
private final Class<?> inClass;
63+
private final Class<?> outClass;
64+
65+
// -- Constructor --
66+
67+
public AbstractConverter(final Class<?> inClass, final Class<?> outClass) {
68+
this.inClass = inClass;
69+
this.outClass = outClass;
70+
}
71+
5272
// -- ConversionHandler methods --
5373

5474
@Override
@@ -74,6 +94,21 @@ public Object convert(final ConversionRequest request) {
7494
return null;
7595
}
7696

97+
@Override
98+
public void populateInputs(Set<Object> objects) {
99+
// No-op
100+
}
101+
102+
@Override
103+
public Class<?> getOutputType() {
104+
return outClass;
105+
}
106+
107+
@Override
108+
public Class<?> getInputType() {
109+
return inClass;
110+
}
111+
77112
// -- Typed methods --
78113

79114
@Override

src/main/java/org/scijava/convert/ConvertService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.convert;
3333

3434
import java.lang.reflect.Type;
35+
import java.util.List;
3536

3637
import org.scijava.plugin.HandlerService;
3738

@@ -101,4 +102,6 @@ public interface ConvertService extends
101102
* @see #supports(ConversionRequest)
102103
*/
103104
boolean supports(Class<?> src, Type dest);
105+
106+
List<Object> getCompatibleInputs(Class<?> dest);
104107
}

src/main/java/org/scijava/convert/Converter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
* @see ConversionRequest
4747
* @author Mark Hiner
4848
*/
49-
public interface Converter extends HandlerPlugin<ConversionRequest> {
49+
public interface Converter extends HandlerPlugin<ConversionRequest>
50+
{
5051

5152
/**
5253
* Checks whether a given {@ConversionRequest} can be
@@ -155,4 +156,24 @@ public interface Converter extends HandlerPlugin<ConversionRequest> {
155156
* @return The conversion output
156157
*/
157158
Object convert(ConversionRequest request);
159+
160+
/**
161+
* Fills the given {@code Object} set with the potential input types known to
162+
* this {@code Converter}.
163+
*
164+
* @param objects A pre-allocated {@link Set} of potential input objects.
165+
* Input objects known to this {@code Converter} will be added to
166+
* this set.
167+
*/
168+
void populateInputs(Set<Object> objects);
169+
170+
/**
171+
* @return The base {@code Class} this {@code Converter} produces as output.
172+
*/
173+
Class<?> getOutputType();
174+
175+
/**
176+
* @return The base {@code Class} this {@code Converter} accepts as input.
177+
*/
178+
Class<?> getInputType();
158179
}

src/main/java/org/scijava/convert/DefaultConvertService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
package org.scijava.convert;
3333

3434
import java.lang.reflect.Type;
35+
import java.util.ArrayList;
36+
import java.util.HashSet;
37+
import java.util.List;
38+
import java.util.Set;
3539

3640
import org.scijava.plugin.Plugin;
3741
import org.scijava.service.Service;
@@ -96,4 +100,17 @@ public boolean supports(final Object src, final Type dest) {
96100
public boolean supports(final Class<?> src, final Type dest) {
97101
return supports(new ConversionRequest(src, dest));
98102
}
103+
104+
@Override
105+
public List<Object> getCompatibleInputs(Class<?> dest) {
106+
Set<Object> objects = new HashSet<Object>();
107+
108+
for (final Converter c : getInstances()) {
109+
if (dest.isAssignableFrom(c.getOutputType())) {
110+
c.populateInputs(objects);
111+
}
112+
}
113+
114+
return new ArrayList<Object>(objects);
115+
}
99116
}

src/main/java/org/scijava/convert/DefaultConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
@Plugin(type = Converter.class)
5858
public class DefaultConverter extends AbstractConverter {
5959

60+
// -- Constructor --
61+
62+
public DefaultConverter() {
63+
super(Object.class, Object.class);
64+
}
65+
6066
// -- ConversionHandler methods --
6167

6268
@Override

0 commit comments

Comments
 (0)