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.

NullValidator.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.validator;
  5. import com.vaadin.data.Validator;
  6. /**
  7. * This validator is used for validating properties that do or do not allow null
  8. * values. By default, nulls are not allowed.
  9. *
  10. * @author Vaadin Ltd.
  11. * @version
  12. * @VERSION@
  13. * @since 3.0
  14. */
  15. @SuppressWarnings("serial")
  16. public class NullValidator implements Validator {
  17. private boolean onlyNullAllowed;
  18. private String errorMessage;
  19. /**
  20. * Creates a new NullValidator.
  21. *
  22. * @param errorMessage
  23. * the error message to display on invalidation.
  24. * @param onlyNullAllowed
  25. * Are only nulls allowed?
  26. */
  27. public NullValidator(String errorMessage, boolean onlyNullAllowed) {
  28. setErrorMessage(errorMessage);
  29. setNullAllowed(onlyNullAllowed);
  30. }
  31. /**
  32. * Validates the data given in value.
  33. *
  34. * @param value
  35. * the value to validate.
  36. * @throws Validator.InvalidValueException
  37. * if the value was invalid.
  38. */
  39. public void validate(Object value) throws Validator.InvalidValueException {
  40. if ((onlyNullAllowed && value != null)
  41. || (!onlyNullAllowed && value == null)) {
  42. throw new Validator.InvalidValueException(errorMessage);
  43. }
  44. }
  45. /**
  46. * Tests if the given value is valid.
  47. *
  48. * @param value
  49. * the value to validate.
  50. * @returns <code>true</code> for valid value, otherwise <code>false</code>.
  51. */
  52. public boolean isValid(Object value) {
  53. return onlyNullAllowed ? value == null : value != null;
  54. }
  55. /**
  56. * Returns <code>true</code> if nulls are allowed otherwise
  57. * <code>false</code>.
  58. */
  59. public final boolean isNullAllowed() {
  60. return onlyNullAllowed;
  61. }
  62. /**
  63. * Sets if nulls (and only nulls) are to be allowed.
  64. *
  65. * @param onlyNullAllowed
  66. * If true, only nulls are allowed. If false only non-nulls are
  67. * allowed. Do we allow nulls?
  68. */
  69. public void setNullAllowed(boolean onlyNullAllowed) {
  70. this.onlyNullAllowed = onlyNullAllowed;
  71. }
  72. /**
  73. * Gets the error message that is displayed in case the value is invalid.
  74. *
  75. * @return the Error Message.
  76. */
  77. public String getErrorMessage() {
  78. return errorMessage;
  79. }
  80. /**
  81. * Sets the error message to be displayed on invalid value.
  82. *
  83. * @param errorMessage
  84. * the Error Message to set.
  85. */
  86. public void setErrorMessage(String errorMessage) {
  87. this.errorMessage = errorMessage;
  88. }
  89. }