View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.common.collection;
25  
26  import net.jcip.annotations.Immutable;
27  import org.modeshape.common.i18n.I18n;
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.HashCode;
30  
31  /**
32   * An immutable representation of a problem, with a status, code, internationalized and parameterized message, values for the
33   * parameters, information about the resource and location, and an optional exception. The use of internationalized messages
34   * allows for automatic localization of the messages (and substitution of the parameter values) via the
35   * {@link #getMessageString()} method.
36   */
37  @Immutable
38  public class Problem {
39  
40      public static final int DEFAULT_CODE = 0;
41  
42      public enum Status {
43          ERROR,
44          WARNING,
45          INFO;
46      }
47  
48      private final Status status;
49      private final I18n message;
50      private final Object[] parameters;
51      private final Throwable throwable;
52      private final int code;
53      private final String resource;
54      private final String location;
55  
56      public Problem( Status status,
57                      int code,
58                      I18n message,
59                      Object[] params,
60                      String resource,
61                      String location,
62                      Throwable throwable ) {
63          CheckArg.isNotNull(status, "status");
64          CheckArg.isNotNull(message, "message");
65          this.status = status;
66          this.code = code;
67          this.message = message;
68          this.parameters = params;
69          this.resource = resource != null ? resource.trim() : null;
70          this.location = location != null ? location.trim() : null;
71          this.throwable = throwable;
72      }
73  
74      /**
75       * @return code
76       */
77      public int getCode() {
78          return this.code;
79      }
80  
81      /**
82       * @return location
83       */
84      public String getLocation() {
85          return this.location;
86      }
87  
88      /**
89       * Get the message written in the current locale.
90       * 
91       * @return the message
92       */
93      public String getMessageString() {
94          return this.message.text(this.parameters);
95      }
96  
97      /**
98       * @return message
99       */
100     public I18n getMessage() {
101         return this.message;
102     }
103 
104     public Object[] getParameters() {
105         return this.parameters;
106     }
107 
108     /**
109      * @return resource
110      */
111     public String getResource() {
112         return this.resource;
113     }
114 
115     /**
116      * @return status
117      */
118     public Status getStatus() {
119         return this.status;
120     }
121 
122     /**
123      * @return throwable
124      */
125     public Throwable getThrowable() {
126         return this.throwable;
127     }
128 
129     /**
130      * {@inheritDoc}
131      * 
132      * @see java.lang.Object#hashCode()
133      */
134     @Override
135     public int hashCode() {
136         return HashCode.compute(status, code, message, resource, location);
137     }
138 
139     /**
140      * {@inheritDoc}
141      * 
142      * @see java.lang.Object#equals(java.lang.Object)
143      */
144     @Override
145     public boolean equals( Object obj ) {
146         if (obj == this) return true;
147         if (obj instanceof Problem) {
148             Problem that = (Problem)obj;
149             if (this.getStatus() != that.getStatus()) return false;
150             if (this.getCode() != that.getCode()) return false;
151             if (!this.getMessage().equals(that.getMessage())) return false;
152             if (!this.getParameters().equals(that.getParameters())) return false;
153 
154             String thisResource = this.getResource();
155             String thatResource = that.getResource();
156             if (thisResource != thatResource) {
157                 if (thisResource == null || !thisResource.equals(thatResource)) return false;
158             }
159 
160             String thisLocation = this.getLocation();
161             String thatLocation = that.getLocation();
162             if (thisLocation != thatLocation) {
163                 if (thisLocation == null || !thisLocation.equals(thatLocation)) return false;
164             }
165 
166             Throwable thisThrowable = this.getThrowable();
167             Throwable thatThrowable = that.getThrowable();
168             if (thisThrowable != thatThrowable) {
169                 if (thisThrowable == null || !thisThrowable.equals(thatThrowable)) return false;
170             }
171             return true;
172         }
173         return false;
174     }
175 
176     /**
177      * {@inheritDoc}
178      * 
179      * @see java.lang.Object#toString()
180      */
181     @Override
182     public String toString() {
183         StringBuilder sb = new StringBuilder();
184         sb.append(this.getStatus()).append(": ");
185         if (this.getCode() != DEFAULT_CODE) {
186             sb.append("(").append(this.getCode()).append(") ");
187         }
188         sb.append(this.getMessageString());
189         if (this.getResource() != null) {
190             sb.append(" Resource=\"").append(this.getResource()).append("\"");
191         }
192         if (this.getLocation() != null) {
193             sb.append(" At \"").append(this.getLocation()).append("\"");
194         }
195         if (this.getThrowable() != null) {
196             sb.append(" (threw ").append(this.getThrowable().getLocalizedMessage()).append(")");
197         }
198         return sb.toString();
199     }
200 
201 }