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