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.

Converter.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.converter;
  5. import java.io.Serializable;
  6. import java.util.Locale;
  7. /**
  8. * Interface that implements conversion between a model and a presentation type.
  9. * <p>
  10. * Typically {@link #convertToPresentation(Object, Locale)} and
  11. * {@link #convertToModel(Object, Locale)} should be symmetric so that chaining
  12. * these together returns the original result for all input but this is not a
  13. * requirement.
  14. * </p>
  15. * <p>
  16. * Converters must not have any side effects (never update UI from inside a
  17. * converter).
  18. * </p>
  19. * <p>
  20. * All Converters must be stateless and thread safe.
  21. * </p>
  22. * <p>
  23. * If conversion of a value fails, a {@link ConversionException} is thrown.
  24. * </p>
  25. *
  26. * @param <MODEL>
  27. * The model type. Must be compatible with what
  28. * {@link #getModelType()} returns.
  29. * @param <PRESENTATION>
  30. * The presentation type. Must be compatible with what
  31. * {@link #getPresentationType()} returns.
  32. * @author Vaadin Ltd.
  33. * @since 7.0
  34. */
  35. public interface Converter<PRESENTATION, MODEL> extends Serializable {
  36. /**
  37. * Converts the given value from target type to source type.
  38. * <p>
  39. * A converter can optionally use locale to do the conversion.
  40. * </p>
  41. * A converter should in most cases be symmetric so chaining
  42. * {@link #convertToPresentation(Object, Locale)} and
  43. * {@link #convertToModel(Object, Locale)} should return the original value.
  44. *
  45. * @param value
  46. * The value to convert, compatible with the target type. Can be
  47. * null
  48. * @param locale
  49. * The locale to use for conversion. Can be null.
  50. * @return The converted value compatible with the source type
  51. * @throws ConversionException
  52. * If the value could not be converted
  53. */
  54. public MODEL convertToModel(PRESENTATION value, Locale locale)
  55. throws ConversionException;
  56. /**
  57. * Converts the given value from source type to target type.
  58. * <p>
  59. * A converter can optionally use locale to do the conversion.
  60. * </p>
  61. * A converter should in most cases be symmetric so chaining
  62. * {@link #convertToPresentation(Object, Locale)} and
  63. * {@link #convertToModel(Object, Locale)} should return the original value.
  64. *
  65. * @param value
  66. * The value to convert, compatible with the target type. Can be
  67. * null
  68. * @param locale
  69. * The locale to use for conversion. Can be null.
  70. * @return The converted value compatible with the source type
  71. * @throws ConversionException
  72. * If the value could not be converted
  73. */
  74. public PRESENTATION convertToPresentation(MODEL value, Locale locale)
  75. throws ConversionException;
  76. /**
  77. * The source type of the converter.
  78. *
  79. * Values of this type can be passed to
  80. * {@link #convertToPresentation(Object, Locale)}.
  81. *
  82. * @return The source type
  83. */
  84. public Class<MODEL> getModelType();
  85. /**
  86. * The target type of the converter.
  87. *
  88. * Values of this type can be passed to
  89. * {@link #convertToModel(Object, Locale)}.
  90. *
  91. * @return The target type
  92. */
  93. public Class<PRESENTATION> getPresentationType();
  94. /**
  95. * An exception that signals that the value passed to
  96. * {@link Converter#convertToPresentation(Object, Locale)} or
  97. * {@link Converter#convertToModel(Object, Locale)} could not be converted.
  98. *
  99. * @author Vaadin Ltd
  100. * @version
  101. * @VERSION@
  102. * @since 7.0
  103. */
  104. public static class ConversionException extends RuntimeException {
  105. /**
  106. * Constructs a new <code>ConversionException</code> without a detail
  107. * message.
  108. */
  109. public ConversionException() {
  110. }
  111. /**
  112. * Constructs a new <code>ConversionException</code> with the specified
  113. * detail message.
  114. *
  115. * @param msg
  116. * the detail message
  117. */
  118. public ConversionException(String msg) {
  119. super(msg);
  120. }
  121. /**
  122. * Constructs a new {@code ConversionException} with the specified
  123. * cause.
  124. *
  125. * @param cause
  126. * The cause of the the exception
  127. */
  128. public ConversionException(Throwable cause) {
  129. super(cause);
  130. }
  131. /**
  132. * Constructs a new <code>ConversionException</code> with the specified
  133. * detail message and cause.
  134. *
  135. * @param message
  136. * the detail message
  137. * @param cause
  138. * The cause of the the exception
  139. */
  140. public ConversionException(String message, Throwable cause) {
  141. super(message, cause);
  142. }
  143. }
  144. }