KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > ValidatorResult


1 /*
2  * $Id: ValidatorResult.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package org.apache.commons.validator;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * This contains the results of a set of validation rules processed
31  * on a JavaBean.
32  */

33 public class ValidatorResult implements Serializable JavaDoc {
34
35     /**
36      * Map of results. The key is the name of the <code>ValidatorAction</code>
37      * and the value is whether or not this field passed or not.
38      */

39     protected Map JavaDoc hAction = new HashMap JavaDoc();
40
41     /**
42      * <code>Field</code> being validated.
43      * TODO This variable is not used. Need to investigate removing it.
44      */

45     protected Field field = null;
46
47     /**
48      * Constructs a <code>ValidatorResult</code> with the associated field being
49      * validated.
50      */

51     public ValidatorResult(Field field) {
52         this.field = field;
53     }
54
55     /**
56      * Add the result of a validator action.
57      */

58     public void add(String JavaDoc validatorName, boolean result) {
59         this.add(validatorName, result, null);
60     }
61
62     /**
63      * Add the result of a validator action.
64      */

65     public void add(String JavaDoc validatorName, boolean result, Object JavaDoc value) {
66         hAction.put(validatorName, new ResultStatus(result, value));
67     }
68
69     public boolean containsAction(String JavaDoc validatorName) {
70         return hAction.containsKey(validatorName);
71     }
72
73     public boolean isValid(String JavaDoc validatorName) {
74         ResultStatus status = (ResultStatus) hAction.get(validatorName);
75         return (status == null) ? false : status.isValid();
76     }
77
78     public Map JavaDoc getActionMap() {
79         return Collections.unmodifiableMap(hAction);
80     }
81
82     /**
83      * Returns the Field that was validated.
84      */

85     public Field getField() {
86         return this.field;
87     }
88
89     /**
90      * Contains the status of the validation.
91      */

92     protected class ResultStatus implements Serializable JavaDoc {
93         private boolean valid = false;
94         private Object JavaDoc result = null;
95
96         public ResultStatus(boolean valid, Object JavaDoc result) {
97             this.valid = valid;
98             this.result = result;
99         }
100
101         /**
102          * Tests whether or not the validation passed.
103          */

104         public boolean isValid() {
105             return valid;
106         }
107
108         /**
109          * Sets whether or not the validation passed.
110          */

111         public void setValid(boolean valid) {
112             this.valid = valid;
113         }
114
115         /**
116          * Gets the result returned by a validation method.
117          * This can be used to retrieve to the correctly
118          * typed value of a date validation for example.
119          */

120         public Object JavaDoc getResult() {
121             return result;
122         }
123
124         /**
125          * Sets the result returned by a validation method.
126          * This can be used to retrieve to the correctly
127          * typed value of a date validation for example.
128          */

129         public void setResult(Object JavaDoc result) {
130             this.result = result;
131         }
132
133     }
134
135 }
Popular Tags