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.

ConverterUtil.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.converter;
  17. import java.io.Serializable;
  18. import java.util.Locale;
  19. import com.vaadin.server.VaadinSession;
  20. public class ConverterUtil implements Serializable {
  21. /**
  22. * Finds a converter that can convert from the given presentation type to
  23. * the given model type and back. Uses the given application to find a
  24. * {@link ConverterFactory} or, if application is null, uses the
  25. * {@link VaadinSession#getCurrent()}.
  26. *
  27. * @param <PRESENTATIONTYPE>
  28. * the presentation type
  29. * @param <MODELTYPE>
  30. * the model type
  31. * @param presentationType
  32. * the presentation type
  33. * @param modelType
  34. * the model type
  35. * @param session
  36. * the session to use to find a ConverterFactory or null to use
  37. * the current session
  38. * @return a Converter capable of converting between the given types or null
  39. * if no converter was found
  40. */
  41. public static <PRESENTATIONTYPE, MODELTYPE> Converter<PRESENTATIONTYPE, MODELTYPE> getConverter(
  42. Class<PRESENTATIONTYPE> presentationType,
  43. Class<MODELTYPE> modelType, VaadinSession session) {
  44. Converter<PRESENTATIONTYPE, MODELTYPE> converter = null;
  45. if (session == null) {
  46. session = VaadinSession.getCurrent();
  47. }
  48. if (session != null) {
  49. ConverterFactory factory = session.getConverterFactory();
  50. converter = factory.createConverter(presentationType, modelType);
  51. }
  52. return converter;
  53. }
  54. /**
  55. * Convert the given value from the data source type to the UI type.
  56. *
  57. * @param modelValue
  58. * the model value to convert
  59. * @param presentationType
  60. * the type of the presentation value
  61. * @param converter
  62. * the converter to use
  63. * @param locale
  64. * the locale to use for conversion
  65. * @param <PRESENTATIONTYPE>
  66. * the presentation type
  67. * @param <MODELTYPE>
  68. * the model type
  69. *
  70. * @return the converted value, compatible with the presentation type, or
  71. * the original value if its type is compatible and no converter is
  72. * set.
  73. * @throws Converter.ConversionException
  74. * if there was a problem converting the value
  75. */
  76. @SuppressWarnings("unchecked")
  77. public static <PRESENTATIONTYPE, MODELTYPE> PRESENTATIONTYPE convertFromModel(
  78. MODELTYPE modelValue,
  79. Class<? extends PRESENTATIONTYPE> presentationType,
  80. Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale)
  81. throws Converter.ConversionException {
  82. if (converter != null) {
  83. /*
  84. * If there is a converter, always use it. It must convert or throw
  85. * an exception.
  86. */
  87. PRESENTATIONTYPE presentation = converter.convertToPresentation(
  88. modelValue, presentationType, locale);
  89. if (presentation != null
  90. && !presentationType.isInstance(presentation)) {
  91. throw new Converter.ConversionException(
  92. "Converter returned an object of type "
  93. + presentation.getClass().getName()
  94. + " when expecting "
  95. + presentationType.getName());
  96. }
  97. return presentation;
  98. }
  99. if (modelValue == null) {
  100. // Null should always be passed through the converter but if there
  101. // is no converter we can safely return null
  102. return null;
  103. }
  104. if (presentationType.isAssignableFrom(modelValue.getClass())) {
  105. return (PRESENTATIONTYPE) modelValue;
  106. } else {
  107. throw new Converter.ConversionException(
  108. "Unable to convert value of type "
  109. + modelValue.getClass().getName()
  110. + " to presentation type "
  111. + presentationType
  112. + ". No converter is set and the types are not compatible.");
  113. }
  114. }
  115. /**
  116. * Convert the given value from the presentation (UI) type to model (data
  117. * source) type.
  118. *
  119. * @param presentationValue
  120. * the presentation value to convert
  121. * @param modelType
  122. * the type of the model
  123. * @param converter
  124. * the converter to use
  125. * @param locale
  126. * the locale to use for conversion
  127. * @param <PRESENTATIONTYPE>
  128. * the presentation type
  129. * @param <MODELTYPE>
  130. * the model type
  131. *
  132. * @return the converted value, compatible with the model type, or the
  133. * original value if its type is compatible and no converter is set.
  134. * @throws Converter.ConversionException
  135. * if there was a problem converting the value
  136. */
  137. public static <MODELTYPE, PRESENTATIONTYPE> MODELTYPE convertToModel(
  138. PRESENTATIONTYPE presentationValue, Class<MODELTYPE> modelType,
  139. Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale)
  140. throws Converter.ConversionException {
  141. if (converter != null) {
  142. /*
  143. * If there is a converter, always use it. It must convert or throw
  144. * an exception.
  145. */
  146. MODELTYPE model = converter.convertToModel(presentationValue,
  147. modelType, locale);
  148. if (model != null && !modelType.isInstance(model)) {
  149. throw new Converter.ConversionException(
  150. "Converter returned an object of type "
  151. + model.getClass().getName()
  152. + " when expecting " + modelType.getName());
  153. }
  154. return model;
  155. }
  156. if (presentationValue == null) {
  157. // Null should always be passed through the converter but if there
  158. // is no converter we can safely return null
  159. return null;
  160. }
  161. if (modelType == null) {
  162. // No model type, return original value
  163. return (MODELTYPE) presentationValue;
  164. } else if (modelType.isAssignableFrom(presentationValue.getClass())) {
  165. // presentation type directly compatible with model type
  166. return modelType.cast(presentationValue);
  167. } else {
  168. throw new Converter.ConversionException(
  169. "Unable to convert value of type "
  170. + presentationValue.getClass().getName()
  171. + " to model type "
  172. + modelType
  173. + ". No converter is set and the types are not compatible.");
  174. }
  175. }
  176. /**
  177. * Checks if the given converter can handle conversion between the given
  178. * presentation and model type. Does strict type checking and only returns
  179. * true if the converter claims it can handle exactly the given types.
  180. *
  181. * @see #canConverterPossiblyHandle(Converter, Class, Class)
  182. *
  183. * @param converter
  184. * The converter to check. If this is null the result is always
  185. * false.
  186. * @param presentationType
  187. * The presentation type
  188. * @param modelType
  189. * The model type
  190. * @return true if the converter supports conversion between the given
  191. * presentation and model type, false otherwise
  192. */
  193. public static boolean canConverterHandle(Converter<?, ?> converter,
  194. Class<?> presentationType, Class<?> modelType) {
  195. if (converter == null) {
  196. return false;
  197. }
  198. if (modelType != converter.getModelType()) {
  199. return false;
  200. }
  201. if (presentationType != converter.getPresentationType()) {
  202. return false;
  203. }
  204. return true;
  205. }
  206. /**
  207. * Checks if it possible that the given converter can handle conversion
  208. * between the given presentation and model type somehow.
  209. *
  210. * @param converter
  211. * The converter to check. If this is null the result is always
  212. * false.
  213. * @param presentationType
  214. * The presentation type
  215. * @param modelType
  216. * The model type
  217. * @return true if the converter possibly support conversion between the
  218. * given presentation and model type, false otherwise
  219. */
  220. public static boolean canConverterPossiblyHandle(Converter<?, ?> converter,
  221. Class<?> presentationType, Class<?> modelType) {
  222. if (converter == null) {
  223. return false;
  224. }
  225. Class<?> converterModelType = converter.getModelType();
  226. if (!modelType.isAssignableFrom(converterModelType)
  227. && !converterModelType.isAssignableFrom(modelType)) {
  228. // model types are not compatible in any way
  229. return false;
  230. }
  231. Class<?> converterPresentationType = converter.getPresentationType();
  232. if (!presentationType.isAssignableFrom(converterPresentationType)
  233. && !converterPresentationType
  234. .isAssignableFrom(presentationType)) {
  235. // presentation types are not compatible in any way
  236. return false;
  237. }
  238. return true;
  239. }
  240. }