1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator;
23
24 import java.io.Serializable;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 /***
30 * This contains the results of a set of validation rules processed
31 * on a JavaBean.
32 */
33 public class ValidatorResult implements Serializable {
34
35 /***
36 * Map of results. The key is the name of the <code>ValidatorAction</code>
37 * and the value is whether or not this field passed or not.
38 */
39 protected Map hAction = new HashMap();
40
41 /***
42 * <code>Field</code> being validated.
43 * TODO This variable is not used. Need to investigate removing it.
44 */
45 protected Field field = null;
46
47 /***
48 * Constructs a <code>ValidatorResult</code> with the associated field being
49 * validated.
50 * @param field Field that was validated.
51 */
52 public ValidatorResult(Field field) {
53 this.field = field;
54 }
55
56 /***
57 * Add the result of a validator action.
58 * @param validatorName Name of the validator.
59 * @param result Whether the validation passed or failed.
60 */
61 public void add(String validatorName, boolean result) {
62 this.add(validatorName, result, null);
63 }
64
65 /***
66 * Add the result of a validator action.
67 * @param validatorName Name of the validator.
68 * @param result Whether the validation passed or failed.
69 * @param value Value returned by the validator.
70 */
71 public void add(String validatorName, boolean result, Object value) {
72 hAction.put(validatorName, new ResultStatus(result, value));
73 }
74
75 /***
76 * Indicate whether a specified validator is in the Result.
77 * @param validatorName Name of the validator.
78 * @return true if the validator is in the result.
79 */
80 public boolean containsAction(String validatorName) {
81 return hAction.containsKey(validatorName);
82 }
83
84 /***
85 * Indicate whether a specified validation passed.
86 * @param validatorName Name of the validator.
87 * @return true if the validation passed.
88 */
89 public boolean isValid(String validatorName) {
90 ResultStatus status = (ResultStatus) hAction.get(validatorName);
91 return (status == null) ? false : status.isValid();
92 }
93
94 /***
95 * Return the result of a validation.
96 * @param validatorName Name of the validator.
97 * @return The validation result.
98 */
99 public Object getResult(String validatorName) {
100 ResultStatus status = (ResultStatus) hAction.get(validatorName);
101 return (status == null) ? null : status.getResult();
102 }
103
104 /***
105 * Return a Map of the validator actions in this Result.
106 * @return Map of validator actions.
107 */
108 public Map getActionMap() {
109 return Collections.unmodifiableMap(hAction);
110 }
111
112 /***
113 * Returns the Field that was validated.
114 * @return The Field associated with this result.
115 */
116 public Field getField() {
117 return this.field;
118 }
119
120 /***
121 * Contains the status of the validation.
122 */
123 protected class ResultStatus implements Serializable {
124 private boolean valid = false;
125 private Object result = null;
126
127 /***
128 * Construct a Result status.
129 * @param valid Whether the validator passed or failed.
130 * @param result Value returned by the validator.
131 */
132 public ResultStatus(boolean valid, Object result) {
133 this.valid = valid;
134 this.result = result;
135 }
136
137 /***
138 * Tests whether or not the validation passed.
139 * @return true if the result was good.
140 */
141 public boolean isValid() {
142 return valid;
143 }
144
145 /***
146 * Sets whether or not the validation passed.
147 * @param valid Whether the validation passed.
148 */
149 public void setValid(boolean valid) {
150 this.valid = valid;
151 }
152
153 /***
154 * Gets the result returned by a validation method.
155 * This can be used to retrieve to the correctly
156 * typed value of a date validation for example.
157 * @return The value returned by the validation.
158 */
159 public Object getResult() {
160 return result;
161 }
162
163 /***
164 * Sets the result returned by a validation method.
165 * This can be used to retrieve to the correctly
166 * typed value of a date validation for example.
167 * @param result The value returned by the validation.
168 */
169 public void setResult(Object result) {
170 this.result = result;
171 }
172
173 }
174
175 }