001/*
002 * Copyright 2002-2018 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 *      https://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
017package org.springframework.validation;
018
019import org.springframework.context.support.DefaultMessageSourceResolvable;
020import org.springframework.util.Assert;
021
022/**
023 * Encapsulates an object error, that is, a global reason for rejecting
024 * an object.
025 *
026 * <p>See the {@link DefaultMessageCodesResolver} javadoc for details on
027 * how a message code list is built for an {@code ObjectError}.
028 *
029 * @author Juergen Hoeller
030 * @since 10.03.2003
031 * @see FieldError
032 * @see DefaultMessageCodesResolver
033 */
034@SuppressWarnings("serial")
035public class ObjectError extends DefaultMessageSourceResolvable {
036
037        private final String objectName;
038
039
040        /**
041         * Create a new instance of the ObjectError class.
042         * @param objectName the name of the affected object
043         * @param defaultMessage the default message to be used to resolve this message
044         */
045        public ObjectError(String objectName, String defaultMessage) {
046                this(objectName, null, null, defaultMessage);
047        }
048
049        /**
050         * Create a new instance of the ObjectError class.
051         * @param objectName the name of the affected object
052         * @param codes the codes to be used to resolve this message
053         * @param arguments     the array of arguments to be used to resolve this message
054         * @param defaultMessage the default message to be used to resolve this message
055         */
056        public ObjectError(String objectName, String[] codes, Object[] arguments, String defaultMessage) {
057                super(codes, arguments, defaultMessage);
058                Assert.notNull(objectName, "Object name must not be null");
059                this.objectName = objectName;
060        }
061
062
063        /**
064         * Return the name of the affected object.
065         */
066        public String getObjectName() {
067                return this.objectName;
068        }
069
070
071        @Override
072        public boolean equals(Object other) {
073                if (this == other) {
074                        return true;
075                }
076                if (other == null || other.getClass() != getClass() || !super.equals(other)) {
077                        return false;
078                }
079                ObjectError otherError = (ObjectError) other;
080                return getObjectName().equals(otherError.getObjectName());
081        }
082
083        @Override
084        public int hashCode() {
085                return super.hashCode() * 29 + getObjectName().hashCode();
086        }
087
088        @Override
089        public String toString() {
090                return "Error in object '" + this.objectName + "': " + resolvableToString();
091        }
092
093}