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

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