org.peaseplate.service
Interface ConversionService

All Known Implementing Classes:
DefaultConversionService

public interface ConversionService

The conversion service is responsible for converting values from one data type to another. The service can be enhances by adding ConversionInitializers to it.

Author:
Manfred Hantschel

Method Summary
 void add(java.lang.Class<? extends ConversionInitializer> initializerClass)
          Adds the conversion initializer specified by its class name to the service.
<SOURCE,TARGET>
void
add(java.lang.Class<SOURCE> sourceType, java.lang.Class<TARGET> targetType, Conversion<SOURCE,TARGET> conversion)
          Adds a conversion from the specified source type to the specified target type to the service.
 void add(java.lang.ClassLoader... classLoaders)
          Scans the specified class loaders for conversion service definitions.
 void add(ConversionInitializer initializer)
          Adds the conversions in the conversion initializer to the service.
<SOURCE,TARGET>
TARGET
convert(SOURCE value, java.lang.Class<TARGET> targetType)
          Converts the specified value to the specified target type.
<SOURCE,TARGET>
TARGET
convey(SOURCE value, java.lang.Class<TARGET> targetType)
          Converts the specified value to the specified target type.
<SOURCE,TARGET>
boolean
isConvertable(java.lang.Class<SOURCE> sourceType, java.lang.Class<TARGET> targetType)
          Returns true if there is a conversion rule defined for the specified source type and the specified target type.
 

Method Detail

add

void add(java.lang.ClassLoader... classLoaders)
         throws java.lang.IllegalArgumentException
Scans the specified class loaders for conversion service definitions. The definitions are located in a resource with the name "META-INF/services/org.peaseplate.service.ConversionService". The file contains class names of ConversionInitializers, one class name per line.

Parameters:
classLoaders - the class loaders
Throws:
java.lang.IllegalArgumentException - if a class could not be instantiated

add

void add(java.lang.Class<? extends ConversionInitializer> initializerClass)
         throws java.lang.IllegalArgumentException
Adds the conversion initializer specified by its class name to the service. The instance will be created immediately, passed to the add(ConversionInitializer) method and then thrown away.

Parameters:
initializerClass - the class
Throws:
java.lang.IllegalArgumentException - if the class could not be instantiated

add

void add(ConversionInitializer initializer)
Adds the conversions in the conversion initializer to the service. The initializer itself will not be stored and can be thrown away.

Parameters:
initializer - the initializer

add

<SOURCE,TARGET> void add(java.lang.Class<SOURCE> sourceType,
                         java.lang.Class<TARGET> targetType,
                         Conversion<SOURCE,TARGET> conversion)
Adds a conversion from the specified source type to the specified target type to the service.

Type Parameters:
SOURCE - the source type
TARGET - the target type
Parameters:
sourceType - the source type
targetType - the target type
conversion - the conversion

convey

<SOURCE,TARGET> TARGET convey(SOURCE value,
                              java.lang.Class<TARGET> targetType)
              throws ConversionException
Converts the specified value to the specified target type. If the value is null, it converts this value according to the conversion rule.

Type Parameters:
SOURCE - the source type
TARGET - the target type
Parameters:
value - the value, may be null
targetType - the target type
Returns:
the converted value
Throws:
ConversionException - if something went wrong during converting

convert

<SOURCE,TARGET> TARGET convert(SOURCE value,
                               java.lang.Class<TARGET> targetType)
               throws ConversionException
Converts the specified value to the specified target type. If the value is null, it returns null, and does not use the conversion rule.

Type Parameters:
SOURCE - the source type
TARGET - the target type
Parameters:
value - the value, may be null
targetType - the target type
Returns:
the converted value
Throws:
ConversionException - if something went wrong during converting

isConvertable

<SOURCE,TARGET> boolean isConvertable(java.lang.Class<SOURCE> sourceType,
                                      java.lang.Class<TARGET> targetType)
Returns true if there is a conversion rule defined for the specified source type and the specified target type.

Type Parameters:
SOURCE - the source type
TARGET - the target type
Parameters:
sourceType - the source type
targetType - the target type
Returns:
true if conversion rule is defined


Copyright © 2008. All Rights Reserved.