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