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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.validator;
  5. /**
  6. * This <code>StringLengthValidator</code> is used to validate the length of
  7. * strings.
  8. *
  9. * @author IT Mill Ltd.
  10. * @version
  11. * @VERSION@
  12. * @since 3.0
  13. */
  14. @SuppressWarnings("serial")
  15. public class StringLengthValidator extends AbstractValidator {
  16. private int minLength = -1;
  17. private int maxLength = -1;
  18. private boolean allowNull = true;
  19. /**
  20. * Creates a new StringLengthValidator with a given error message.
  21. *
  22. * @param errorMessage
  23. * the message to display in case the value does not validate.
  24. */
  25. public StringLengthValidator(String errorMessage) {
  26. super(errorMessage);
  27. }
  28. /**
  29. * Creates a new StringLengthValidator with a given error message,
  30. * permissable lengths and null-string allowance.
  31. *
  32. * @param errorMessage
  33. * the message to display in case the value does not validate.
  34. * @param minLength
  35. * the minimum permissible length of the string.
  36. * @param maxLength
  37. * the maximum permissible length of the string.
  38. * @param allowNull
  39. * Are null strings permissible? This can be handled better by
  40. * setting a field as required or not.
  41. */
  42. public StringLengthValidator(String errorMessage, int minLength,
  43. int maxLength, boolean allowNull) {
  44. this(errorMessage);
  45. setMinLength(minLength);
  46. setMaxLength(maxLength);
  47. setNullAllowed(allowNull);
  48. }
  49. /**
  50. * Checks if the given value is valid.
  51. *
  52. * @param value
  53. * the value to validate.
  54. * @return <code>true</code> for valid value, otherwise <code>false</code>.
  55. */
  56. public boolean isValid(Object value) {
  57. if (value == null) {
  58. return allowNull;
  59. }
  60. final String s = value.toString();
  61. if (s == null) {
  62. return allowNull;
  63. }
  64. final int len = s.length();
  65. if ((minLength >= 0 && len < minLength)
  66. || (maxLength >= 0 && len > maxLength)) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Returns <code>true</code> if null strings are allowed.
  73. *
  74. * @return <code>true</code> if allows null string, otherwise
  75. * <code>false</code>.
  76. */
  77. @Deprecated
  78. public final boolean isNullAllowed() {
  79. return allowNull;
  80. }
  81. /**
  82. * Gets the maximum permissible length of the string.
  83. *
  84. * @return the maximum length of the string.
  85. */
  86. public final int getMaxLength() {
  87. return maxLength;
  88. }
  89. /**
  90. * Gets the minimum permissible length of the string.
  91. *
  92. * @return the minimum length of the string.
  93. */
  94. public final int getMinLength() {
  95. return minLength;
  96. }
  97. /**
  98. * Sets whether null-strings are to be allowed. This can be better handled
  99. * by setting a field as required or not.
  100. */
  101. @Deprecated
  102. public void setNullAllowed(boolean allowNull) {
  103. this.allowNull = allowNull;
  104. }
  105. /**
  106. * Sets the maximum permissible length of the string.
  107. *
  108. * @param maxLength
  109. * the length to set.
  110. */
  111. public void setMaxLength(int maxLength) {
  112. if (maxLength < -1) {
  113. maxLength = -1;
  114. }
  115. this.maxLength = maxLength;
  116. }
  117. /**
  118. * Sets the minimum permissible length.
  119. *
  120. * @param minLength
  121. * the length to set.
  122. */
  123. public void setMinLength(int minLength) {
  124. if (minLength < -1) {
  125. minLength = -1;
  126. }
  127. this.minLength = minLength;
  128. }
  129. }