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 * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
031 */
032public class ValidatorResults implements Serializable {
033
034    private static final long serialVersionUID = -2709911078904924839L;
035
036    /**
037     * Map of validation results.
038     */
039    protected Map hResults = new HashMap();
040
041    /**
042     * Merge another ValidatorResults into mine.
043     *
044     * @param results ValidatorResults to merge.
045     */
046    public void merge(ValidatorResults results) {
047        this.hResults.putAll(results.hResults);
048    }
049
050    /**
051     * Add a the result of a validator action.
052     *
053     * @param field The field validated.
054     * @param validatorName The name of the validator.
055     * @param result The result of the validation.
056     */
057    public void add(Field field, String validatorName, boolean result) {
058        this.add(field, validatorName, result, null);
059    }
060
061    /**
062     * Add a the result of a validator action.
063     *
064     * @param field The field validated.
065     * @param validatorName The name of the validator.
066     * @param result The result of the validation.
067     * @param value The value returned by the validator.
068     */
069    public void add(
070            Field field,
071            String validatorName,
072            boolean result,
073            Object value) {
074
075        ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
076
077        if (validatorResult == null) {
078            validatorResult = new ValidatorResult(field);
079            this.hResults.put(field.getKey(), validatorResult);
080        }
081
082        validatorResult.add(validatorName, result, value);
083    }
084
085    /**
086     * Clear all results recorded by this object.
087     */
088    public void clear() {
089        this.hResults.clear();
090    }
091
092    /**
093     * Return <code>true</code> if there are no messages recorded
094     * in this collection, or <code>false</code> otherwise.
095     *
096     * @return Whether these results are empty.
097     */
098    public boolean isEmpty() {
099        return this.hResults.isEmpty();
100    }
101
102    /**
103     * Gets the <code>ValidatorResult</code> associated
104     * with the key passed in.  The key the <code>ValidatorResult</code>
105     * is stored under is the <code>Field</code>'s getKey method.
106     *
107     * @param key The key generated from <code>Field</code> (this is often just
108     * the field name).
109     *
110     * @return The result of a specified key.
111     */
112    public ValidatorResult getValidatorResult(String key) {
113        return (ValidatorResult) this.hResults.get(key);
114    }
115
116    /**
117     * Return the set of property names for which at least one message has
118     * been recorded.
119     * @return An unmodifiable Set of the property names.
120     */
121    public Set getPropertyNames() {
122        return Collections.unmodifiableSet(this.hResults.keySet());
123    }
124
125    /**
126     * Get a <code>Map</code> of any <code>Object</code>s returned from
127     * validation routines.
128     *
129     * @return Map of objections returned by validators.
130     */
131    public Map getResultValueMap() {
132        Map results = new HashMap();
133
134        for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
135            String propertyKey = (String) i.next();
136            ValidatorResult vr = this.getValidatorResult(propertyKey);
137
138            for (Iterator x = vr.getActions(); x.hasNext();) {
139                String actionKey = (String)x.next();
140                Object result = vr.getResult(actionKey);
141
142                if (result != null && !(result instanceof Boolean)) {
143                    results.put(propertyKey, result);
144                }
145            }
146        }
147
148        return results;
149    }
150
151}