View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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: 1441676 $ $Date: 2013-02-02 02:15:15 +0100 (Sa, 02 Feb 2013) $
30   */
31  public class ValidatorResult implements Serializable {
32  
33      private static final long serialVersionUID = -3713364681647250531L;
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 an Iterator of the action names contained in this Result.
106      * @return The set of action names.
107      */
108     public Iterator getActions() {
109         return Collections.unmodifiableMap(hAction).keySet().iterator();
110     }
111 
112     /**
113      * Return a Map of the validator actions in this Result.
114      * @return Map of validator actions.
115      * @deprecated Use getActions() to return the set of actions
116      *             the isValid(name) and getResult(name) methods
117      *             to determine the contents of ResultStatus.
118      *
119      */
120     public Map getActionMap() {
121         return Collections.unmodifiableMap(hAction);
122     }
123 
124     /**
125      * Returns the Field that was validated.
126      * @return The Field associated with this result.
127      */
128     public Field getField() {
129         return this.field;
130     }
131 
132     /**
133      * Contains the status of the validation.
134      */
135     protected static class ResultStatus implements Serializable {
136 
137         private static final long serialVersionUID = 4076665918535320007L;
138 
139         private boolean valid = false;
140         private Object result = null;
141 
142        /**
143         * Construct a Result status.
144          * @param valid Whether the validator passed or failed.
145          * @param result Value returned by the validator.
146         */
147         public ResultStatus(boolean valid, Object result) {
148             this.valid = valid;
149             this.result = result;
150         }
151         /**
152          * Provided for backwards binary compatibility only
153          * @deprecated Use {@link #ResultStatus(boolean, Object)} instead
154          */
155         public ResultStatus(ValidatorResult ignored, boolean valid, Object result) {
156             this(valid, result);
157         }
158 
159         /**
160          * Tests whether or not the validation passed.
161          * @return true if the result was good.
162          */
163         public boolean isValid() {
164             return valid;
165         }
166 
167         /**
168          * Sets whether or not the validation passed.
169          * @param valid Whether the validation passed.
170          */
171         public void setValid(boolean valid) {
172             this.valid = valid;
173         }
174 
175         /**
176          * Gets the result returned by a validation method.
177          * This can be used to retrieve to the correctly
178          * typed value of a date validation for example.
179          * @return The value returned by the validation.
180          */
181         public Object getResult() {
182             return result;
183         }
184 
185         /**
186          * Sets the result returned by a validation method.
187          * This can be used to retrieve to the correctly
188          * typed value of a date validation for example.
189          * @param result The value returned by the validation.
190          */
191         public void setResult(Object result) {
192             this.result = result;
193         }
194 
195     }
196 
197 }