Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LogEventLocation |
|
| 1.434782608695652;1,435 |
1 | /* | |
2 | * This file is part of AceLogger. | |
3 | * | |
4 | * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU | |
5 | * Lesser General Public License as published by the Free Software Foundation, either version 3 of | |
6 | * the License, or (at your option) any later version. | |
7 | * | |
8 | * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | |
9 | * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * Lesser General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU Lesser General Public License along with AceLogger. | |
13 | * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. | |
14 | */ | |
15 | package net.sourceforge.acelogger.location; | |
16 | ||
17 | import java.io.Serializable; | |
18 | ||
19 | /** | |
20 | * Stores location information for a log event. | |
21 | * | |
22 | * @author Zardi (https://sourceforge.net/users/daniel_zardi) | |
23 | * @version 1.0.0 | |
24 | * @since 1.0.0 | |
25 | */ | |
26 | public class LogEventLocation implements Serializable { | |
27 | ||
28 | /** | |
29 | * This constant is used when a serialized object is read to ensure that the object is | |
30 | * compatible with the current version of this class. For more info on version changes see | |
31 | * <a href="http://java.sun.com/javase/6/docs/platform/serialization/spec/version.html#6678"> | |
32 | * Type Changes Affecting Serialization | |
33 | * </a>. | |
34 | */ | |
35 | private static final long serialVersionUID = 1L; | |
36 | ||
37 | /** | |
38 | * The caller package name. | |
39 | */ | |
40 | private String packageName; | |
41 | ||
42 | /** | |
43 | * The caller class name. | |
44 | */ | |
45 | private String className; | |
46 | ||
47 | /** | |
48 | * The caller method name. | |
49 | */ | |
50 | private String methodName; | |
51 | ||
52 | /** | |
53 | * The caller line number. | |
54 | */ | |
55 | private String lineNumber; | |
56 | ||
57 | /** | |
58 | * The caller file name. | |
59 | */ | |
60 | private String fileName; | |
61 | ||
62 | /** | |
63 | * The caller thread name. | |
64 | */ | |
65 | private String threadName; | |
66 | ||
67 | /** | |
68 | * Constructs a new LogEventLocation without any information. | |
69 | * | |
70 | * @since 1.0.0 | |
71 | */ | |
72 | public LogEventLocation() { | |
73 | 1 | this("", ""); |
74 | 1 | } |
75 | ||
76 | /** | |
77 | * Constructs a new LogEventLocation for a given class. | |
78 | * | |
79 | * @param source | |
80 | * The callers class. | |
81 | * @since 1.0.0 | |
82 | */ | |
83 | public LogEventLocation(Class<?> source) { | |
84 | 0 | this(source.getPackage().getName(), source.getSimpleName()); |
85 | 0 | if (source.getEnclosingClass() != null) { |
86 | 0 | setClassName(source.getEnclosingClass().getSimpleName().concat("$").concat(className)); |
87 | } | |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * Constructs a new LogEventLocation for a given method and class. | |
92 | * | |
93 | * @param source | |
94 | * The caller class. | |
95 | * @param methodName | |
96 | * The caller method. | |
97 | * @since 1.0.0 | |
98 | */ | |
99 | public LogEventLocation(Class<?> source, String methodName) { | |
100 | 0 | this(source); |
101 | 0 | setMethodName(methodName); |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * Constructs a new LogEventLocation for a given package and class. | |
106 | * | |
107 | * @param packageName | |
108 | * The caller package name. | |
109 | * @param className | |
110 | * The caller class name. | |
111 | * @since 1.0.0 | |
112 | */ | |
113 | public LogEventLocation(String packageName, String className) { | |
114 | 1 | this(packageName, className, ""); |
115 | 1 | } |
116 | ||
117 | /** | |
118 | * Constructs a new LogEventLocation for a given package, class and method. | |
119 | * | |
120 | * @param packageName | |
121 | * The caller package name. | |
122 | * @param className | |
123 | * The caller class name. | |
124 | * @param methodName | |
125 | * The caller method name. | |
126 | * @since 1.0.0 | |
127 | */ | |
128 | public LogEventLocation(String packageName, String className, String methodName) { | |
129 | 6 | this(packageName, className, methodName, ""); |
130 | 6 | } |
131 | ||
132 | /** | |
133 | * Constructs a new LogEventLocation for a given package, class, method and file. | |
134 | * | |
135 | * @param packageName | |
136 | * The caller package name. | |
137 | * @param className | |
138 | * The caller class name. | |
139 | * @param methodName | |
140 | * The caller method name. | |
141 | * @param fileName | |
142 | * The caller file name. | |
143 | * @since 1.0.0 | |
144 | */ | |
145 | public LogEventLocation(String packageName, String className, String methodName, | |
146 | String fileName) { | |
147 | 6 | this(packageName, className, methodName, fileName, 0); |
148 | 6 | } |
149 | ||
150 | /** | |
151 | * Constructs a new LogEventLocation for a given package, class, method, file and line. | |
152 | * | |
153 | * @param packageName | |
154 | * The caller package name. | |
155 | * @param className | |
156 | * The caller class name. | |
157 | * @param methodName | |
158 | * The caller method name. | |
159 | * @param fileName | |
160 | * The caller file name. | |
161 | * @param lineNumber | |
162 | * The caller line number. | |
163 | * @since 1.0.0 | |
164 | */ | |
165 | public LogEventLocation(String packageName, String className, String methodName, | |
166 | String fileName, int lineNumber) { | |
167 | 6 | this(packageName, className, methodName, fileName, lineNumber, ""); |
168 | 6 | } |
169 | ||
170 | /** | |
171 | * Constructs a new LogEventLocation for a given package, class, method, file, line and thread. | |
172 | * | |
173 | * @param packageName | |
174 | * The caller package name. | |
175 | * @param className | |
176 | * The caller class name. | |
177 | * @param methodName | |
178 | * The caller method name. | |
179 | * @param fileName | |
180 | * The caller file name. | |
181 | * @param lineNumber | |
182 | * The caller line number. | |
183 | * @param threadName | |
184 | * The caller thread name. | |
185 | * @since 1.0.0 | |
186 | */ | |
187 | public LogEventLocation(String packageName, String className, String methodName, | |
188 | 16 | String fileName, int lineNumber, String threadName) { |
189 | 16 | setPackageName(packageName); |
190 | 16 | setClassName(className); |
191 | 16 | setMethodName(methodName); |
192 | 16 | setLineNumber(lineNumber); |
193 | 16 | setFileName(fileName); |
194 | 16 | setThreadName(threadName); |
195 | 16 | } |
196 | ||
197 | /** | |
198 | * Gets the caller package name. | |
199 | * | |
200 | * @return The caller package name. | |
201 | * @since 1.0.0 | |
202 | */ | |
203 | public final String getPackageName() { | |
204 | 17 | return packageName; |
205 | } | |
206 | ||
207 | /** | |
208 | * Sets the caller package name. If the name is <code>null</code>, the package will be | |
209 | * identified by a empty string. | |
210 | * | |
211 | * @param packageName | |
212 | * The caller package name. | |
213 | * @since 1.0.0 | |
214 | */ | |
215 | public final void setPackageName(String packageName) { | |
216 | 17 | if (packageName == null) { |
217 | 1 | this.packageName = ""; |
218 | } else { | |
219 | 16 | this.packageName = packageName; |
220 | } | |
221 | 17 | } |
222 | ||
223 | /** | |
224 | * Gets the caller class name. | |
225 | * | |
226 | * @return The caller class name. | |
227 | * @since 1.0.0 | |
228 | */ | |
229 | public final String getClassName() { | |
230 | 11 | return className; |
231 | } | |
232 | ||
233 | /** | |
234 | * Sets the caller class name. If the name is <code>null</code>, the class will be identified by | |
235 | * a empty string. | |
236 | * | |
237 | * @param className | |
238 | * The caller class name. | |
239 | * @since 1.0.0 | |
240 | */ | |
241 | public final void setClassName(String className) { | |
242 | 17 | if (className == null) { |
243 | 1 | this.className = ""; |
244 | } else { | |
245 | 16 | this.className = className; |
246 | } | |
247 | 17 | } |
248 | ||
249 | /** | |
250 | * Gets the caller method name. | |
251 | * | |
252 | * @return The caller method name. | |
253 | * @since 1.0.0 | |
254 | */ | |
255 | public final String getMethodName() { | |
256 | 8 | return methodName; |
257 | } | |
258 | ||
259 | /** | |
260 | * Sets the caller method name. If the name is <code>null</code>, the method will be identified | |
261 | * by a empty string. | |
262 | * | |
263 | * @param methodName | |
264 | * The caller method name. | |
265 | * @since 1.0.0 | |
266 | */ | |
267 | public final void setMethodName(String methodName) { | |
268 | 17 | if (methodName == null) { |
269 | 1 | this.methodName = ""; |
270 | } else { | |
271 | 16 | this.methodName = methodName; |
272 | } | |
273 | 17 | } |
274 | ||
275 | /** | |
276 | * Gets the caller line number. | |
277 | * | |
278 | * @return The caller line number. | |
279 | * @since 1.0.0 | |
280 | */ | |
281 | public final String getLineNumber() { | |
282 | 22 | return lineNumber; |
283 | } | |
284 | ||
285 | /** | |
286 | * Sets the caller line number. | |
287 | * | |
288 | * @param lineNumber | |
289 | * The caller line number. | |
290 | * @since 1.0.0 | |
291 | */ | |
292 | public final void setLineNumber(int lineNumber) { | |
293 | 16 | this.lineNumber = String.valueOf(lineNumber); |
294 | 16 | } |
295 | ||
296 | /** | |
297 | * Gets the caller file name. | |
298 | * | |
299 | * @return The caller file name. | |
300 | * @since 1.0.0 | |
301 | */ | |
302 | public final String getFileName() { | |
303 | 31 | return fileName; |
304 | } | |
305 | ||
306 | /** | |
307 | * Sets the caller file name. If the name is <code>null</code>, the file will be identified by a | |
308 | * empty string. | |
309 | * | |
310 | * @param fileName | |
311 | * The caller file name. | |
312 | * @since 1.0.0 | |
313 | */ | |
314 | public final void setFileName(String fileName) { | |
315 | 18 | if (fileName == null) { |
316 | 1 | this.fileName = ""; |
317 | } else { | |
318 | 17 | this.fileName = fileName; |
319 | } | |
320 | 18 | } |
321 | ||
322 | /** | |
323 | * Gets the caller thread name. | |
324 | * | |
325 | * @return The caller thread name. | |
326 | * @since 1.0.0 | |
327 | */ | |
328 | public final String getThreadName() { | |
329 | 2 | return threadName; |
330 | } | |
331 | ||
332 | /** | |
333 | * Sets the caller thread name. If the name is <code>null</code>, the thread will be identified | |
334 | * by a empty string. | |
335 | * | |
336 | * @param threadName | |
337 | * The caller thread name. | |
338 | * @since 1.0.0 | |
339 | */ | |
340 | public final void setThreadName(String threadName) { | |
341 | 18 | if (threadName == null) { |
342 | 1 | this.threadName = ""; |
343 | } else { | |
344 | 17 | this.threadName = threadName; |
345 | } | |
346 | 18 | } |
347 | ||
348 | /** | |
349 | * Gets the full qualified name of the callers class. | |
350 | * | |
351 | * @return The full qualified name of the callers class. | |
352 | * @since 1.0.0 | |
353 | */ | |
354 | public String getFullQualifiedName() { | |
355 | 6 | String name = getClassName(); |
356 | 6 | if (getPackageName().length() > 0) { |
357 | 6 | name = getPackageName().concat(".").concat(name); |
358 | } | |
359 | 6 | return name; |
360 | } | |
361 | ||
362 | /** | |
363 | * Gets a javadoc like name for the caller, this name resembles something like | |
364 | * <i>caller.package.SomeClass#aMethod</i>. | |
365 | * | |
366 | * @return A javadoc like name for the caller. | |
367 | * @since 1.0.0 | |
368 | */ | |
369 | public String getJavadocLikeName() { | |
370 | 3 | String name = getFullQualifiedName(); |
371 | 3 | if (getMethodName().length() > 0) { |
372 | 3 | name = name.concat("#").concat(getMethodName()); |
373 | } | |
374 | 3 | return name; |
375 | } | |
376 | ||
377 | /** | |
378 | * Gets a string representing the file name and line number of the caller. If there is no | |
379 | * information about them, an empty string is returned. The resulting string will resemble | |
380 | * something like <i>(file.java:10)</i>. | |
381 | * | |
382 | * @return A string representing the file name and line number of the caller. | |
383 | * @since 1.0.0 | |
384 | */ | |
385 | public String getStackTraceLikeName() { | |
386 | 18 | String name = ""; |
387 | 18 | if (getFileName().length() > 0 && getLineNumber().length() > 0) { |
388 | 11 | name = getFileName().concat(":").concat(getLineNumber()); |
389 | } | |
390 | 18 | return name; |
391 | } | |
392 | ||
393 | } |