1 /*
2  * Copyright 2002-2008 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.context.support.DefaultMessageSourceResolvable;
20 import org.springframework.util.Assert;
21 
22 /**
23  * Encapsulates an object error, that is, a global reason for rejecting
24  * an object.
25  *
26  * <p>See the {@link DefaultMessageCodesResolver} javadoc for details on
27  * how a message code list is built for an <code>ObjectError</code>.
28  *
29  * @author Juergen Hoeller
30  * @see FieldError
31  * @see DefaultMessageCodesResolver
32  * @since 10.03.2003
33  */
34 @SuppressWarnings("serial")
35 public class ObjectError extends DefaultMessageSourceResolvable {
36 
37 	private final String objectName;
38 
39 
40 	/**
41 	 * Create a new instance of the ObjectError class.
42 	 * @param objectName the name of the affected object
43 	 * @param defaultMessage the default message to be used to resolve this message
44 	 */
ObjectError(String objectName, String defaultMessage)45 	public ObjectError(String objectName, String defaultMessage) {
46 		this(objectName, null, null, defaultMessage);
47 	}
48 
49 	/**
50 	 * Create a new instance of the ObjectError class.
51 	 * @param objectName the name of the affected object
52 	 * @param codes the codes to be used to resolve this message
53 	 * @param arguments	the array of arguments to be used to resolve this message
54 	 * @param defaultMessage the default message to be used to resolve this message
55 	 */
ObjectError(String objectName, String[] codes, Object[] arguments, String defaultMessage)56 	public ObjectError(String objectName, String[] codes, Object[] arguments, String defaultMessage) {
57 		super(codes, arguments, defaultMessage);
58 		Assert.notNull(objectName, "Object name must not be null");
59 		this.objectName = objectName;
60 	}
61 
62 
63 	/**
64 	 * Return the name of the affected object.
65 	 */
getObjectName()66 	public String getObjectName() {
67 		return this.objectName;
68 	}
69 
70 
71 	@Override
toString()72 	public String toString() {
73 		return "Error in object '" + this.objectName + "': " + resolvableToString();
74 	}
75 
76 	@Override
equals(Object other)77 	public boolean equals(Object other) {
78 		if (this == other) {
79 			return true;
80 		}
81 		if (!(getClass().equals(other.getClass())) || !super.equals(other)) {
82 			return false;
83 		}
84 		ObjectError otherError = (ObjectError) other;
85 		return getObjectName().equals(otherError.getObjectName());
86 	}
87 
88 	@Override
hashCode()89 	public int hashCode() {
90 		return super.hashCode() * 29 + getObjectName().hashCode();
91 	}
92 
93 }
94