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.

RangeValidator.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.validator;
  17. /**
  18. * An base implementation for validating any objects that implement
  19. * {@link Comparable}.
  20. *
  21. * Verifies that the value is of the given type and within the (optionally)
  22. * given limits. Typically you want to use a sub class of this like
  23. * {@link IntegerRangeValidator}, {@link DoubleRangeValidator} or
  24. * {@link DateRangeValidator} in applications.
  25. * <p>
  26. * Note that {@link RangeValidator} always accept null values. Make a field
  27. * required to ensure that no empty values are accepted or override
  28. * {@link #isValidValue(Comparable)}.
  29. * </p>
  30. *
  31. * @param <T>
  32. * The type of Number to validate. Must implement Comparable so that
  33. * minimum and maximum checks work.
  34. * @author Vaadin Ltd.
  35. * @since 7.0
  36. */
  37. public class RangeValidator<T extends Comparable> extends AbstractValidator<T> {
  38. private T minValue = null;
  39. private boolean minValueIncluded = true;
  40. private T maxValue = null;
  41. private boolean maxValueIncluded = true;
  42. private Class<T> type;
  43. /**
  44. * Creates a new range validator of the given type.
  45. *
  46. * @param errorMessage
  47. * The error message to use if validation fails
  48. * @param type
  49. * The type of object the validator can validate.
  50. * @param minValue
  51. * The minimum value that should be accepted or null for no limit
  52. * @param maxValue
  53. * The maximum value that should be accepted or null for no limit
  54. */
  55. public RangeValidator(String errorMessage, Class<T> type, T minValue,
  56. T maxValue) {
  57. super(errorMessage);
  58. this.type = type;
  59. this.minValue = minValue;
  60. this.maxValue = maxValue;
  61. }
  62. /**
  63. * Checks if the minimum value is part of the accepted range
  64. *
  65. * @return true if the minimum value is part of the range, false otherwise
  66. */
  67. public boolean isMinValueIncluded() {
  68. return minValueIncluded;
  69. }
  70. /**
  71. * Sets if the minimum value is part of the accepted range
  72. *
  73. * @param minValueIncluded
  74. * true if the minimum value should be part of the range, false
  75. * otherwise
  76. */
  77. public void setMinValueIncluded(boolean minValueIncluded) {
  78. this.minValueIncluded = minValueIncluded;
  79. }
  80. /**
  81. * Checks if the maximum value is part of the accepted range
  82. *
  83. * @return true if the maximum value is part of the range, false otherwise
  84. */
  85. public boolean isMaxValueIncluded() {
  86. return maxValueIncluded;
  87. }
  88. /**
  89. * Sets if the maximum value is part of the accepted range
  90. *
  91. * @param maxValueIncluded
  92. * true if the maximum value should be part of the range, false
  93. * otherwise
  94. */
  95. public void setMaxValueIncluded(boolean maxValueIncluded) {
  96. this.maxValueIncluded = maxValueIncluded;
  97. }
  98. /**
  99. * Gets the minimum value of the range
  100. *
  101. * @return the minimum value
  102. */
  103. public T getMinValue() {
  104. return minValue;
  105. }
  106. /**
  107. * Sets the minimum value of the range. Use
  108. * {@link #setMinValueIncluded(boolean)} to control whether this value is
  109. * part of the range or not.
  110. *
  111. * @param minValue
  112. * the minimum value
  113. */
  114. public void setMinValue(T minValue) {
  115. this.minValue = minValue;
  116. }
  117. /**
  118. * Gets the maximum value of the range
  119. *
  120. * @return the maximum value
  121. */
  122. public T getMaxValue() {
  123. return maxValue;
  124. }
  125. /**
  126. * Sets the maximum value of the range. Use
  127. * {@link #setMaxValueIncluded(boolean)} to control whether this value is
  128. * part of the range or not.
  129. *
  130. * @param maxValue
  131. * the maximum value
  132. */
  133. public void setMaxValue(T maxValue) {
  134. this.maxValue = maxValue;
  135. }
  136. /*
  137. * (non-Javadoc)
  138. *
  139. * @see
  140. * com.vaadin.data.validator.AbstractValidator#isValidValue(java.lang.Object
  141. * )
  142. */
  143. @Override
  144. protected boolean isValidValue(T value) {
  145. if (value == null
  146. || (String.class.equals(getType()) && "".equals(value))) {
  147. return true;
  148. }
  149. if (getMinValue() != null) {
  150. // Ensure that the min limit is ok
  151. int result = value.compareTo(getMinValue());
  152. if (result < 0) {
  153. // value less than min value
  154. return false;
  155. } else if (result == 0 && !isMinValueIncluded()) {
  156. // values equal and min value not included
  157. return false;
  158. }
  159. }
  160. if (getMaxValue() != null) {
  161. // Ensure that the Max limit is ok
  162. int result = value.compareTo(getMaxValue());
  163. if (result > 0) {
  164. // value greater than max value
  165. return false;
  166. } else if (result == 0 && !isMaxValueIncluded()) {
  167. // values equal and max value not included
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. /*
  174. * (non-Javadoc)
  175. *
  176. * @see com.vaadin.data.validator.AbstractValidator#getType()
  177. */
  178. @Override
  179. public Class<T> getType() {
  180. return type;
  181. }
  182. }