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.

AbstractValidator.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.validator;
  5. import com.vaadin.data.Validator;
  6. /**
  7. * Abstract {@link com.vaadin.data.Validator Validator} implementation that
  8. * provides a basic Validator implementation except the
  9. * {@link #isValidValue(Object)} method.
  10. * <p>
  11. * To include the value that failed validation in the exception message you can
  12. * use "{0}" in the error message. This will be replaced with the failed value
  13. * (converted to string using {@link #toString()}) or "null" if the value is
  14. * null.
  15. * </p>
  16. * <p>
  17. * The default implementation of AbstractValidator does not support HTML in
  18. * error messages. To enable HTML support, override
  19. * {@link InvalidValueException#getHtmlMessage()} and throw such exceptions from
  20. * {@link #validate(Object)}.
  21. * </p>
  22. * <p>
  23. * Since Vaadin 7, subclasses can either implement {@link #validate(Object)}
  24. * directly or implement {@link #isValidValue(Object)} when migrating legacy
  25. * applications. To check validity, {@link #validate(Object)} should be used.
  26. * </p>
  27. *
  28. * @param <T>
  29. * The type
  30. * @author Vaadin Ltd.
  31. * @version
  32. * @VERSION@
  33. * @since 5.4
  34. */
  35. public abstract class AbstractValidator<T> implements Validator {
  36. /**
  37. * Error message that is included in an {@link InvalidValueException} if
  38. * such is thrown.
  39. */
  40. private String errorMessage;
  41. /**
  42. * Constructs a validator with the given error message.
  43. *
  44. * @param errorMessage
  45. * the message to be included in an {@link InvalidValueException}
  46. * (with "{0}" replaced by the value that failed validation).
  47. */
  48. public AbstractValidator(String errorMessage) {
  49. this.errorMessage = errorMessage;
  50. }
  51. /**
  52. * Since Vaadin 7, subclasses of AbstractValidator should override
  53. * {@link #isValidValue(Object)} or {@link #validate(Object)} instead of
  54. * {@link #isValid(Object)}. {@link #validate(Object)} should normally be
  55. * used to check values.
  56. *
  57. * @param value
  58. * @return true if the value is valid
  59. */
  60. public boolean isValid(Object value) {
  61. try {
  62. validate(value);
  63. return true;
  64. } catch (InvalidValueException e) {
  65. return false;
  66. }
  67. }
  68. /**
  69. * Internally check the validity of a value. This method can be used to
  70. * perform validation in subclasses if customization of the error message is
  71. * not needed. Otherwise, subclasses should override
  72. * {@link #validate(Object)} and the return value of this method is ignored.
  73. *
  74. * This method should not be called from outside the validator class itself.
  75. *
  76. * @param value
  77. * @return
  78. */
  79. protected abstract boolean isValidValue(T value);
  80. @Override
  81. public void validate(Object value) throws InvalidValueException {
  82. // isValidType ensures that value can safely be cast to TYPE
  83. if (!isValidType(value) || !isValidValue((T) value)) {
  84. String message = getErrorMessage().replace("{0}",
  85. String.valueOf(value));
  86. throw new InvalidValueException(message);
  87. }
  88. }
  89. /**
  90. * Checks the type of the value to validate to ensure it conforms with
  91. * getType. Enables sub classes to handle the specific type instead of
  92. * Object.
  93. *
  94. * @param value
  95. * The value to check
  96. * @return true if the value can safely be cast to the type specified by
  97. * {@link #getType()}
  98. */
  99. protected boolean isValidType(Object value) {
  100. if (value == null) {
  101. return true;
  102. }
  103. return getType().isAssignableFrom(value.getClass());
  104. }
  105. /**
  106. * Returns the message to be included in the exception in case the value
  107. * does not validate.
  108. *
  109. * @return the error message provided in the constructor or using
  110. * {@link #setErrorMessage(String)}.
  111. */
  112. public String getErrorMessage() {
  113. return errorMessage;
  114. }
  115. /**
  116. * Sets the message to be included in the exception in case the value does
  117. * not validate. The exception message is typically shown to the end user.
  118. *
  119. * @param errorMessage
  120. * the error message. "{0}" is automatically replaced by the
  121. * value that did not validate.
  122. */
  123. public void setErrorMessage(String errorMessage) {
  124. this.errorMessage = errorMessage;
  125. }
  126. public abstract Class<T> getType();
  127. }