View Javadoc

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  		this("", "");
74  	}
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  		this(source.getPackage().getName(), source.getSimpleName());
85  		if (source.getEnclosingClass() != null) {
86  			setClassName(source.getEnclosingClass().getSimpleName().concat("$").concat(className));
87  		}
88  	}
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 		this(source);
101 		setMethodName(methodName);
102 	}
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 		this(packageName, className, "");
115 	}
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 		this(packageName, className, methodName, "");
130 	}
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 		this(packageName, className, methodName, fileName, 0);
148 	}
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 		this(packageName, className, methodName, fileName, lineNumber, "");
168 	}
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 			String fileName, int lineNumber, String threadName) {
189 		setPackageName(packageName);
190 		setClassName(className);
191 		setMethodName(methodName);
192 		setLineNumber(lineNumber);
193 		setFileName(fileName);
194 		setThreadName(threadName);
195 	}
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 		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 		if (packageName == null) {
217 			this.packageName = "";
218 		} else {
219 			this.packageName = packageName;
220 		}
221 	}
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 		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 		if (className == null) {
243 			this.className = "";
244 		} else {
245 			this.className = className;
246 		}
247 	}
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 		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 		if (methodName == null) {
269 			this.methodName = "";
270 		} else {
271 			this.methodName = methodName;
272 		}
273 	}
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 		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 		this.lineNumber = String.valueOf(lineNumber);
294 	}
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 		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 		if (fileName == null) {
316 			this.fileName = "";
317 		} else {
318 			this.fileName = fileName;
319 		}
320 	}
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 		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 		if (threadName == null) {
342 			this.threadName = "";
343 		} else {
344 			this.threadName = threadName;
345 		}
346 	}
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 		String name = getClassName();
356 		if (getPackageName().length() > 0) {
357 			name = getPackageName().concat(".").concat(name);
358 		}
359 		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 		String name = getFullQualifiedName();
371 		if (getMethodName().length() > 0) {
372 			name = name.concat("#").concat(getMethodName());
373 		}
374 		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 		String name = "";
387 		if (getFileName().length() > 0 && getLineNumber().length() > 0) {
388 			name = getFileName().concat(":").concat(getLineNumber());
389 		}
390 		return name;
391 	}
392 
393 }