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  import org.modeshape.common.util.Logger;
31  
32  /**
33   * An immutable representation of a problem, with a status, code, internationalized and parameterized message, values for the
34   * parameters, information about the resource and location, and an optional exception. The use of internationalized messages
35   * allows for automatic localization of the messages (and substitution of the parameter values) via the
36   * {@link #getMessageString()} method.
37   */
38  @Immutable
39  public class Problem {
40  
41      public static final int DEFAULT_CODE = 0;
42  
43      public enum Status {
44          ERROR,
45          WARNING,
46          INFO;
47  
48          public Logger.Level getLogLevel() {
49              switch (this) {
50                  case ERROR:
51                      return Logger.Level.ERROR;
52                  case WARNING:
53                      return Logger.Level.WARNING;
54                  case INFO:
55                  default:
56                      return Logger.Level.INFO;
57              }
58          }
59      }
60  
61      private final Status status;
62      private final I18n message;
63      private final Object[] parameters;
64      private final Throwable throwable;
65      private final int code;
66      private final String resource;
67      private final String location;
68  
69      public Problem( Status status,
70                      int code,
71                      I18n message,
72                      Object[] params,
73                      String resource,
74                      String location,
75                      Throwable throwable ) {
76          CheckArg.isNotNull(status, "status");
77          CheckArg.isNotNull(message, "message");
78          this.status = status;
79          this.code = code;
80          this.message = message;
81          this.parameters = params;
82          this.resource = resource != null ? resource.trim() : null;
83          this.location = location != null ? location.trim() : null;
84          this.throwable = throwable;
85      }
86  
87      /**
88       * @return code
89       */
90      public int getCode() {
91          return this.code;
92      }
93  
94      /**
95       * @return location
96       */
97      public String getLocation() {
98          return this.location;
99      }
100 
101     /**
102      * Get the message written in the current locale.
103      * 
104      * @return the message
105      */
106     public String getMessageString() {
107         return this.message.text(this.parameters);
108     }
109 
110     /**
111      * @return message
112      */
113     public I18n getMessage() {
114         return this.message;
115     }
116 
117     public Object[] getParameters() {
118         return this.parameters;
119     }
120 
121     /**
122      * @return resource
123      */
124     public String getResource() {
125         return this.resource;
126     }
127 
128     /**
129      * @return status
130      */
131     public Status getStatus() {
132         return this.status;
133     }
134 
135     /**
136      * @return throwable
137      */
138     public Throwable getThrowable() {
139         return this.throwable;
140     }
141 
142     /**
143      * {@inheritDoc}
144      * 
145      * @see java.lang.Object#hashCode()
146      */
147     @Override
148     public int hashCode() {
149         return HashCode.compute(status, code, message, resource, location);
150     }
151 
152     /**
153      * {@inheritDoc}
154      * 
155      * @see java.lang.Object#equals(java.lang.Object)
156      */
157     @Override
158     public boolean equals( Object obj ) {
159         if (obj == this) return true;
160         if (obj instanceof Problem) {
161             Problem that = (Problem)obj;
162             if (this.getStatus() != that.getStatus()) return false;
163             if (this.getCode() != that.getCode()) return false;
164             if (!this.getMessage().equals(that.getMessage())) return false;
165             if (!this.getParameters().equals(that.getParameters())) return false;
166 
167             String thisResource = this.getResource();
168             String thatResource = that.getResource();
169             if (thisResource != thatResource) {
170                 if (thisResource == null || !thisResource.equals(thatResource)) return false;
171             }
172 
173             String thisLocation = this.getLocation();
174             String thatLocation = that.getLocation();
175             if (thisLocation != thatLocation) {
176                 if (thisLocation == null || !thisLocation.equals(thatLocation)) return false;
177             }
178 
179             Throwable thisThrowable = this.getThrowable();
180             Throwable thatThrowable = that.getThrowable();
181             if (thisThrowable != thatThrowable) {
182                 if (thisThrowable == null || !thisThrowable.equals(thatThrowable)) return false;
183             }
184             return true;
185         }
186         return false;
187     }
188 
189     /**
190      * {@inheritDoc}
191      * 
192      * @see java.lang.Object#toString()
193      */
194     @Override
195     public String toString() {
196         StringBuilder sb = new StringBuilder();
197         sb.append(this.getStatus()).append(": ");
198         if (this.getCode() != DEFAULT_CODE) {
199             sb.append("(").append(this.getCode()).append(") ");
200         }
201         sb.append(this.getMessageString());
202         if (this.getResource() != null) {
203             sb.append(" Resource=\"").append(this.getResource()).append("\"");
204         }
205         if (this.getLocation() != null) {
206             sb.append(" At \"").append(this.getLocation()).append("\"");
207         }
208         if (this.getThrowable() != null) {
209             sb.append(" (threw ").append(this.getThrowable().getLocalizedMessage()).append(")");
210         }
211         return sb.toString();
212     }
213 
214 }