    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;
    024
    025/**
    026 * This contains the results of a set of validation rules processed
    027 * on a JavaBean.
    028 */
    029//TODO mutable non-private fields
    030public class ValidatorResult implements Serializable {
    031
    032    /**
    033     * Contains the status of the validation.
    034     */
    035    protected static class ResultStatus implements Serializable {
    036
    037        private static final long serialVersionUID = 4076665918535320007L;
    038
    039        /** Whether or not the validation passed. */
    040        private boolean valid;
    041
    042        /** Result returned by a validation method. */
    043        private Object result;
    044
    045       /**
    046        * Constructs a Result status.
    047        * @param valid Whether the validator passed or failed.
    048        * @param result Value returned by the validator.
    049        */
    050        public ResultStatus(final boolean valid, final Object result) {
    051            this.valid = valid;
    052            this.result = result;
    053        }
    054        /**
    055         * Provided for backwards binary compatibility only.
    056         *
    057         * @param ignored ignored by this method
    058         * @param valid Whether the validator passed or failed.
    059         * @param result Value returned by the validator.
    060         * @deprecated Use {@code ResultStatus(boolean, Object)} instead
    061         */
    062        @Deprecated
    063        public ResultStatus(final ValidatorResult ignored, final boolean valid, final Object result) {
    064            this(valid, result);
    065        }
    066
    067        /**
    068         * Gets the result returned by a validation method.
    069         * This can be used to retrieve to the correctly
    070         * typed value of a date validation for example.
    071         * @return The value returned by the validation.
    072         */
    073        public Object getResult() {
    074            return result;
    075        }
    076
    077        /**
    078         * Tests whether or not the validation passed.
    079         * @return true if the result was good.
    080         */
    081        public boolean isValid() {
    082            return valid;
    083        }
    084
    085        /**
    086         * Sets the result returned by a validation method.
    087         * This can be used to retrieve to the correctly
    088         * typed value of a date validation for example.
    089         * @param result The value returned by the validation.
    090         */
    091        public void setResult(final Object result) {
    092            this.result = result;
    093        }
    094
    095        /**
    096         * Sets whether or not the validation passed.
    097         * @param valid Whether the validation passed.
    098         */
    099        public void setValid(final boolean valid) {
    100            this.valid = valid;
    101        }
    102
    103    }
    104
    105    private static final long serialVersionUID = -3713364681647250531L;
    106
    107    /**
    108     * Map of results.  The key is the name of the {@code ValidatorAction}
    109     * and the value is whether or not this field passed or not.
    110     */
    111    protected Map<String, ResultStatus> hAction = new HashMap<>();
    112
    113    /**
    114     * {@code Field} being validated.
    115     * TODO This variable is not used.  Need to investigate removing it.
    116     */
    117    protected Field field;
    118
    119    /**
    120     * Constructs a {@code ValidatorResult} with the associated field being
    121     * validated.
    122     * @param field Field that was validated.
    123     */
    124    public ValidatorResult(final Field field) {
    125        this.field = field;
    126    }
    127
    128    /**
    129     * Add the result of a validator action.
    130     * @param validatorName Name of the validator.
    131     * @param result Whether the validation passed or failed.
    132     */
    133    public void add(final String validatorName, final boolean result) {
    134        this.add(validatorName, result, null);
    135    }
    136
    137    /**
    138     * Add the result of a validator action.
    139     * @param validatorName Name of the validator.
    140     * @param result Whether the validation passed or failed.
    141     * @param value Value returned by the validator.
    142     */
    143    public void add(final String validatorName, final boolean result, final Object value) {
    144        hAction.put(validatorName, new ResultStatus(result, value));
    145    }
    146
    147    /**
    148     * Indicate whether a specified validator is in the Result.
    149     * @param validatorName Name of the validator.
    150     * @return true if the validator is in the result.
    151     */
    152    public boolean containsAction(final String validatorName) {
    153        return hAction.containsKey(validatorName);
    154    }
    155
    156    /**
    157     * Gets a Map of the validator actions in this Result.
    158     * @return Map of validator actions.
    159     * @deprecated Use getActions() to return the set of actions
    160     *             the isValid(name) and getResult(name) methods
    161     *             to determine the contents of ResultStatus.
    162     */
    163    @Deprecated
    164    public Map<String, ResultStatus> getActionMap() {
    165        return Collections.unmodifiableMap(hAction);
    166    }
    167
    168    /**
    169     * Gets an Iterator of the action names contained in this Result.
    170     * @return The set of action names.
    171     */
    172    public Iterator<String> getActions() {
    173        return Collections.unmodifiableMap(hAction).keySet().iterator();
    174    }
    175
    176    /**
    177     * Returns the Field that was validated.
    178     * @return The Field associated with this result.
    179     */
    180    public Field getField() {
    181        return field;
    182    }
    183
    184    /**
    185     * Gets the result of a validation.
    186     * @param validatorName Name of the validator.
    187     * @return The validation result.
    188     */
    189    public Object getResult(final String validatorName) {
    190        final ResultStatus status = hAction.get(validatorName);
    191        return status == null ? null : status.getResult();
    192    }
    193
    194    /**
    195     * Indicate whether a specified validation passed.
    196     * @param validatorName Name of the validator.
    197     * @return true if the validation passed.
    198     */
    199    public boolean isValid(final String validatorName) {
    200        final ResultStatus status = hAction.get(validatorName);
    201        return status != null && status.isValid();
    202    }
    203
    204}