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