001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.validation;
018:
019: import org.springframework.util.Assert;
020:
021: /**
022: * Encapsulates a field error, that is, a reason for rejecting a specific
023: * field value.
024: *
025: * <p>See the {@link DefaultMessageCodesResolver} javadoc for details on
026: * how a message code list is built for a <code>FieldError</code>.
027: *
028: * @author Rod Johnson
029: * @author Juergen Hoeller
030: * @since 10.03.2003
031: * @see DefaultMessageCodesResolver
032: */
033: public class FieldError extends ObjectError {
034:
035: private final String field;
036:
037: private final Object rejectedValue;
038:
039: private final boolean bindingFailure;
040:
041: /**
042: * Create a new FieldError instance.
043: * @param objectName the name of the affected object
044: * @param field the affected field of the object
045: * @param rejectedValue the rejected field value
046: * @param bindingFailure whether this error represents a binding failure
047: * (like a type mismatch); else, it is a validation failure
048: * @param codes the codes to be used to resolve this message
049: * @param arguments the array of arguments to be used to resolve this message
050: * @param defaultMessage the default message to be used to resolve this message
051: */
052: public FieldError(String objectName, String field,
053: Object rejectedValue, boolean bindingFailure,
054: String[] codes, Object[] arguments, String defaultMessage) {
055:
056: super (objectName, codes, arguments, defaultMessage);
057: Assert.notNull(field, "Field must not be null");
058: this .field = field;
059: this .rejectedValue = rejectedValue;
060: this .bindingFailure = bindingFailure;
061: }
062:
063: /**
064: * Return the affected field of the object.
065: */
066: public String getField() {
067: return field;
068: }
069:
070: /**
071: * Return the rejected field value.
072: */
073: public Object getRejectedValue() {
074: return rejectedValue;
075: }
076:
077: /**
078: * Return whether this error represents a binding failure
079: * (like a type mismatch); else, it is a validation failure.
080: */
081: public boolean isBindingFailure() {
082: return bindingFailure;
083: }
084:
085: public String toString() {
086: return "Field error in object '" + getObjectName()
087: + "' on field '" + this .field + "': rejected value ["
088: + this .rejectedValue + "]; " + resolvableToString();
089: }
090:
091: public boolean equals(Object other) {
092: if (this == other) {
093: return true;
094: }
095: if (!super .equals(other)) {
096: return false;
097: }
098: FieldError otherError = (FieldError) other;
099: return getField().equals(otherError.getField());
100: }
101:
102: public int hashCode() {
103: return super .hashCode() * 29 + getField().hashCode();
104: }
105:
106: }
|