View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.krad.util;
17  
18  
19  import org.apache.commons.lang.ArrayUtils;
20  import org.apache.commons.lang.StringUtils;
21  
22  import java.io.Serializable;
23  import java.util.Arrays;
24  
25  
26  /**
27   * Contains the error message key and parameters for a specific instantiation of an error message.
28   * 
29   * 
30   */
31  public class ErrorMessage implements Serializable {
32      private String errorKey;
33      private String[] messageParameters;
34  
35      /**
36       * Default constructor, required by AutoPopulatingList
37       */
38      public ErrorMessage() {
39      }
40  
41      /**
42       * Convenience constructor which sets both fields
43       * 
44       * @param errorKey
45       * @param messageParameters
46       */
47      public ErrorMessage(String errorKey, String... messageParameters) {
48          if (StringUtils.isBlank(errorKey)) {
49              throw new IllegalArgumentException("invalid (blank) errorKey");
50          }
51  
52          setErrorKey(errorKey);
53          setMessageParameters((String[]) ArrayUtils.clone(messageParameters));
54      }
55  
56  
57      public void setErrorKey(String errorKey) {
58          if (StringUtils.isBlank(errorKey)) {
59              throw new IllegalArgumentException("invalid (blank) errorKey");
60          }
61  
62          this.errorKey = errorKey;
63      }
64  
65      public String getErrorKey() {
66          return errorKey;
67      }
68  
69  
70      public void setMessageParameters(String[] messageParameters) {
71          this.messageParameters = messageParameters;
72      }
73  
74      public String[] getMessageParameters() {
75          return messageParameters;
76      }
77  
78  
79      /**
80       * @see java.lang.Object#toString()
81       */
82      @Override
83      public String toString() {
84          StringBuffer s = new StringBuffer(getErrorKey());
85  
86          String[] params = getMessageParameters();
87          if (params != null) {
88              s.append("(");
89              for (int i = 0; i < params.length; ++i) {
90                  if (i > 0) {
91                      s.append(", ");
92                  }
93                  s.append(params[i]);
94              }
95              s.append(")");
96          }
97          return s.toString();
98      }
99  
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 }