From - The type to translate values from and to.To - The type to translate values to and from.public interface Translator<From,To> extends Converter<From,To>
Translator is used to convert (transform, translate, project,
evaluate, ...) values from one form into another and back. As such, a
Translator is little more than an arbitrary function and its reveres
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> uuidTranslator = new Translator<String, UUID> () {
Override
public UUID convert(String uuidString) throws ConverterException {
try{
return UUID.fromString(uuidString);
} catch (IllegalArgumentException e) {
throw new ConverterException(e);
}
}
Override
public String revert(UUID uuid) {
return uuid.toString();
}
};
Converter<Entity, Integer> idConverter = new Converter<Entity, Integer> () {
Override
public Integer convert(Entity entity) throws ConverterException {
return entity.getId();
}
Override
public Entity revert(Integer id) {
try{
return EntityDao.getById(id);
} catch (EntityDaoException e) {
throw new ConverterException(e);
}
}
};
Converter| Modifier and Type | Method and Description |
|---|---|
From |
revert(To to)
Called to revert a given value.
|
From revert(To to) throws ConversionException
Converter.convert(Object).
Depending on the use case, if the given value null, the
Translator 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.
to - The value to be reverted.ConversionException - If the reversion failed.Copyright © 2015–2016 Markenwerk – Gesellschaft für markenbildende Maßnahmen mbH. All rights reserved.