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