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