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.

StringToBooleanConverter.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.util.Locale;
  18. /**
  19. * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by
  20. * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}.
  21. * <p> Leading and trailing white spaces are ignored when converting from a String. </p>
  22. * <p> For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}</p>
  23. *
  24. * @author Vaadin Ltd
  25. * @since 7.0
  26. */
  27. public class StringToBooleanConverter implements Converter<String, Boolean> {
  28. private final String trueString;
  29. private final String falseString;
  30. /**
  31. * Creates converter with default string representations - "true" and "false"
  32. *
  33. */
  34. public StringToBooleanConverter() {
  35. this(Boolean.TRUE.toString(), Boolean.FALSE.toString());
  36. }
  37. /**
  38. * Creates converter with custom string representation.
  39. *
  40. * @since 7.5.4
  41. * @param falseString string representation for <code>false</code>
  42. * @param trueString string representation for <code>true</code>
  43. */
  44. public StringToBooleanConverter(String trueString, String falseString) {
  45. this.trueString = trueString;
  46. this.falseString = falseString;
  47. }
  48. /*
  49. * (non-Javadoc)
  50. *
  51. * @see
  52. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  53. * java.lang.Class, java.util.Locale)
  54. */
  55. @Override
  56. public Boolean convertToModel(String value,
  57. Class<? extends Boolean> targetType, Locale locale)
  58. throws ConversionException {
  59. if (value == null || value.isEmpty()) {
  60. return null;
  61. }
  62. // Remove leading and trailing white space
  63. value = value.trim();
  64. if (getTrueString().equals(value)) {
  65. return true;
  66. } else if (getFalseString().equals(value)) {
  67. return false;
  68. } else {
  69. throw new ConversionException("Cannot convert " + value + " to "
  70. + getModelType().getName());
  71. }
  72. }
  73. /**
  74. * Gets the string representation for true. Default is "true", if not set in constructor.
  75. *
  76. * @return the string representation for true
  77. */
  78. protected String getTrueString() {
  79. return trueString;
  80. }
  81. /**
  82. * Gets the string representation for false. Default is "false", if not set in constructor.
  83. *
  84. * @return the string representation for false
  85. */
  86. protected String getFalseString() {
  87. return falseString;
  88. }
  89. /*
  90. * (non-Javadoc)
  91. *
  92. * @see
  93. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  94. * .Object, java.lang.Class, java.util.Locale)
  95. */
  96. @Override
  97. public String convertToPresentation(Boolean value,
  98. Class<? extends String> targetType, Locale locale)
  99. throws ConversionException {
  100. if (value == null) {
  101. return null;
  102. }
  103. if (value) {
  104. return getTrueString(locale);
  105. } else {
  106. return getFalseString(locale);
  107. }
  108. }
  109. /**
  110. * Gets the locale-depended string representation for false.
  111. * Default is locale-independent value provided by {@link #getFalseString()}
  112. *
  113. * @since 7.5.4
  114. * @param locale to be used
  115. * @return the string representation for false
  116. */
  117. protected String getFalseString(Locale locale) {
  118. return getFalseString();
  119. }
  120. /**
  121. * Gets the locale-depended string representation for true.
  122. * Default is locale-independent value provided by {@link #getTrueString()}
  123. *
  124. * @since 7.5.4
  125. * @param locale to be used
  126. * @return the string representation for true
  127. */
  128. protected String getTrueString(Locale locale) {
  129. return getTrueString();
  130. }
  131. /*
  132. * (non-Javadoc)
  133. *
  134. * @see com.vaadin.data.util.converter.Converter#getModelType()
  135. */
  136. @Override
  137. public Class<Boolean> getModelType() {
  138. return Boolean.class;
  139. }
  140. /*
  141. * (non-Javadoc)
  142. *
  143. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  144. */
  145. @Override
  146. public Class<String> getPresentationType() {
  147. return String.class;
  148. }
  149. }