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