1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.util.condition;
18
19 /**
20 * An abstract implementation of the {@link Condition} interface. This class takes care of the
21 * logical operation of the condition. It is highly recommended for condition implementation to
22 * sub-class this class when possible.
23 *
24 * @author Uri Boness
25 */
26 public abstract class AbstractCondition implements Condition {
27
28 /**
29 * See {@link Condition#check(Object)}.
30 * <p/>
31 * Delegates to the {@link #doCheck(Object)}. Provides callback methods for sub-classes to intercept
32 * the checking.
33 */
34 public final boolean check(Object object) {
35 beforeObjectChecked(object);
36 boolean result = doCheck(object);
37 return afterObjectChecked(object, result);
38 }
39
40 /**
41 * Performs the actual checking of this condition on the checked object.
42 *
43 * @param object The object to be checked.
44 * @return <code>true</code> if the given object adheres to this condition, <code>false</code> otherwise.
45 */
46 public abstract boolean doCheck(Object object);
47
48 /**
49 * See {@link Condition#and(Condition)}
50 */
51 public Condition and(Condition condition) {
52 return Conditions.and(this, condition);
53 }
54
55 /**
56 * See {@link Condition#or(Condition)}
57 */
58 public Condition or(Condition condition) {
59 return Conditions.or(this, condition);
60 }
61
62 /**
63 * A callback method that enables sub-classes intercept the object checking before it is
64 * being checked. Sub-classes may override this method and perform custom assertions and prevent the
65 * checking by throwing an {@link IllegalArgumentException};
66 *
67 * @param object
68 */
69 protected void beforeObjectChecked(Object object) throws IllegalArgumentException {
70 }
71
72 /**
73 * A callback method that enables sub-classes to intercept the object checking after it was checked. Sub-classes
74 * can override this method and change the check result. By default, this method returns the original check result.
75 *
76 * @param object The checked object.
77 * @param originalResult The original check result as returned by the specific condition implementation.
78 * @return The final check result.
79 */
80 protected boolean afterObjectChecked(Object object, boolean originalResult) {
81 return originalResult;
82 }
83 }