Fix Sonar findings in TmfEvent
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.event;
15
16 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
17
18 /**
19 * A basic implementation of ITmfEvent.
20 * <p>
21 * Note that for performance reasons TmfEvent is NOT immutable. If a shallow
22 * copy of the event is needed, use the copy constructor. Otherwise (deep copy)
23 * use clone().
24 *
25 * @since 1.0
26 * @version 1.0
27 * @author Francois Chouinard
28 * @see ITmfTimestamp
29 * @see ITmfEventType
30 * @see ITmfEventField
31 * @see ITmfTrace
32 */
33 public class TmfEvent implements ITmfEvent {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 private ITmfTrace<? extends ITmfEvent> fTrace;
40 private long fRank;
41 private ITmfTimestamp fTimestamp;
42 private String fSource;
43 private ITmfEventType fType;
44 private ITmfEventField fContent;
45 private String fReference;
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 /**
52 * Default constructor
53 */
54 public TmfEvent() {
55 this(null, -1, null, null, null, null, null);
56 }
57
58 /**
59 * Constructor - no rank
60 */
61 public TmfEvent(final ITmfTrace<? extends ITmfEvent> trace, final ITmfTimestamp timestamp, final String source,
62 final ITmfEventType type, final ITmfEventField content, final String reference)
63 {
64 this(trace, -1, timestamp, source, type, content, reference);
65 }
66
67 /**
68 * Constructor - no rank, no content
69 */
70 public TmfEvent(final ITmfTrace<? extends ITmfEvent> trace, final ITmfTimestamp timestamp, final String source,
71 final ITmfEventType type, final String reference)
72 {
73 this(trace, -1, timestamp, source, type, null, reference);
74 }
75
76 /**
77 * Constructor - no rank, no content, no trace
78 */
79 public TmfEvent(final ITmfTimestamp timestamp, final String source, final ITmfEventType type, final String reference)
80 {
81 this(null, -1, timestamp, source, type, null, reference);
82 }
83
84 /**
85 * Full constructor
86 *
87 * @param trace the parent trace
88 * @param rank the event rank (in the trace)
89 * @param timestamp the event timestamp
90 * @param source the event source
91 * @param type the event type
92 * @param content the event content (payload)
93 * @param reference the event reference
94 */
95 public TmfEvent(final ITmfTrace<? extends ITmfEvent> trace, final long rank, final ITmfTimestamp timestamp, final String source,
96 final ITmfEventType type, final ITmfEventField content, final String reference)
97 {
98 fTrace = trace;
99 fRank = rank;
100 fTimestamp = timestamp;
101 fSource = source;
102 fType = type;
103 fContent = content;
104 fReference = reference;
105 }
106
107 /**
108 * Copy constructor
109 *
110 * @param event the original event
111 */
112 public TmfEvent(final ITmfEvent event) {
113 if (event == null) {
114 throw new IllegalArgumentException();
115 }
116 fTrace = event.getTrace();
117 fRank = event.getRank();
118 fTimestamp = event.getTimestamp();
119 fSource = event.getSource();
120 fType = event.getType();
121 fContent = event.getContent();
122 fReference = event.getReference();
123 }
124
125 // ------------------------------------------------------------------------
126 // ITmfEvent
127 // ------------------------------------------------------------------------
128
129 /* (non-Javadoc)
130 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTrace()
131 */
132 @Override
133 public ITmfTrace<? extends ITmfEvent> getTrace() {
134 return fTrace;
135 }
136
137 /* (non-Javadoc)
138 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getRank()
139 */
140 @Override
141 public long getRank() {
142 return fRank;
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getTimestamp()
147 */
148 @Override
149 public ITmfTimestamp getTimestamp() {
150 return fTimestamp;
151 }
152
153 /* (non-Javadoc)
154 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getSource()
155 */
156 @Override
157 public String getSource() {
158 return fSource;
159 }
160
161 /* (non-Javadoc)
162 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getType()
163 */
164 @Override
165 public ITmfEventType getType() {
166 return fType;
167 }
168
169 /* (non-Javadoc)
170 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getContent()
171 */
172 @Override
173 public ITmfEventField getContent() {
174 return fContent;
175 }
176
177 /* (non-Javadoc)
178 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEvent#getReference()
179 */
180 @Override
181 public String getReference() {
182 return fReference;
183 }
184
185 // ------------------------------------------------------------------------
186 // Convenience setters
187 // ------------------------------------------------------------------------
188
189 /**
190 * @param trace the new event trace
191 */
192 protected void setTrace(final ITmfTrace<? extends ITmfEvent> trace) {
193 fTrace = trace;
194 }
195
196 /**
197 * @param rank the new event rank
198 */
199 protected void setRank(final long rank) {
200 fRank = rank;
201 }
202
203 /**
204 * @param timestamp the new event timestamp
205 */
206 protected void setTimestamp(final ITmfTimestamp timestamp) {
207 fTimestamp = timestamp;
208 }
209
210 /**
211 * @param source the new event source
212 */
213 protected void setSource(final String source) {
214 fSource = source;
215 }
216
217 /**
218 * @param type the new event type
219 */
220 protected void setType(final ITmfEventType type) {
221 fType = type;
222 }
223
224 /**
225 * @param content the event new content
226 */
227 protected void setContent(final ITmfEventField content) {
228 fContent = content;
229 }
230
231 /**
232 * @param reference the new event reference
233 */
234 protected void setReference(final String reference) {
235 fReference = reference;
236 }
237
238 // ------------------------------------------------------------------------
239 // Cloneable
240 // ------------------------------------------------------------------------
241
242 /* (non-Javadoc)
243 * @see java.lang.Object#clone()
244 */
245 @Override
246 public TmfEvent clone() {
247 TmfEvent clone = null;
248 try {
249 clone = (TmfEvent) super.clone();
250 clone.fTrace = fTrace;
251 clone.fRank = fRank;
252 clone.fTimestamp = fTimestamp != null ? fTimestamp.clone() : null;
253 clone.fSource = fSource;
254 clone.fType = fType != null ? fType.clone() : null;
255 clone.fContent = fContent != null ? fContent.clone() : null;
256 clone.fReference = fReference;
257 } catch (final CloneNotSupportedException e) {
258 }
259 return clone;
260 }
261
262 // ------------------------------------------------------------------------
263 // Object
264 // ------------------------------------------------------------------------
265
266 /* (non-Javadoc)
267 * @see java.lang.Object#hashCode()
268 */
269 @Override
270 public int hashCode() {
271 final int prime = 31;
272 int result = 1;
273 result = prime * result + ((fTrace == null) ? 0 : fTrace.hashCode());
274 result = prime * result + (int) (fRank ^ (fRank >>> 32));
275 result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
276 result = prime * result + ((fSource == null) ? 0 : fSource.hashCode());
277 result = prime * result + ((fType == null) ? 0 : fType.hashCode());
278 result = prime * result + ((fContent == null) ? 0 : fContent.hashCode());
279 result = prime * result + ((fReference == null) ? 0 : fReference.hashCode());
280 return result;
281 }
282
283 /* (non-Javadoc)
284 * @see java.lang.Object#equals(java.lang.Object)
285 */
286 @Override
287 public boolean equals(final Object obj) {
288 if (this == obj) {
289 return true;
290 }
291 if (obj == null) {
292 return false;
293 }
294 if (!(obj instanceof TmfEvent)) {
295 return false;
296 }
297 final TmfEvent other = (TmfEvent) obj;
298 if (fTrace == null) {
299 if (other.fTrace != null) {
300 return false;
301 }
302 } else if (!fTrace.equals(other.fTrace)) {
303 return false;
304 }
305 if (fRank != other.fRank) {
306 return false;
307 }
308 if (fTimestamp == null) {
309 if (other.fTimestamp != null) {
310 return false;
311 }
312 } else if (!fTimestamp.equals(other.fTimestamp)) {
313 return false;
314 }
315 if (fSource == null) {
316 if (other.fSource != null) {
317 return false;
318 }
319 } else if (!fSource.equals(other.fSource)) {
320 return false;
321 }
322 if (fType == null) {
323 if (other.fType != null) {
324 return false;
325 }
326 } else if (!fType.equals(other.fType)) {
327 return false;
328 }
329 if (fContent == null) {
330 if (other.fContent != null) {
331 return false;
332 }
333 } else if (!fContent.equals(other.fContent)) {
334 return false;
335 }
336 if (fReference == null) {
337 if (other.fReference != null) {
338 return false;
339 }
340 } else if (!fReference.equals(other.fReference)) {
341 return false;
342 }
343 return true;
344 }
345
346 /* (non-Javadoc)
347 * @see java.lang.Object#toString()
348 */
349 @Override
350 @SuppressWarnings("nls")
351 public String toString() {
352 return "TmfEvent [fTimestamp=" + fTimestamp + ", fTrace=" + fTrace + ", fRank=" + fRank
353 + ", fSource=" + fSource + ", fType=" + fType + ", fContent=" + fContent
354 + ", fReference=" + fReference + "]";
355 }
356
357 }
This page took 0.039851 seconds and 5 git commands to generate.