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.

StringLengthValidator.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2000-2018 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.validator;
  17. import com.vaadin.data.ValidationResult;
  18. import com.vaadin.data.ValueContext;
  19. /**
  20. * Verifies that the length of a string is within the given range.
  21. *
  22. * @author Vaadin Ltd.
  23. * @since 8.0
  24. */
  25. @SuppressWarnings("serial")
  26. public class StringLengthValidator extends AbstractValidator<String> {
  27. private final RangeValidator<Integer> validator;
  28. /**
  29. * Creates a new StringLengthValidator with a given error message and
  30. * minimum and maximum length limits.
  31. *
  32. * @param errorMessage
  33. * the error message to return if validation fails
  34. * @param minLength
  35. * the minimum permissible length of the string or null for no
  36. * limit.
  37. * @param maxLength
  38. * the maximum permissible length of the string or null for no
  39. * limit.
  40. */
  41. public StringLengthValidator(String errorMessage, Integer minLength,
  42. Integer maxLength) {
  43. super(errorMessage);
  44. validator = RangeValidator.of(errorMessage, minLength, maxLength);
  45. }
  46. @Override
  47. public ValidationResult apply(String value, ValueContext context) {
  48. if (value == null) {
  49. return toResult(value, true);
  50. }
  51. ValidationResult lengthCheck = validator.apply(value.length(), context);
  52. return toResult(value, !lengthCheck.isError());
  53. }
  54. /**
  55. * Gets the maximum permissible length of the string.
  56. *
  57. * @return the maximum length of the string or null if there is no limit
  58. */
  59. public Integer getMaxLength() {
  60. return validator.getMaxValue();
  61. }
  62. /**
  63. * Gets the minimum permissible length of the string.
  64. *
  65. * @return the minimum length of the string or null if there is no limit
  66. */
  67. public Integer getMinLength() {
  68. return validator.getMinValue();
  69. }
  70. /**
  71. * Sets the maximum permissible length of the string.
  72. *
  73. * @param maxLength
  74. * the maximum length to accept or null for no limit
  75. */
  76. public void setMaxLength(Integer maxLength) {
  77. validator.setMaxValue(maxLength);
  78. }
  79. /**
  80. * Sets the minimum permissible length.
  81. *
  82. * @param minLength
  83. * the minimum length to accept or null for no limit
  84. */
  85. public void setMinLength(Integer minLength) {
  86. validator.setMaxValue(minLength);
  87. }
  88. @Override
  89. public String toString() {
  90. return String.format("%s[%d, %d]", getClass().getSimpleName(),
  91. getMinLength(), getMaxLength());
  92. }
  93. }