From - The type to convert values from.To - The type to convert values to.public interface Converter<From,To>
Converter is used to convert (transform, translate, project,
evaluate, ...) values from one form into another. As such, a
Converter is little more than an arbitrary function. It is usually
used in a scenario where some sort of data provider wants to offer the
possibility to convert values into the desired type before delivery or during
the data processing.
Common use cases of a Converter include:
Converter<String, UUID> uuidConverter = new Converter<String, UUID> () {
Override
public UUID convert(String uuidString) throws ConverterException {
try{
return UUID.fromString(uuidString);
} catch (IllegalArgumentException e) {
throw new ConverterException(e);
}
}
};
Converter<Entity, Integer> idConverter = new Converter<Entity, Integer> () {
Override
public Integer convert(Entity entity) throws ConverterException {
return entity.getId();
}
};
Translator| Modifier and Type | Method and Description |
|---|---|
To |
convert(From from)
Called to convert a given value.
|
To convert(From from) throws ConversionException
Depending on the use case, if the given value null, the
Converter should return null.
It lies in the responsibility of the caller, to handle unwanted
null-values by replacing them with a sensible default value or
throwing a NullPointerException.
Implementers should catch any exception and wrap them in a
ConversionException.
from - The value to be converted.ConversionException - If the conversion failed.Copyright © 2015–2016 Markenwerk – Gesellschaft für markenbildende Maßnahmen mbH. All rights reserved.