KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > validation > FieldError


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.validation;
18
19 import org.springframework.util.Assert;
20
21 /**
22  * Encapsulates a field error, that is, a reason for rejecting a specific
23  * field value.
24  *
25  * <p>See the {@link DefaultMessageCodesResolver} javadoc for details on
26  * how a message code list is built for a <code>FieldError</code>.
27  *
28  * @author Rod Johnson
29  * @author Juergen Hoeller
30  * @since 10.03.2003
31  * @see DefaultMessageCodesResolver
32  */

33 public class FieldError extends ObjectError {
34
35     private final String JavaDoc field;
36
37     private final Object JavaDoc rejectedValue;
38
39     private final boolean bindingFailure;
40
41
42     /**
43      * Create a new FieldError instance.
44      * @param objectName the name of the affected object
45      * @param field the affected field of the object
46      * @param rejectedValue the rejected field value
47      * @param bindingFailure whether this error represents a binding failure
48      * (like a type mismatch); else, it is a validation failure
49      * @param codes the codes to be used to resolve this message
50      * @param arguments the array of arguments to be used to resolve this message
51      * @param defaultMessage the default message to be used to resolve this message
52      */

53     public FieldError(
54             String JavaDoc objectName, String JavaDoc field, Object JavaDoc rejectedValue, boolean bindingFailure,
55             String JavaDoc[] codes, Object JavaDoc[] arguments, String JavaDoc defaultMessage) {
56
57         super(objectName, codes, arguments, defaultMessage);
58         Assert.notNull(field, "Field must not be null");
59         this.field = field;
60         this.rejectedValue = rejectedValue;
61         this.bindingFailure = bindingFailure;
62     }
63
64
65     /**
66      * Return the affected field of the object.
67      */

68     public String JavaDoc getField() {
69         return field;
70     }
71
72     /**
73      * Return the rejected field value.
74      */

75     public Object JavaDoc getRejectedValue() {
76         return rejectedValue;
77     }
78
79     /**
80      * Return whether this error represents a binding failure
81      * (like a type mismatch); else, it is a validation failure.
82      */

83     public boolean isBindingFailure() {
84         return bindingFailure;
85     }
86
87
88     public String JavaDoc toString() {
89         return "Field error in object '" + getObjectName() + "' on field '" + this.field +
90                 "': rejected value [" + this.rejectedValue + "]; " + resolvableToString();
91     }
92
93     public boolean equals(Object JavaDoc other) {
94         if (this == other) {
95             return true;
96         }
97         if (!super.equals(other)) {
98             return false;
99         }
100         FieldError otherError = (FieldError) other;
101         return getField().equals(otherError.getField());
102     }
103
104     public int hashCode() {
105         return super.hashCode() * 29 + getField().hashCode();
106     }
107
108 }
109
Popular Tags