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  import java.util.Set;
25  
26  /**
27   * This contains the results of a set of validation rules processed
28   * on a JavaBean.
29   */
30  //TODO mutable non-private fields
31  public class ValidatorResults implements Serializable {
32  
33      private static final long serialVersionUID = -2709911078904924839L;
34  
35      /**
36       * Map of validation results.
37       */
38      protected Map<String, ValidatorResult> hResults = new HashMap<>();
39  
40      /**
41       * Add a the result of a validator action.
42       *
43       * @param field The field validated.
44       * @param validatorName The name of the validator.
45       * @param result The result of the validation.
46       */
47      public void add(final Field field, final String validatorName, final boolean result) {
48          this.add(field, validatorName, result, null);
49      }
50  
51      /**
52       * Add a the result of a validator action.
53       *
54       * @param field The field validated.
55       * @param validatorName The name of the validator.
56       * @param result The result of the validation.
57       * @param value The value returned by the validator.
58       */
59      public void add(
60              final Field field,
61              final String validatorName,
62              final boolean result,
63              final Object value) {
64  
65          ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
66  
67          if (validatorResult == null) {
68              validatorResult = new ValidatorResult(field);
69              this.hResults.put(field.getKey(), validatorResult);
70          }
71  
72          validatorResult.add(validatorName, result, value);
73      }
74  
75      /**
76       * Clear all results recorded by this object.
77       */
78      public void clear() {
79          this.hResults.clear();
80      }
81  
82      /**
83       * Return the set of property names for which at least one message has
84       * been recorded.
85       * @return An unmodifiable Set of the property names.
86       */
87      public Set<String> getPropertyNames() {
88          return Collections.unmodifiableSet(this.hResults.keySet());
89      }
90  
91      /**
92       * Gets a <code>Map</code> of any <code>Object</code>s returned from
93       * validation routines.
94       *
95       * @return Map of objections returned by validators.
96       */
97      public Map<String, Object> getResultValueMap() {
98          final Map<String, Object> results = new HashMap<>();
99  
100         for (final String propertyKey : hResults.keySet()) {
101             final ValidatorResult vr = this.getValidatorResult(propertyKey);
102 
103             for (final Iterator<String> x = vr.getActions(); x.hasNext();) {
104                 final String actionKey = x.next();
105                 final Object result = vr.getResult(actionKey);
106 
107                 if (result != null && !(result instanceof Boolean)) {
108                     results.put(propertyKey, result);
109                 }
110             }
111         }
112 
113         return results;
114     }
115 
116     /**
117      * Gets the <code>ValidatorResult</code> associated
118      * with the key passed in.  The key the <code>ValidatorResult</code>
119      * is stored under is the <code>Field</code>'s getKey method.
120      *
121      * @param key The key generated from <code>Field</code> (this is often just
122      * the field name).
123      *
124      * @return The result of a specified key.
125      */
126     public ValidatorResult getValidatorResult(final String key) {
127         return this.hResults.get(key);
128     }
129 
130     /**
131      * Return {@code true} if there are no messages recorded
132      * in this collection, or {@code false} otherwise.
133      *
134      * @return Whether these results are empty.
135      */
136     public boolean isEmpty() {
137         return this.hResults.isEmpty();
138     }
139 
140     /**
141      * Merge another ValidatorResults into mine.
142      *
143      * @param results ValidatorResults to merge.
144      */
145     public void merge(final ValidatorResults results) {
146         this.hResults.putAll(results.hResults);
147     }
148 
149 }