001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator;
018
019import java.io.Serializable;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.Map;
024import java.util.Set;
025
026/**
027 * This contains the results of a set of validation rules processed
028 * on a JavaBean.
029 */
030//TODO mutable non-private fields
031public class ValidatorResults implements Serializable {
032
033    private static final long serialVersionUID = -2709911078904924839L;
034
035    /**
036     * Map of validation results.
037     */
038    protected Map<String, ValidatorResult> hResults = new HashMap<>();
039
040    /**
041     * Add a the result of a validator action.
042     *
043     * @param field The field validated.
044     * @param validatorName The name of the validator.
045     * @param result The result of the validation.
046     */
047    public void add(final Field field, final String validatorName, final boolean result) {
048        this.add(field, validatorName, result, null);
049    }
050
051    /**
052     * Add a the result of a validator action.
053     *
054     * @param field The field validated.
055     * @param validatorName The name of the validator.
056     * @param result The result of the validation.
057     * @param value The value returned by the validator.
058     */
059    public void add(
060            final Field field,
061            final String validatorName,
062            final boolean result,
063            final Object value) {
064
065        ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
066
067        if (validatorResult == null) {
068            validatorResult = new ValidatorResult(field);
069            this.hResults.put(field.getKey(), validatorResult);
070        }
071
072        validatorResult.add(validatorName, result, value);
073    }
074
075    /**
076     * Clear all results recorded by this object.
077     */
078    public void clear() {
079        this.hResults.clear();
080    }
081
082    /**
083     * Gets the set of property names for which at least one message has
084     * been recorded.
085     * @return An unmodifiable Set of the property names.
086     */
087    public Set<String> getPropertyNames() {
088        return Collections.unmodifiableSet(this.hResults.keySet());
089    }
090
091    /**
092     * Gets a <code>Map</code> of any <code>Object</code>s returned from
093     * validation routines.
094     *
095     * @return Map of objections returned by validators.
096     */
097    public Map<String, Object> getResultValueMap() {
098        final Map<String, Object> results = new HashMap<>();
099
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     * Gets {@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}