public interface Converter<I,O> extends HandlerPlugin<ConversionRequest>
Plugin
for converting between classes and
types.
NB: by default, the provided canConvert(org.scijava.convert.ConversionRequest)
methods will return
false
if the input is null
. This allows Converter
implementors to assume any input is non-null
. Casting
null Object
inputs is handled by the NullConverter
, while
null
class inputs are handled by the DefaultConverter
.
ConversionRequest
Modifier and Type | Method and Description |
---|---|
default boolean |
canConvert(Class<?> src,
Class<?> dest)
Checks whether objects of the given class can be converted to the specified
type.
|
default boolean |
canConvert(Class<?> src,
Type dest)
Checks whether objects of the given class can be converted to the specified
type.
|
default boolean |
canConvert(ConversionRequest request)
Checks whether a given
ConversionRequest can be processed, by
converting the desired ConversionRequest.sourceClass() to its
ConversionRequest.destClass() or
ConversionRequest.destType() . |
default boolean |
canConvert(Object src,
Class<?> dest)
Checks whether the given object's type can be converted to the specified
type.
|
default boolean |
canConvert(Object src,
Type dest)
Checks whether the given object's type can be converted to the specified
type.
|
default Object |
convert(ConversionRequest request)
Converts the given
ConversionRequest.sourceObject() to the
specified ConversionRequest.destClass() or
ConversionRequest.destType() . |
<T> T |
convert(Object src,
Class<T> dest)
Converts the given object to an object of the specified type.
|
default Object |
convert(Object src,
Type dest)
As
convert(Object, Class) but capable of creating and populating
multi-element objects (Collection s and array types). |
Class<I> |
getInputType() |
Class<O> |
getOutputType() |
default Class<ConversionRequest> |
getType()
Gets the type associated with the object.
|
void |
populateInputCandidates(Collection<Object> objects)
Populates the given collection with objects which are known to exist, and
which are usable as inputs for this converter.
|
default boolean |
supports(ConversionRequest request)
Gets whether this object is compatible with the given data object.
|
getIdentifier, log
context, getContext, setContext
compareTo, getPriority, setPriority
getInfo, setInfo
getLocation
getVersion
default boolean canConvert(ConversionRequest request)
ConversionRequest
can be processed, by
converting the desired ConversionRequest.sourceClass()
to its
ConversionRequest.destClass()
or
ConversionRequest.destType()
.convert(ConversionRequest)
default boolean canConvert(Object src, Type dest)
convert(Object, Type)
default boolean canConvert(Object src, Class<?> dest)
convert(Object, Class)
default boolean canConvert(Class<?> src, Type dest)
Note that this does not necessarily entail that
convert(Object, Type)
on a specific object of the given source
class will succeed. For example:
canConvert(String.class, List<Integer>)
will return true
because a String
can in general be converted to an Integer
and then wrapped into a List
, but calling
convert("5.1", List<Integer>)
will throw a
NumberFormatException
when the conversion is actually attempted via
the Integer(String)
constructor.
convert(Object, Type)
default boolean canConvert(Class<?> src, Class<?> dest)
Note that this does not necessarily entail that
convert(Object, Class)
on a specific object of the given source
class will succeed. For example:
canConvert(String.class, int.class)
will return true
because a String
can in general be converted to an int
, but
calling convert("5.1", int.class)
will throw a
NumberFormatException
when the conversion is actually attempted via
the Integer(String)
constructor.
convert(Object, Class)
default Object convert(ConversionRequest request)
ConversionRequest.sourceObject()
to the
specified ConversionRequest.destClass()
or
ConversionRequest.destType()
.request
- ConversionRequest
to process.convert(Object, Class)
,
convert(Object, Type)
default Object convert(Object src, Type dest)
convert(Object, Class)
but capable of creating and populating
multi-element objects (Collection
s and array types). If a single
element type is provided, it will be converted the same as
convert(Object, Class)
. If a multi-element type is detected, then
the value parameter will be interpreted as potential collection of values.
An appropriate container will be created, and the full set of values will
be type converted and added.
NB: This method should be capable of creating any array type, but if a
Collection
interface or abstract class is provided we can only make
a best guess as to what container type to instantiate; defaults are
provided for Set
, Queue
, and List
.
src
- The object to convert.dest
- Type to which the object should be converted.<T> T convert(Object src, Class<T> dest)
String
, which uses the
Object.toString()
method instead). In the case of primitive types,
returns an object of the corresponding wrapped type. If the destination
type does not have an appropriate constructor, returns null.T
- Type to which the object should be converted.src
- The object to convert.dest
- Type to which the object should be converted.void populateInputCandidates(Collection<Object> objects)
That is: each such object added to the collection would return true
if queried with converter.canConvert(object)
, and hence would
produce an output of type getOutputType()
if passed to
converter.convert(object)
.
The means by which "known objects" are determined is implementation
dependent, although the most typical use case is to query the
ObjectService
for known objects of type getInputType()
,
and return those. But other behaviors are possible, depending on the
converter implementation.
objects
- an initialized collection into which appropriate objects
will be inserted.default boolean supports(ConversionRequest request)
Typed
By default, this method will return true
iff the data is assignable
to the associated type given by Typed.getType()
. But individual
implementations may have other requirements beyond class assignability.
supports
in interface Typed<ConversionRequest>
default Class<ConversionRequest> getType()
Typed
getType
in interface Typed<ConversionRequest>
Copyright © 2009–2023 SciJava. All rights reserved.