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.

AbstractStringValidator.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.vaadin.data.validator;
  2. /**
  3. * Validator base class for validating strings. See
  4. * {@link com.vaadin.data.validator.AbstractValidator} for more
  5. * information.
  6. *
  7. * <p>
  8. * If the validation fails, the exception thrown contains the error message with
  9. * its argument 0 replaced with the string being validated.
  10. * </p>
  11. *
  12. * @author IT Mill Ltd.
  13. * @version
  14. * @VERSION@
  15. * @since 5.4
  16. */
  17. @SuppressWarnings("serial")
  18. public abstract class AbstractStringValidator extends AbstractValidator {
  19. /**
  20. * Constructs a validator for strings.
  21. * <p>
  22. * Null and empty string values are always accepted. To disallow empty
  23. * values, set the field being validated as required.
  24. * </p>
  25. *
  26. * @param errorMessage
  27. * the message included in the exception (with its parameter {0}
  28. * replaced by the string to be validated) in case the validation
  29. * fails
  30. */
  31. public AbstractStringValidator(String errorMessage) {
  32. super(errorMessage);
  33. }
  34. public boolean isValid(Object value) {
  35. if (value == null) {
  36. return true;
  37. }
  38. if (!(value instanceof String)) {
  39. return false;
  40. }
  41. return isValidString((String) value);
  42. }
  43. /**
  44. * Checks if the given string is valid.
  45. *
  46. * @param value
  47. * String to check. Can never be null.
  48. * @return true if the string is valid, false otherwise
  49. */
  50. protected abstract boolean isValidString(String value);
  51. }