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.

CompositeValidator.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.validator;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import com.vaadin.data.Validator;
  10. /**
  11. * The <code>CompositeValidator</code> allows you to chain (compose) many
  12. * validators to validate one field. The contained validators may be required to
  13. * all validate the value to validate or it may be enough that one contained
  14. * validator validates the value. This behaviour is controlled by the modes
  15. * <code>AND</code> and <code>OR</code>.
  16. *
  17. * @author IT Mill Ltd.
  18. * @version
  19. * @VERSION@
  20. * @since 3.0
  21. */
  22. @SuppressWarnings("serial")
  23. public class CompositeValidator extends AbstractValidator {
  24. /**
  25. * The validators are combined with <code>AND</code> clause: validity of the
  26. * composite implies validity of the all validators it is composed of must
  27. * be valid.
  28. */
  29. public static final int MODE_AND = 0;
  30. /**
  31. * The validators are combined with <code>OR</code> clause: validity of the
  32. * composite implies that some of validators it is composed of must be
  33. * valid.
  34. */
  35. public static final int MODE_OR = 1;
  36. /**
  37. * The validators are combined with and clause: validity of the composite
  38. * implies validity of the all validators it is composed of
  39. */
  40. public static final int MODE_DEFAULT = MODE_AND;
  41. /**
  42. * Operation mode.
  43. */
  44. private int mode = MODE_DEFAULT;
  45. /**
  46. * List of contained validators.
  47. */
  48. private final List<Validator> validators = new LinkedList<Validator>();
  49. /**
  50. * Construct a composite validator in <code>AND</code> mode without error
  51. * message.
  52. */
  53. public CompositeValidator() {
  54. super("");
  55. }
  56. /**
  57. * Constructs a composite validator in given mode.
  58. */
  59. public CompositeValidator(int mode, String errorMessage) {
  60. super(errorMessage);
  61. setMode(mode);
  62. }
  63. /**
  64. * Validates the given value.
  65. * <p>
  66. * The value is valid, if:
  67. * <ul>
  68. * <li><code>MODE_AND</code>: All of the sub-validators are valid
  69. * <li><code>MODE_OR</code>: Any of the sub-validators are valid
  70. * </ul>
  71. *
  72. * If the value is invalid, validation error is thrown. If the error message
  73. * is set (non-null), it is used. If the error message has not been set, the
  74. * first error occurred is thrown.
  75. * </p>
  76. *
  77. * @param value
  78. * the value to check.
  79. * @throws Validator.InvalidValueException
  80. * if the value is not valid.
  81. */
  82. @Override
  83. public void validate(Object value) throws Validator.InvalidValueException {
  84. switch (mode) {
  85. case MODE_AND:
  86. for (Validator validator : validators) {
  87. validator.validate(value);
  88. }
  89. return;
  90. case MODE_OR:
  91. Validator.InvalidValueException first = null;
  92. for (Validator v : validators) {
  93. try {
  94. v.validate(value);
  95. return;
  96. } catch (final Validator.InvalidValueException e) {
  97. if (first == null) {
  98. first = e;
  99. }
  100. }
  101. }
  102. if (first == null) {
  103. return;
  104. }
  105. final String em = getErrorMessage();
  106. if (em != null) {
  107. throw new Validator.InvalidValueException(em);
  108. } else {
  109. throw first;
  110. }
  111. }
  112. throw new IllegalStateException(
  113. "The validator is in unsupported operation mode");
  114. }
  115. /**
  116. * Checks the validity of the the given value. The value is valid, if:
  117. * <ul>
  118. * <li><code>MODE_AND</code>: All of the sub-validators are valid
  119. * <li><code>MODE_OR</code>: Any of the sub-validators are valid
  120. * </ul>
  121. *
  122. * @param value
  123. * the value to check.
  124. */
  125. public boolean isValid(Object value) {
  126. switch (mode) {
  127. case MODE_AND:
  128. for (Validator v : validators) {
  129. if (!v.isValid(value)) {
  130. return false;
  131. }
  132. }
  133. return true;
  134. case MODE_OR:
  135. for (Validator v : validators) {
  136. if (v.isValid(value)) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. throw new IllegalStateException(
  143. "The valitor is in unsupported operation mode");
  144. }
  145. /**
  146. * Gets the mode of the validator.
  147. *
  148. * @return Operation mode of the validator: <code>MODE_AND</code> or
  149. * <code>MODE_OR</code>.
  150. */
  151. public final int getMode() {
  152. return mode;
  153. }
  154. /**
  155. * Sets the mode of the validator. The valid modes are:
  156. * <ul>
  157. * <li><code>MODE_AND</code> (default)
  158. * <li><code>MODE_OR</code>
  159. * </ul>
  160. *
  161. * @param mode
  162. * the mode to set.
  163. */
  164. public void setMode(int mode) {
  165. if (mode != MODE_AND && mode != MODE_OR) {
  166. throw new IllegalArgumentException("Mode " + mode + " unsupported");
  167. }
  168. this.mode = mode;
  169. }
  170. /**
  171. * Gets the error message for the composite validator. If the error message
  172. * is null, original error messages of the sub-validators are used instead.
  173. */
  174. @Override
  175. public String getErrorMessage() {
  176. if (getErrorMessage() != null) {
  177. return getErrorMessage();
  178. }
  179. // TODO Return composite error message
  180. return null;
  181. }
  182. /**
  183. * Adds validator to the interface.
  184. *
  185. * @param validator
  186. * the Validator object which performs validation checks on this
  187. * set of data field values.
  188. */
  189. public void addValidator(Validator validator) {
  190. if (validator == null) {
  191. return;
  192. }
  193. validators.add(validator);
  194. }
  195. /**
  196. * Removes a validator from the composite.
  197. *
  198. * @param validator
  199. * the Validator object which performs validation checks on this
  200. * set of data field values.
  201. */
  202. public void removeValidator(Validator validator) {
  203. validators.remove(validator);
  204. }
  205. /**
  206. * Gets sub-validators by class.
  207. *
  208. * <p>
  209. * If the component contains directly or recursively (it contains another
  210. * composite containing the validator) validators compatible with given type
  211. * they are returned. This only applies to <code>AND</code> mode composite
  212. * validators.
  213. * </p>
  214. *
  215. * <p>
  216. * If the validator is in <code>OR</code> mode or does not contain any
  217. * validators of given type null is returned.
  218. * </p>
  219. *
  220. * @return Collection<Validator> of validators compatible with given type
  221. * that must apply or null if none fould.
  222. */
  223. public Collection<Validator> getSubValidators(Class validatorType) {
  224. if (mode != MODE_AND) {
  225. return null;
  226. }
  227. final HashSet<Validator> found = new HashSet<Validator>();
  228. for (Validator v : validators) {
  229. if (validatorType.isAssignableFrom(v.getClass())) {
  230. found.add(v);
  231. }
  232. if (v instanceof CompositeValidator
  233. && ((CompositeValidator) v).getMode() == MODE_AND) {
  234. final Collection<Validator> c = ((CompositeValidator) v)
  235. .getSubValidators(validatorType);
  236. if (c != null) {
  237. found.addAll(c);
  238. }
  239. }
  240. }
  241. return found.isEmpty() ? null : found;
  242. }
  243. }