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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2000-2016 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.v7.data.util.converter;
  17. import java.util.Locale;
  18. /**
  19. * A converter that wraps another {@link Converter} and reverses source and
  20. * target types.
  21. *
  22. * @param <MODEL>
  23. * The source type
  24. * @param <PRESENTATION>
  25. * The target type
  26. *
  27. * @author Vaadin Ltd
  28. * @since 7.0
  29. */
  30. public class ReverseConverter<PRESENTATION, MODEL>
  31. implements Converter<PRESENTATION, MODEL> {
  32. private Converter<MODEL, PRESENTATION> realConverter;
  33. /**
  34. * Creates a converter from source to target based on a converter that
  35. * converts from target to source.
  36. *
  37. * @param converter
  38. * The converter to use in a reverse fashion
  39. */
  40. public ReverseConverter(Converter<MODEL, PRESENTATION> converter) {
  41. this.realConverter = converter;
  42. }
  43. /*
  44. * (non-Javadoc)
  45. *
  46. * @see com.vaadin.data.util.converter.Converter#convertToModel(java
  47. * .lang.Object, java.util.Locale)
  48. */
  49. @Override
  50. public MODEL convertToModel(PRESENTATION value,
  51. Class<? extends MODEL> targetType, Locale locale)
  52. throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
  53. return realConverter.convertToPresentation(value, targetType, locale);
  54. }
  55. /*
  56. * (non-Javadoc)
  57. *
  58. * @see
  59. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  60. * .Object, java.util.Locale)
  61. */
  62. @Override
  63. public PRESENTATION convertToPresentation(MODEL value,
  64. Class<? extends PRESENTATION> targetType, Locale locale)
  65. throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
  66. return realConverter.convertToModel(value, targetType, locale);
  67. }
  68. /*
  69. * (non-Javadoc)
  70. *
  71. * @see com.vaadin.data.util.converter.Converter#getSourceType()
  72. */
  73. @Override
  74. public Class<MODEL> getModelType() {
  75. return realConverter.getPresentationType();
  76. }
  77. /*
  78. * (non-Javadoc)
  79. *
  80. * @see com.vaadin.data.util.converter.Converter#getTargetType()
  81. */
  82. @Override
  83. public Class<PRESENTATION> getPresentationType() {
  84. return realConverter.getModelType();
  85. }
  86. }