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 * @return An unmodifiable Set of the property names.
93 */
94 public Set<String> getPropertyNames() {
95 return Collections.unmodifiableSet(hResults.keySet());
96 }
97
98 /**
99 * Gets a {@link Map} of any {@code Object}s returned from
100 * validation routines.
101 *
102 * @return Map of objections returned by validators.
103 */
104 public Map<String, Object> getResultValueMap() {
105 final Map<String, Object> results = new HashMap<>();
106
107 for (final String propertyKey : hResults.keySet()) {
108 final ValidatorResult vr = getValidatorResult(propertyKey);
109
110 for (final Iterator<String> x = vr.getActions(); x.hasNext();) {
111 final String actionKey = x.next();
112 final Object result = vr.getResult(actionKey);
113
114 if (result != null && !(result instanceof Boolean)) {
115 results.put(propertyKey, result);
116 }
117 }
118 }
119
120 return results;
121 }
122
123 /**
124 * Gets the {@code ValidatorResult} associated
125 * with the key passed in. The key the {@code ValidatorResult}
126 * is stored under is the {@code Field}'s getKey method.
127 *
128 * @param key The key generated from {@code Field} (this is often just
129 * the field name).
130 *
131 * @return The result of a specified key.
132 */
133 public ValidatorResult getValidatorResult(final String key) {
134 return hResults.get(key);
135 }
136
137 /**
138 * Gets {@code true} if there are no messages recorded
139 * in this collection, or {@code false} otherwise.
140 *
141 * @return Whether these results are empty.
142 */
143 public boolean isEmpty() {
144 return hResults.isEmpty();
145 }
146
147 /**
148 * Merge another ValidatorResults into mine.
149 *
150 * @param results ValidatorResults to merge.
151 */
152 public void merge(final ValidatorResults results) {
153 hResults.putAll(results.hResults);
154 }
155
156 }