Interface Converter<I,O>
-
- All Superinterfaces:
Comparable<Prioritized>,Contextual,HandlerPlugin<ConversionRequest>,HasPluginInfo,Identifiable,Locatable,Logged,Prioritized,RichPlugin,SciJavaPlugin,SingletonPlugin,Typed<ConversionRequest>,TypedPlugin<ConversionRequest>,Versioned
- All Known Implementing Classes:
AbstractConverter,AbstractDelegateConverter,ArrayConverters.BoolArrayUnwrapper,ArrayConverters.BoolArrayWrapper,ArrayConverters.ByteArrayUnwrapper,ArrayConverters.ByteArrayWrapper,ArrayConverters.CharArrayUnwrapper,ArrayConverters.CharArrayWrapper,ArrayConverters.DoubleArrayUnwrapper,ArrayConverters.DoubleArrayWrapper,ArrayConverters.FloatArrayUnwrapper,ArrayConverters.FloatArrayWrapper,ArrayConverters.IntArrayUnwrapper,ArrayConverters.IntArrayWrapper,ArrayConverters.LongArrayUnwrapper,ArrayConverters.LongArrayWrapper,ArrayConverters.ShortArrayUnwrapper,ArrayConverters.ShortArrayWrapper,ArrayToStringConverter,CastingConverter,DefaultConverter,FileListConverters.FileArrayToStringConverter,FileListConverters.FileToStringConverter,FileListConverters.StringToFileArrayConverter,FileListConverters.StringToFileConverter,FileToPathConverter,NullConverter,NumberConverters.BigIntegerToBigDecimalConverter,NumberConverters.ByteToBigDecimalConverter,NumberConverters.ByteToBigIntegerConverter,NumberConverters.ByteToDoubleConverter,NumberConverters.ByteToFloatConverter,NumberConverters.ByteToIntegerConverter,NumberConverters.ByteToLongConverter,NumberConverters.ByteToShortConverter,NumberConverters.DoubleToBigDecimalConverter,NumberConverters.FloatToBigDecimalConverter,NumberConverters.FloatToDoubleConverter,NumberConverters.IntegerToBigDecimalConverter,NumberConverters.IntegerToBigIntegerConverter,NumberConverters.IntegerToDoubleConverter,NumberConverters.IntegerToLongConverter,NumberConverters.LongToBigDecimalConverter,NumberConverters.LongToBigIntegerConverter,NumberConverters.ShortToBigDecimalConverter,NumberConverters.ShortToBigIntegerConverter,NumberConverters.ShortToDoubleConverter,NumberConverters.ShortToFloatConverter,NumberConverters.ShortToIntegerConverter,NumberConverters.ShortToLongConverter,NumberToBigDecimalConverter,NumberToBigIntegerConverter,NumberToDoubleConverter,NumberToFloatConverter,NumberToIntegerConverter,NumberToLongConverter,NumberToNumberConverter,NumberToShortConverter,PathToFileConverter,PrimitiveArrayUnwrapper,PrimitiveArrayWrapper,StringToArrayConverter,StringToNumberConverter
public interface Converter<I,O> extends HandlerPlugin<ConversionRequest>
Extensible conversionPluginfor converting between classes and types.NB: by default, the provided
canConvert(org.scijava.convert.ConversionRequest)methods will returnfalseif the input isnull. This allowsConverterimplementors to assume any input is non-null. Castingnull Objectinputs is handled by theNullConverter, whilenullclass inputs are handled by theDefaultConverter.- Author:
- Mark Hiner
- See Also:
ConversionRequest
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default booleancanConvert(Class<?> src, Class<?> dest)Checks whether objects of the given class can be converted to the specified type.default booleancanConvert(Class<?> src, Type dest)Checks whether objects of the given class can be converted to the specified type.default booleancanConvert(Object src, Class<?> dest)Checks whether the given object's type can be converted to the specified type.default booleancanConvert(Object src, Type dest)Checks whether the given object's type can be converted to the specified type.default booleancanConvert(ConversionRequest request)Checks whether a givenConversionRequestcan be processed, by converting the desiredConversionRequest.sourceClass()to itsConversionRequest.destClass()orConversionRequest.destType().<T> Tconvert(Object src, Class<T> dest)Converts the given object to an object of the specified type.default Objectconvert(Object src, Type dest)Asconvert(Object, Class)but capable of creating and populating multi-element objects (Collections and array types).default Objectconvert(ConversionRequest request)Converts the givenConversionRequest.sourceObject()to the specifiedConversionRequest.destClass()orConversionRequest.destType().Class<I>getInputType()Class<O>getOutputType()default Class<ConversionRequest>getType()Gets the type associated with the object.voidpopulateInputCandidates(Collection<Object> objects)Populates the given collection with objects which are known to exist, and which are usable as inputs for this converter.default booleansupports(ConversionRequest request)Gets whether this object is compatible with the given data object.-
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
-
Methods inherited from interface org.scijava.plugin.HasPluginInfo
getInfo, setInfo
-
Methods inherited from interface org.scijava.Locatable
getLocation
-
Methods inherited from interface org.scijava.Prioritized
compareTo, getPriority, setPriority
-
Methods inherited from interface org.scijava.plugin.RichPlugin
getIdentifier, log
-
Methods inherited from interface org.scijava.Versioned
getVersion
-
-
-
-
Method Detail
-
canConvert
default boolean canConvert(ConversionRequest request)
Checks whether a givenConversionRequestcan be processed, by converting the desiredConversionRequest.sourceClass()to itsConversionRequest.destClass()orConversionRequest.destType().- See Also:
convert(ConversionRequest)
-
canConvert
default boolean canConvert(Object src, Type dest)
Checks whether the given object's type can be converted to the specified type.- See Also:
convert(Object, Type)
-
canConvert
default boolean canConvert(Object src, Class<?> dest)
Checks whether the given object's type can be converted to the specified type.- See Also:
convert(Object, Class)
-
canConvert
default boolean canConvert(Class<?> src, Type dest)
Checks whether objects of the given class can be converted to the specified type.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 returntruebecause aStringcan in general be converted to anIntegerand then wrapped into aList, but callingconvert("5.1", List<Integer>)will throw aNumberFormatExceptionwhen the conversion is actually attempted via theInteger(String)constructor.- See Also:
convert(Object, Type)
-
canConvert
default boolean canConvert(Class<?> src, Class<?> dest)
Checks whether objects of the given class can be converted to the specified type.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 returntruebecause aStringcan in general be converted to anint, but callingconvert("5.1", int.class)will throw aNumberFormatExceptionwhen the conversion is actually attempted via theInteger(String)constructor.- See Also:
convert(Object, Class)
-
convert
default Object convert(ConversionRequest request)
Converts the givenConversionRequest.sourceObject()to the specifiedConversionRequest.destClass()orConversionRequest.destType().- Parameters:
request-ConversionRequestto process.- Returns:
- The conversion output
- See Also:
convert(Object, Class),convert(Object, Type)
-
convert
default Object convert(Object src, Type dest)
Asconvert(Object, Class)but capable of creating and populating multi-element objects (Collections and array types). If a single element type is provided, it will be converted the same asconvert(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
Collectioninterface or abstract class is provided we can only make a best guess as to what container type to instantiate; defaults are provided forSet,Queue, andList.- Parameters:
src- The object to convert.dest- Type to which the object should be converted.
-
convert
<T> T convert(Object src, Class<T> dest)
Converts the given object to an object of the specified type. The object is casted directly if possible, or else a new object is created using the destination type's public constructor that takes the original object as input (except when converting toString, which uses theObject.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.- Type Parameters:
T- Type to which the object should be converted.- Parameters:
src- The object to convert.dest- Type to which the object should be converted.
-
populateInputCandidates
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.That is: each such object added to the collection would return
trueif queried withconverter.canConvert(object), and hence would produce an output of typegetOutputType()if passed toconverter.convert(object).The means by which "known objects" are determined is implementation dependent, although the most typical use case is to query the
ObjectServicefor known objects of typegetInputType(), and return those. But other behaviors are possible, depending on the converter implementation.- Parameters:
objects- an initialized collection into which appropriate objects will be inserted.
-
supports
default boolean supports(ConversionRequest request)
Description copied from interface:TypedGets whether this object is compatible with the given data object.By default, this method will return
trueiff the data is assignable to the associated type given byTyped.getType(). But individual implementations may have other requirements beyond class assignability.
-
-