1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
34
35
36
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
89
90 public int getCode() {
91 return this.code;
92 }
93
94
95
96
97 public String getLocation() {
98 return this.location;
99 }
100
101
102
103
104
105
106 public String getMessageString() {
107 return this.message.text(this.parameters);
108 }
109
110
111
112
113 public I18n getMessage() {
114 return this.message;
115 }
116
117 public Object[] getParameters() {
118 return this.parameters;
119 }
120
121
122
123
124 public String getResource() {
125 return this.resource;
126 }
127
128
129
130
131 public Status getStatus() {
132 return this.status;
133 }
134
135
136
137
138 public Throwable getThrowable() {
139 return this.throwable;
140 }
141
142
143
144
145
146
147 @Override
148 public int hashCode() {
149 return HashCode.compute(status, code, message, resource, location);
150 }
151
152
153
154
155
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
191
192
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 }