org.scijava.convert
Class AbstractConverter<I,O>

java.lang.Object
  extended by org.scijava.AbstractContextual
      extended by org.scijava.plugin.AbstractRichPlugin
          extended by org.scijava.plugin.AbstractTypedPlugin<D>
              extended by org.scijava.plugin.AbstractHandlerPlugin<ConversionRequest>
                  extended by org.scijava.convert.AbstractConverter<I,O>
All Implemented Interfaces:
Comparable<Prioritized>, Contextual, Converter<I,O>, HandlerPlugin<ConversionRequest>, HasPluginInfo, RichPlugin, SciJavaPlugin, SingletonPlugin, TypedPlugin<ConversionRequest>, Prioritized, Typed<ConversionRequest>
Direct Known Subclasses:
DefaultConverter, NullConverter

public abstract class AbstractConverter<I,O>
extends AbstractHandlerPlugin<ConversionRequest>
implements Converter<I,O>

Abstract superclass for Converter plugins. Performs appropriate dispatching of canConvert(ConversionRequest) and convert(ConversionRequest) calls based on the actual state of the given ConversionRequest.

Note that the supports(ConversionRequest) method is overridden as well, to delegate to the appropriate canConvert(org.scijava.convert.ConversionRequest).

NB: by default, the populateInputCandidates(Collection) method has a dummy implementation. Effectively, this is opt-in behavior. If a converter implementation would like to suggest candidates for conversion, this method can be overridden.

Author:
Mark Hiner

Constructor Summary
AbstractConverter()
           
 
Method Summary
 boolean canConvert(Class<?> src, Type dest)
          Deprecated. 
 boolean canConvert(ConversionRequest request)
          Checks whether a given can be processed, by converting the desired ConversionRequest.sourceClass() to its ConversionRequest.destClass() or ConversionRequest.destType().
 boolean canConvert(Object src, Class<?> dest)
          Checks whether the given object's type can be converted to the specified type.
 boolean canConvert(Object src, Type dest)
          Checks whether the given object's type can be converted to the specified type.
 Object convert(ConversionRequest request)
          Converts the given ConversionRequest.sourceObject() to the specified ConversionRequest.destClass() or ConversionRequest.destType().
 Object convert(Object src, Type dest)
          As Converter.convert(Object, Class) but capable of creating and populating multi-element objects (Collections and array types).
 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.
 boolean supports(ConversionRequest request)
          Gets whether this object is compatible with the given data object.
 
Methods inherited from class org.scijava.plugin.AbstractRichPlugin
compareTo, getInfo, getPriority, setInfo, setPriority, toString
 
Methods inherited from class org.scijava.AbstractContextual
context, getContext, setContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.scijava.convert.Converter
canConvert, convert, getInputType, getOutputType
 
Methods inherited from interface org.scijava.Contextual
context, getContext, setContext
 
Methods inherited from interface org.scijava.Prioritized
getPriority, setPriority
 
Methods inherited from interface java.lang.Comparable
compareTo
 
Methods inherited from interface org.scijava.plugin.HasPluginInfo
getInfo, setInfo
 

Constructor Detail

AbstractConverter

public AbstractConverter()
Method Detail

canConvert

public boolean canConvert(ConversionRequest request)
Description copied from interface: Converter
Checks whether a given can be processed, by converting the desired ConversionRequest.sourceClass() to its ConversionRequest.destClass() or ConversionRequest.destType().

Specified by:
canConvert in interface Converter<I,O>
See Also:
Converter.convert(ConversionRequest)

canConvert

public boolean canConvert(Object src,
                          Type dest)
Description copied from interface: Converter
Checks whether the given object's type can be converted to the specified type.

Note that this does not necessarily entail that Converter.convert(Object, Type) on that specific object will succeed. For example: canConvert("5.1", 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.Integer(String) constructor.

Specified by:
canConvert in interface Converter<I,O>
See Also:
Converter.convert(Object, Type)

canConvert

public boolean canConvert(Object src,
                          Class<?> dest)
Description copied from interface: Converter
Checks whether the given object's type can be converted to the specified type.

Note that this does not necessarily entail that Converter.convert(Object, Class) on that specific object will succeed. For example: canConvert("5.1", 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.Integer(String) constructor.

Specified by:
canConvert in interface Converter<I,O>
See Also:
Converter.convert(Object, Class)

convert

public Object convert(Object src,
                      Type dest)
Description copied from interface: Converter
As Converter.convert(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 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 and List subclasses.

Specified by:
convert in interface Converter<I,O>
Parameters:
src - The object to convert.
dest - Type to which the object should be converted.

convert

public Object convert(ConversionRequest request)
Description copied from interface: Converter
Converts the given ConversionRequest.sourceObject() to the specified ConversionRequest.destClass() or ConversionRequest.destType().

Specified by:
convert in interface Converter<I,O>
Parameters:
request - ConversionRequest to process.
Returns:
The conversion output
See Also:
Converter.convert(Object, Class), Converter.convert(Object, Type)

populateInputCandidates

public void populateInputCandidates(Collection<Object> objects)
Description copied from interface: Converter
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 true if queried with converter.canConvert(object), and hence would produce an output of type Converter.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 Converter.getInputType(), and return those. But other behaviors are possible, depending on the converter implementation.

Specified by:
populateInputCandidates in interface Converter<I,O>
Parameters:
objects - an initialized collection into which appropriate objects will be inserted.

supports

public boolean supports(ConversionRequest request)
Description copied from interface: Typed
Gets whether this object is compatible with the given data object.

Typically, this will be the case when data.getClass() is assignable to the type associated with the object (i.e., the one returned by Typed.getType()). But individual implementations may have other requirements beyond class assignability.

Specified by:
supports in interface Typed<ConversionRequest>
Overrides:
supports in class AbstractTypedPlugin<ConversionRequest>

getType

public Class<ConversionRequest> getType()
Description copied from interface: Typed
Gets the type associated with the object.

Specified by:
getType in interface Typed<ConversionRequest>

canConvert

@Deprecated
public boolean canConvert(Class<?> src,
                                     Type dest)
Deprecated. 

Description copied from interface: Converter
Checks whether objects of the given class can be converted to the specified type.

Specified by:
canConvert in interface Converter<I,O>
See Also:
Converter.convert(Object, Type)


Copyright © 2009–2014 SciJava. All rights reserved.