public class DefaultConverter extends AbstractConverter<Object,Object>
Converter
implementation. Provides useful conversion
functionality for many common conversion cases.
Supported conversions include:
Constructor and Description |
---|
DefaultConverter() |
Modifier and Type | Method and Description |
---|---|
boolean |
canConvert(Class<?> src,
Class<?> dest)
Checks whether objects of the given class can be converted to the specified
type.
|
<T> T |
convert(Object src,
Class<T> dest)
Converts the given object to an object of the specified type.
|
Object |
convert(Object src,
Type dest)
As
Converter.convert(Object, Class) but capable of creating and populating
multi-element objects (Collection s and array types). |
Class<Object> |
getInputType() |
Class<Object> |
getOutputType() |
populateInputCandidates, supports
getInfo, getPriority, setInfo, setPriority, toString
context, getContext
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
canConvert, canConvert, canConvert, canConvert, convert, getType
getIdentifier, log
context, getContext, setContext
compareTo, getPriority, setPriority
getInfo, setInfo
getLocation
getVersion
public Object convert(Object src, Type dest)
Converter
Converter.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
Converter.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.public <T> T convert(Object src, Class<T> dest)
Converter
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.public Class<Object> getOutputType()
Class
this Converter
produces as output.public Class<Object> getInputType()
Class
this Converter
accepts as input.public boolean canConvert(Class<?> src, Class<?> dest)
Converter
Note that this does not necessarily entail that
Converter.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.
Converter.convert(Object, Class)
Copyright © 2009–2023 SciJava. All rights reserved.