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: 1652498 $
30   */
31  //TODO mutable non-private fields
32  public class ValidatorResult implements Serializable {
33  
34      private static final long serialVersionUID = -3713364681647250531L;
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<String, ResultStatus> hAction = new HashMap<String, ResultStatus>();
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<String> 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<String, ResultStatus> 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 static class ResultStatus implements Serializable {
137 
138         private static final long serialVersionUID = 4076665918535320007L;
139 
140         private boolean valid = false;
141         private Object result = null;
142 
143        /**
144         * Construct a Result status.
145         * @param valid Whether the validator passed or failed.
146         * @param result Value returned by the validator.
147         */
148         public ResultStatus(boolean valid, Object result) {
149             this.valid = valid;
150             this.result = result;
151         }
152         /**
153          * Provided for backwards binary compatibility only.
154          *
155          * @param ignored ignored by this method
156          * @param valid Whether the validator passed or failed.
157          * @param result Value returned by the validator.
158          *
159          * @deprecated Use {@code ResultStatus(boolean, Object)} instead
160          */
161         public ResultStatus(ValidatorResult ignored, boolean valid, Object result) {
162             this(valid, result);
163         }
164 
165         /**
166          * Tests whether or not the validation passed.
167          * @return true if the result was good.
168          */
169         public boolean isValid() {
170             return valid;
171         }
172 
173         /**
174          * Sets whether or not the validation passed.
175          * @param valid Whether the validation passed.
176          */
177         public void setValid(boolean valid) {
178             this.valid = valid;
179         }
180 
181         /**
182          * Gets the result returned by a validation method.
183          * This can be used to retrieve to the correctly
184          * typed value of a date validation for example.
185          * @return The value returned by the validation.
186          */
187         public Object getResult() {
188             return result;
189         }
190 
191         /**
192          * Sets the result returned by a validation method.
193          * This can be used to retrieve to the correctly
194          * typed value of a date validation for example.
195          * @param result The value returned by the validation.
196          */
197         public void setResult(Object result) {
198             this.result = result;
199         }
200 
201     }
202 
203 }