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.
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);
}
}
};
| 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).
Implementers should catch any exception and wrap them in a
ConversionException.
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.
to - The value to be reverted.ConversionException - If the reversion failed.Copyright © 2015–2017 Markenwerk – Gesellschaft für markenbildende Maßnahmen mbH. All rights reserved.