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