Source code
Description: source: package: org.apache.commons.validator, class: ValidatorResults

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