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: 1739361 $
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 = 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 = 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    @Deprecated
122    public Map<String, ResultStatus> getActionMap() {
123        return Collections.unmodifiableMap(hAction);
124    }
125
126    /**
127     * Returns the Field that was validated.
128     * @return The Field associated with this result.
129     */
130    public Field getField() {
131        return this.field;
132    }
133
134    /**
135     * Contains the status of the validation.
136     */
137    protected static class ResultStatus implements Serializable {
138
139        private static final long serialVersionUID = 4076665918535320007L;
140
141        private boolean valid = false;
142        private Object result = null;
143
144       /**
145        * Construct a Result status.
146        * @param valid Whether the validator passed or failed.
147        * @param result Value returned by the validator.
148        */
149        public ResultStatus(boolean valid, Object result) {
150            this.valid = valid;
151            this.result = result;
152        }
153        /**
154         * Provided for backwards binary compatibility only.
155         *
156         * @param ignored ignored by this method
157         * @param valid Whether the validator passed or failed.
158         * @param result Value returned by the validator.
159         *
160         * @deprecated Use {@code ResultStatus(boolean, Object)} instead
161         */
162        @Deprecated
163        public ResultStatus(ValidatorResult ignored, boolean valid, Object result) {
164            this(valid, result);
165        }
166
167        /**
168         * Tests whether or not the validation passed.
169         * @return true if the result was good.
170         */
171        public boolean isValid() {
172            return valid;
173        }
174
175        /**
176         * Sets whether or not the validation passed.
177         * @param valid Whether the validation passed.
178         */
179        public void setValid(boolean valid) {
180            this.valid = valid;
181        }
182
183        /**
184         * Gets the result returned by a validation method.
185         * This can be used to retrieve to the correctly
186         * typed value of a date validation for example.
187         * @return The value returned by the validation.
188         */
189        public Object getResult() {
190            return result;
191        }
192
193        /**
194         * Sets the result returned by a validation method.
195         * This can be used to retrieve to the correctly
196         * typed value of a date validation for example.
197         * @param result The value returned by the validation.
198         */
199        public void setResult(Object result) {
200            this.result = result;
201        }
202
203    }
204
205}