    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 *      https://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     * Constructs a new instance.
    042     */
    043    public ValidatorResults() {
    044        // empty
    045    }
    046
    047    /**
    048     * Add the result of a validator action.
    049     *
    050     * @param field The field validated.
    051     * @param validatorName The name of the validator.
    052     * @param result The result of the validation.
    053     */
    054    public void add(final Field field, final String validatorName, final boolean result) {
    055        this.add(field, validatorName, result, null);
    056    }
    057
    058    /**
    059     * Add the result of a validator action.
    060     *
    061     * @param field The field validated.
    062     * @param validatorName The name of the validator.
    063     * @param result The result of the validation.
    064     * @param value The value returned by the validator.
    065     */
    066    public void add(
    067            final Field field,
    068            final String validatorName,
    069            final boolean result,
    070            final Object value) {
    071
    072        ValidatorResult validatorResult = getValidatorResult(field.getKey());
    073
    074        if (validatorResult == null) {
    075            validatorResult = new ValidatorResult(field);
    076            hResults.put(field.getKey(), validatorResult);
    077        }
    078
    079        validatorResult.add(validatorName, result, value);
    080    }
    081
    082    /**
    083     * Clear all results recorded by this object.
    084     */
    085    public void clear() {
    086        hResults.clear();
    087    }
    088
    089    /**
    090     * Gets the set of property names for which at least one message has
    091     * been recorded.
    092     * @return An unmodifiable Set of the property names.
    093     */
    094    public Set<String> getPropertyNames() {
    095        return Collections.unmodifiableSet(hResults.keySet());
    096    }
    097
    098    /**
    099     * 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}