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    *      https://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.Iterator;
23  import java.util.Map;
24  
25  /**
26   * This contains the results of a set of validation rules processed
27   * on a JavaBean.
28   */
29  //TODO mutable non-private fields
30  public class ValidatorResult implements Serializable {
31  
32      /**
33       * Contains the status of the validation.
34       */
35      protected static class ResultStatus implements Serializable {
36  
37          private static final long serialVersionUID = 4076665918535320007L;
38  
39          /** Whether or not the validation passed. */
40          private boolean valid;
41  
42          /** Result returned by a validation method. */
43          private Object result;
44  
45         /**
46          * Constructs a Result status.
47          *
48          * @param valid Whether the validator passed or failed.
49          * @param result Value returned by the validator.
50          */
51          public ResultStatus(final boolean valid, final Object result) {
52              this.valid = valid;
53              this.result = result;
54          }
55  
56          /**
57           * Provided for backwards binary compatibility only.
58           *
59           * @param ignored ignored by this method
60           * @param valid Whether the validator passed or failed.
61           * @param result Value returned by the validator.
62           * @deprecated Use {@code ResultStatus(boolean, Object)} instead
63           */
64          @Deprecated
65          public ResultStatus(final ValidatorResult ignored, final boolean valid, final Object result) {
66              this(valid, result);
67          }
68  
69          /**
70           * Gets the result returned by a validation method.
71           * This can be used to retrieve to the correctly
72           * typed value of a date validation for example.
73           *
74           * @return The value returned by the validation.
75           */
76          public Object getResult() {
77              return result;
78          }
79  
80          /**
81           * Tests whether or not the validation passed.
82           *
83           * @return true if the result was good.
84           */
85          public boolean isValid() {
86              return valid;
87          }
88  
89          /**
90           * Sets the result returned by a validation method.
91           * This can be used to retrieve to the correctly
92           * typed value of a date validation for example.
93           *
94           * @param result The value returned by the validation.
95           */
96          public void setResult(final Object result) {
97              this.result = result;
98          }
99  
100         /**
101          * Sets whether or not the validation passed.
102          *
103          * @param valid Whether the validation passed.
104          */
105         public void setValid(final boolean valid) {
106             this.valid = valid;
107         }
108 
109     }
110 
111     private static final long serialVersionUID = -3713364681647250531L;
112 
113     /**
114      * Map of results.  The key is the name of the {@code ValidatorAction}
115      * and the value is whether or not this field passed or not.
116      */
117     protected Map<String, ResultStatus> hAction = new HashMap<>();
118 
119     /**
120      * {@code Field} being validated.
121      * TODO This variable is not used.  Need to investigate removing it.
122      */
123     protected Field field;
124 
125     /**
126      * Constructs a {@code ValidatorResult} with the associated field being
127      * validated.
128      *
129      * @param field Field that was validated.
130      */
131     public ValidatorResult(final Field field) {
132         this.field = field;
133     }
134 
135     /**
136      * Add the result of a validator action.
137      *
138      * @param validatorName Name of the validator.
139      * @param result Whether the validation passed or failed.
140      */
141     public void add(final String validatorName, final boolean result) {
142         this.add(validatorName, result, null);
143     }
144 
145     /**
146      * Add the result of a validator action.
147      *
148      * @param validatorName Name of the validator.
149      * @param result Whether the validation passed or failed.
150      * @param value Value returned by the validator.
151      */
152     public void add(final String validatorName, final boolean result, final Object value) {
153         hAction.put(validatorName, new ResultStatus(result, value));
154     }
155 
156     /**
157      * Indicate whether a specified validator is in the Result.
158      *
159      * @param validatorName Name of the validator.
160      * @return true if the validator is in the result.
161      */
162     public boolean containsAction(final String validatorName) {
163         return hAction.containsKey(validatorName);
164     }
165 
166     /**
167      * Gets a Map of the validator actions in this Result.
168      *
169      * @return Map of validator actions.
170      * @deprecated Use getActions() to return the set of actions
171      *             the isValid(name) and getResult(name) methods
172      *             to determine the contents of ResultStatus.
173      */
174     @Deprecated
175     public Map<String, ResultStatus> getActionMap() {
176         return Collections.unmodifiableMap(hAction);
177     }
178 
179     /**
180      * Gets an Iterator of the action names contained in this Result.
181      *
182      * @return The set of action names.
183      */
184     public Iterator<String> getActions() {
185         return Collections.unmodifiableMap(hAction).keySet().iterator();
186     }
187 
188     /**
189      * Returns the Field that was validated.
190      *
191      * @return The Field associated with this result.
192      */
193     public Field getField() {
194         return field;
195     }
196 
197     /**
198      * Gets the result of a validation.
199      *
200      * @param validatorName Name of the validator.
201      * @return The validation result.
202      */
203     public Object getResult(final String validatorName) {
204         final ResultStatus status = hAction.get(validatorName);
205         return status == null ? null : status.getResult();
206     }
207 
208     /**
209      * Indicate whether a specified validation passed.
210      *
211      * @param validatorName Name of the validator.
212      * @return true if the validation passed.
213      */
214     public boolean isValid(final String validatorName) {
215         final ResultStatus status = hAction.get(validatorName);
216         return status != null && status.isValid();
217     }
218 
219 }