Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

StringLengthValidator.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.data.validator;
  5. import com.itmill.toolkit.data.Validator;
  6. /**
  7. * This <code>StringLengthValidator</code> is used to validate the length of
  8. * strings.
  9. *
  10. * @author IT Mill Ltd.
  11. * @version
  12. * @VERSION@
  13. * @since 3.0
  14. */
  15. public class StringLengthValidator implements Validator {
  16. private int minLength = -1;
  17. private int maxLength = -1;
  18. private boolean allowNull = true;
  19. private String errorMessage;
  20. /**
  21. * Creates a new StringLengthValidator with a given error message.
  22. *
  23. * @param errorMessage
  24. * the message to display in case the value does not
  25. * validate.
  26. */
  27. public StringLengthValidator(String errorMessage) {
  28. setErrorMessage(errorMessage);
  29. }
  30. /**
  31. * Creates a new StringLengthValidator with a given error message,
  32. * permissable lengths and null-string allowance.
  33. *
  34. * @param errorMessage
  35. * the message to display in case the value does not
  36. * validate.
  37. * @param minLength
  38. * the minimum permissable length of the string.
  39. * @param maxLength
  40. * the maximum permissable length of the string.
  41. * @param allowNull
  42. * Are null strings permissable?
  43. */
  44. public StringLengthValidator(String errorMessage, int minLength,
  45. int maxLength, boolean allowNull) {
  46. this(errorMessage);
  47. setMinLength(minLength);
  48. setMaxLength(maxLength);
  49. setNullAllowed(allowNull);
  50. }
  51. /**
  52. * Validates the value.
  53. *
  54. * @param value
  55. * the value to validate.
  56. * @throws Validator.InvalidValueException
  57. * if the value was invalid.
  58. */
  59. public void validate(Object value) throws Validator.InvalidValueException {
  60. if (value == null && !allowNull) {
  61. throw new Validator.InvalidValueException(errorMessage);
  62. }
  63. final String s = value.toString();
  64. if (s == null && !allowNull) {
  65. throw new Validator.InvalidValueException(errorMessage);
  66. }
  67. final int len = s.length();
  68. if ((minLength >= 0 && len < minLength)
  69. || (maxLength >= 0 && len > maxLength)) {
  70. throw new Validator.InvalidValueException(errorMessage);
  71. }
  72. }
  73. /**
  74. * Checks if the given value is valid.
  75. *
  76. * @param value
  77. * the value to validate.
  78. * @return <code>true</code> for valid value, otherwise <code>false</code>.
  79. */
  80. public boolean isValid(Object value) {
  81. if (value == null && !allowNull) {
  82. return true;
  83. }
  84. final String s = value.toString();
  85. if (s == null && !allowNull) {
  86. return true;
  87. }
  88. final int len = s.length();
  89. if ((minLength >= 0 && len < minLength)
  90. || (maxLength >= 0 && len > maxLength)) {
  91. return false;
  92. }
  93. return true;
  94. }
  95. /**
  96. * Returns <code>true</code> if null strings are allowed.
  97. *
  98. * @return <code>true</code> if allows null string, otherwise
  99. * <code>false</code>.
  100. */
  101. public final boolean isNullAllowed() {
  102. return allowNull;
  103. }
  104. /**
  105. * Gets the maximum permissable length of the string.
  106. *
  107. * @return the maximum length of the string.
  108. */
  109. public final int getMaxLength() {
  110. return maxLength;
  111. }
  112. /**
  113. * Gets the minimum permissable length of the string.
  114. *
  115. * @return the minimum length of the string.
  116. */
  117. public final int getMinLength() {
  118. return minLength;
  119. }
  120. /**
  121. * Sets whether null-strings are to be allowed.
  122. */
  123. public void setNullAllowed(boolean allowNull) {
  124. this.allowNull = allowNull;
  125. }
  126. /**
  127. * Sets the maximum permissable length of the string.
  128. *
  129. * @param maxLength
  130. * the length to set.
  131. */
  132. public void setMaxLength(int maxLength) {
  133. if (maxLength < -1) {
  134. maxLength = -1;
  135. }
  136. this.maxLength = maxLength;
  137. }
  138. /**
  139. * Sets the minimum permissable length.
  140. *
  141. * @param minLength
  142. * the length to set.
  143. */
  144. public void setMinLength(int minLength) {
  145. if (minLength < -1) {
  146. minLength = -1;
  147. }
  148. this.minLength = minLength;
  149. }
  150. /**
  151. * Gets the message to be displayed in case the value does not validate.
  152. *
  153. * @return the Error Message.
  154. */
  155. public String getErrorMessage() {
  156. return errorMessage;
  157. }
  158. /**
  159. * Sets the message to be displayer in case the value does not validate.
  160. *
  161. * @param errorMessage
  162. * the Error Message to set.
  163. */
  164. public void setErrorMessage(String errorMessage) {
  165. this.errorMessage = errorMessage;
  166. }
  167. }