You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReverseConverter.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.converter;
  5. import java.util.Locale;
  6. /**
  7. * A converter that wraps another {@link Converter} and reverses source and
  8. * target types.
  9. *
  10. * @param <MODEL>
  11. * The source type
  12. * @param <PRESENTATION>
  13. * The target type
  14. *
  15. * @author Vaadin Ltd
  16. * @since 7.0
  17. */
  18. public class ReverseConverter<PRESENTATION, MODEL> implements
  19. Converter<PRESENTATION, MODEL> {
  20. private Converter<MODEL, PRESENTATION> realConverter;
  21. /**
  22. * Creates a converter from source to target based on a converter that
  23. * converts from target to source.
  24. *
  25. * @param converter
  26. * The converter to use in a reverse fashion
  27. */
  28. public ReverseConverter(Converter<MODEL, PRESENTATION> converter) {
  29. this.realConverter = converter;
  30. }
  31. /*
  32. * (non-Javadoc)
  33. *
  34. * @see com.vaadin.data.util.converter.Converter#convertToModel(java
  35. * .lang.Object, java.util.Locale)
  36. */
  37. @Override
  38. public MODEL convertToModel(PRESENTATION value, Locale locale)
  39. throws com.vaadin.data.util.converter.Converter.ConversionException {
  40. return realConverter.convertToPresentation(value, locale);
  41. }
  42. /*
  43. * (non-Javadoc)
  44. *
  45. * @see
  46. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  47. * .Object, java.util.Locale)
  48. */
  49. @Override
  50. public PRESENTATION convertToPresentation(MODEL value, Locale locale)
  51. throws com.vaadin.data.util.converter.Converter.ConversionException {
  52. return realConverter.convertToModel(value, locale);
  53. }
  54. /*
  55. * (non-Javadoc)
  56. *
  57. * @see com.vaadin.data.util.converter.Converter#getSourceType()
  58. */
  59. @Override
  60. public Class<MODEL> getModelType() {
  61. return realConverter.getPresentationType();
  62. }
  63. /*
  64. * (non-Javadoc)
  65. *
  66. * @see com.vaadin.data.util.converter.Converter#getTargetType()
  67. */
  68. @Override
  69. public Class<PRESENTATION> getPresentationType() {
  70. return realConverter.getModelType();
  71. }
  72. }