001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.krad.util;
017
018
019import org.apache.commons.lang.ArrayUtils;
020import org.apache.commons.lang.StringUtils;
021
022import java.io.Serializable;
023import java.util.Arrays;
024
025
026/**
027 * Contains the error message key and parameters for a specific instantiation of an error message.
028 * 
029 * 
030 */
031public class ErrorMessage implements Serializable {
032    private String errorKey;
033    private String[] messageParameters;
034
035    /**
036     * Default constructor, required by AutoPopulatingList
037     */
038    public ErrorMessage() {
039    }
040
041    /**
042     * Convenience constructor which sets both fields
043     * 
044     * @param errorKey
045     * @param messageParameters
046     */
047    public ErrorMessage(String errorKey, String... messageParameters) {
048        if (StringUtils.isBlank(errorKey)) {
049            throw new IllegalArgumentException("invalid (blank) errorKey");
050        }
051
052        setErrorKey(errorKey);
053        setMessageParameters((String[]) ArrayUtils.clone(messageParameters));
054    }
055
056
057    public void setErrorKey(String errorKey) {
058        if (StringUtils.isBlank(errorKey)) {
059            throw new IllegalArgumentException("invalid (blank) errorKey");
060        }
061
062        this.errorKey = errorKey;
063    }
064
065    public String getErrorKey() {
066        return errorKey;
067    }
068
069
070    public void setMessageParameters(String[] messageParameters) {
071        this.messageParameters = messageParameters;
072    }
073
074    public String[] getMessageParameters() {
075        return messageParameters;
076    }
077
078
079    /**
080     * @see java.lang.Object#toString()
081     */
082    @Override
083    public String toString() {
084        StringBuffer s = new StringBuffer(getErrorKey());
085
086        String[] params = getMessageParameters();
087        if (params != null) {
088            s.append("(");
089            for (int i = 0; i < params.length; ++i) {
090                if (i > 0) {
091                    s.append(", ");
092                }
093                s.append(params[i]);
094            }
095            s.append(")");
096        }
097        return s.toString();
098    }
099
100
101    /**
102     * 
103     * @see java.lang.Object#equals(java.lang.Object)
104     */
105    @Override
106    public boolean equals(Object obj) {
107        boolean equals = false;
108
109        if (this == obj) {
110            equals = true;
111        }
112        else if (obj instanceof ErrorMessage) {
113            ErrorMessage other = (ErrorMessage) obj;
114
115            if (StringUtils.equals(getErrorKey(), other.getErrorKey())) {
116                equals = Arrays.equals(getMessageParameters(), other.getMessageParameters());
117            }
118        }
119
120        return equals;
121    }
122
123    /**
124     * Defined because when you redefine equals, you must redefine hashcode.
125     * 
126     * @see java.lang.Object#hashCode()
127     */
128    @Override
129    public int hashCode() {
130        int hashCode = 5011966;
131
132        if (getErrorKey() != null) {
133            hashCode = getErrorKey().hashCode();
134        }
135
136        return hashCode;
137    }
138}