View Javadoc

1   /*
2    * $Id: ValidatorResult.java 367437 2006-01-10 00:39:58Z niallp $
3    * $Rev: 367437 $
4    * $Date: 2006-01-10 00:39:58 +0000 (Tue, 10 Jan 2006) $
5    *
6    * ====================================================================
7    * Copyright 2001-2006 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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  import java.util.Iterator;
29  
30  /***
31   * This contains the results of a set of validation rules processed 
32   * on a JavaBean.
33   */
34  public class ValidatorResult implements Serializable {
35  
36      /***
37       * Map of results.  The key is the name of the <code>ValidatorAction</code>
38       * and the value is whether or not this field passed or not.
39       */
40      protected Map hAction = new HashMap();
41  
42      /***
43       * <code>Field</code> being validated.
44       * TODO This variable is not used.  Need to investigate removing it.
45       */
46      protected Field field = null;
47  
48      /***
49       * Constructs a <code>ValidatorResult</code> with the associated field being
50       * validated.
51       * @param field Field that was validated.
52       */
53      public ValidatorResult(Field field) {
54          this.field = field;
55      }
56  
57      /***
58       * Add the result of a validator action.
59       * @param validatorName Name of the validator.
60       * @param result Whether the validation passed or failed.
61       */
62      public void add(String validatorName, boolean result) {
63          this.add(validatorName, result, null);
64      }
65  
66      /***
67       * Add the result of a validator action.
68       * @param validatorName Name of the validator.
69       * @param result Whether the validation passed or failed.
70       * @param value Value returned by the validator.
71       */
72      public void add(String validatorName, boolean result, Object value) {
73          hAction.put(validatorName, new ResultStatus(result, value));
74      }
75  
76      /***
77       * Indicate whether a specified validator is in the Result.
78       * @param validatorName Name of the validator.
79       * @return true if the validator is in the result.
80       */
81      public boolean containsAction(String validatorName) {
82          return hAction.containsKey(validatorName);
83      }
84  
85      /***
86       * Indicate whether a specified validation passed.
87       * @param validatorName Name of the validator.
88       * @return true if the validation passed.
89       */
90      public boolean isValid(String validatorName) {
91          ResultStatus status = (ResultStatus) hAction.get(validatorName);
92          return (status == null) ? false : status.isValid();
93      }
94  
95      /***
96       * Return the result of a validation.
97       * @param validatorName Name of the validator.
98       * @return The validation result.
99       */
100     public Object getResult(String validatorName) {
101         ResultStatus status = (ResultStatus) hAction.get(validatorName);
102         return (status == null) ? null : status.getResult();
103     }
104 
105     /***
106      * Return an Iterator of the action names contained in this Result.
107      * @return The set of action names.
108      */
109     public Iterator getActions() {
110         return Collections.unmodifiableMap(hAction).keySet().iterator();
111     }
112 
113     /***
114      * Return a Map of the validator actions in this Result.
115      * @return Map of validator actions.
116      * @deprecated Use getActions() to return the set of actions
117      *             the isValid(name) and getResult(name) methods
118      *             to determine the contents of ResultStatus.
119      *
120      */
121     public Map getActionMap() {
122         return Collections.unmodifiableMap(hAction);
123     }
124 
125     /***
126      * Returns the Field that was validated.
127      * @return The Field associated with this result.
128      */
129     public Field getField() {
130         return this.field;
131     }
132 
133     /***
134      * Contains the status of the validation.
135      */
136     protected class ResultStatus implements Serializable {
137         private boolean valid = false;
138         private Object result = null;
139 
140        /***
141         * Construct a Result status.
142          * @param valid Whether the validator passed or failed.
143          * @param result Value returned by the validator.
144         */
145         public ResultStatus(boolean valid, Object result) {
146             this.valid = valid;
147             this.result = result;
148         }
149 
150         /***
151          * Tests whether or not the validation passed.
152          * @return true if the result was good.
153          */
154         public boolean isValid() {
155             return valid;
156         }
157 
158         /***
159          * Sets whether or not the validation passed.
160          * @param valid Whether the validation passed.
161          */
162         public void setValid(boolean valid) {
163             this.valid = valid;
164         }
165 
166         /***
167          * Gets the result returned by a validation method.
168          * This can be used to retrieve to the correctly
169          * typed value of a date validation for example.
170          * @return The value returned by the validation.
171          */
172         public Object getResult() {
173             return result;
174         }
175 
176         /***
177          * Sets the result returned by a validation method.
178          * This can be used to retrieve to the correctly
179          * typed value of a date validation for example.
180          * @param result The value returned by the validation.
181          */
182         public void setResult(Object result) {
183             this.result = result;
184         }
185 
186     }
187 
188 }