01: package org.jreform.criteria;
02:
03: import org.jreform.Criterion;
04:
05: /**
06: * An abstract criterion that verifies itself and sets an error message
07: * if validation fails.
08: *
09: * @author armandino (at) gmail.com
10: */
11: public abstract class AbstractCriterion<T> implements Criterion<T> {
12: private String messageOnError;
13:
14: /**
15: * Verifies that the argument satisfies this criterion.
16: */
17: protected abstract boolean verify(T value);
18:
19: /**
20: * Generates an error message for this criterion.
21: */
22: protected abstract String generateErrorMessage();
23:
24: public final boolean isSatisfied(T value) {
25: boolean isSatisfied = value != null && verify(value);
26:
27: if (value == null) {
28: setOnError("Please enter a valid value");
29: } else if (!isSatisfied) {
30: setOnError(generateErrorMessage());
31: }
32:
33: return isSatisfied;
34: }
35:
36: public final String getOnError() {
37: return messageOnError;
38: }
39:
40: public final void setOnError(String messageOnError) {
41: this.messageOnError = messageOnError;
42: }
43:
44: }
|