您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractValidator.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.validator;
  5. import com.vaadin.data.Validator;
  6. /**
  7. * Default Validator base class. See {@link com.vaadin.data.validator.Validator}
  8. * for more information.
  9. * <p>
  10. * If the validation fails, the exception thrown contains the error message with
  11. * its argument 0 replaced with the toString() of the object 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 AbstractValidator implements Validator {
  21. /**
  22. * Error message.
  23. */
  24. private String errorMessage;
  25. /**
  26. * Constructs a validator with an error message.
  27. *
  28. * @param errorMessage
  29. * the message included in the exception (with its parameter {0}
  30. * replaced by toString() of the object to be validated) in case
  31. * the validation fails
  32. */
  33. public AbstractValidator(String errorMessage) {
  34. this.errorMessage = errorMessage;
  35. }
  36. public void validate(Object value) throws InvalidValueException {
  37. if (!isValid(value)) {
  38. String message;
  39. if (value == null) {
  40. message = errorMessage.replace("{0}", "null");
  41. } else {
  42. message = errorMessage.replace("{0}", value.toString());
  43. }
  44. throw new InvalidValueException(message);
  45. }
  46. }
  47. /**
  48. * Gets the message to be displayed in case the value does not validate.
  49. *
  50. * @return the Error Message.
  51. */
  52. public String getErrorMessage() {
  53. return errorMessage;
  54. }
  55. /**
  56. * Sets the message to be displayed in case the value does not validate.
  57. *
  58. * @param errorMessage
  59. * the Error Message to set.
  60. */
  61. public void setErrorMessage(String errorMessage) {
  62. this.errorMessage = errorMessage;
  63. }
  64. }