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