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