1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.trace;
39
40 import com.gargoylesoftware.base.util.DetailedNullPointerException;
41 import java.util.Date;
42
43 /***
44 * An item of data to be written.
45 *
46 * @version $Revision: 1.5 $
47 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
48 */
49 public class TraceItem implements Cloneable {
50
51 private TraceItem prevItem_;
52 private TraceItem nextItem_;
53
54 private TraceChannel channel_;
55
56 private String message_;
57 private Throwable throwable_;
58 private Date time_;
59 private Thread thread_;
60 private Object lock_;
61
62 /***
63 * Create a new item.
64 */
65 public TraceItem() {
66 }
67
68 /***
69 * Reset all the values to null
70 */
71 /*package*/void clear() {/package-summary/html">class="comment">package*/void clear() {/package-summary.html">void clear() {
72 message_ = null;
73 throwable_ = null;
74 time_ = null;
75 thread_ = null;
76 lock_ = null;
77 channel_ = null;
78 }
79
80 /*** @return true if the message has no content */
81 boolean isClear() {
82 return message_ == null
83 && throwable_ == null
84 && time_ == null
85 && thread_ == null
86 && lock_ == null
87 && channel_ == null;
88 }
89
90 /***
91 * Return a string representation of this object for testing purposes.
92 * @return The string representation
93 */
94 public String toString() {
95 final StringBuffer buffer = new StringBuffer(200);
96 buffer.append("TraceItem[message=[");
97 buffer.append(message_);
98 buffer.append("] throwable=[");
99 buffer.append(throwable_);
100 buffer.append("] time=[");
101 buffer.append(time_);
102 buffer.append("] thread=[");
103 buffer.append(thread_);
104 buffer.append("] lock=[");
105 buffer.append(lock_);
106 buffer.append("] channel=[");
107 buffer.append(channel_);
108 buffer.append("]]");
109 return buffer.toString();
110 }
111 /***
112 * Return true if this item contains any text.
113 * @return true if this item contains any text.
114 */
115 public boolean containsText() {
116 return message_ != null | throwable_ != null;
117 }
118
119 /***
120 * Return the message.
121 * @return the message.
122 */
123 public String getMessage() {
124 return message_;
125 }
126
127 /*** @param string The message */
128 void setMessage( final String string ) {
129 message_ = string;
130 }
131
132 /***
133 * Return the throwable.
134 * @return the throwable.
135 */
136 public Throwable getThrowable() {
137 return throwable_;
138 }
139
140 /*** @param t The throwable */
141 void setThrowable( final Throwable t ) {
142 throwable_ = t;
143 }
144
145 /***
146 * Return the time that the item was written to Trace
147 * @return the time.
148 */
149 public Date getTime() {
150 return time_;
151 }
152
153 /*** @param time The time */
154 void setTime( final Date time ) {
155 assertNotNull("time", time);
156 time_ = time;
157 }
158
159 /*** @return The thread */
160 Thread getThread() {
161 return thread_;
162 }
163
164 /*** @param thread The thread */
165 void setThread( final Thread thread ) {
166 assertNotNull("thread", thread);
167 thread_ = thread;
168 }
169 /***
170 * Return the name of the thread that called Trace.
171 * @return the name of the thread.
172 */
173 public String getThreadName() {
174
175 if( thread_ == null ) {
176 return null;
177 }
178
179 try {
180 return thread_.getName();
181 }
182 catch( final NullPointerException e ) {
183
184
185 return "Unknown thread[hash=" + thread_.hashCode() + "]";
186 }
187 }
188
189 /***
190 * Return the lock.
191 * @return the lock.
192 */
193 public Object getLock() {
194 return lock_;
195 }
196
197 /*** @param lock The lock */
198 void setLock( final Object lock ) {
199 lock_ = lock;
200 }
201
202 /***
203 * Return the previous item.
204 * @return the previous item.
205 */
206 public TraceItem getPrevItem() {
207 return prevItem_;
208 }
209
210 /*** @param item The previous item */
211 void setPrevItem( final TraceItem item ) {
212 prevItem_ = item;
213 }
214
215 /***
216 * Return the next item.
217 * @return the next item.
218 */
219 public TraceItem getNextItem() {
220 return nextItem_;
221 }
222
223 /*** @param item The next item */
224 void setNextItem( final TraceItem item ) {
225 nextItem_ = item;
226 }
227
228 /***
229 * Return the channel.
230 * @return the channel.
231 */
232 public TraceChannel getChannel() {
233 return channel_;
234 }
235
236 /*** @param channel The channel */
237 void setChannel( final TraceChannel channel ) {
238 assertNotNull("channel", channel);
239 channel_ = channel;
240 }
241
242 /***
243 * Return a copy of this object.
244 * @return A copy.
245 * @throws CloneNotSupportedException If this object is not cloneable.
246 */
247 public Object clone() throws CloneNotSupportedException {
248 return super.clone();
249 }
250
251
252 /***
253 * Verify that the specified value is not null. If it is then throw an exception
254 *
255 * @param fieldName The name of the field to check
256 * @param fieldValue The value of the field to check
257 * @exception DetailedNullPointerException If fieldValue is null
258 */
259 protected final void assertNotNull( final String fieldName, final Object fieldValue )
260 throws DetailedNullPointerException {
261
262 if( fieldValue == null ) {
263 throw new DetailedNullPointerException(fieldName);
264 }
265 }
266 }
267