Add a trace reference to the base event structure (fix for Bug360566)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / event / TmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.event;
14
15 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
16
17 /**
18 * <b><u>TmfEvent</u></b>
19 * <p>
20 * The basic event structure in the TMF. In its canonical form, an event has:
21 * <ul>
22 * <li> a normalized timestamp
23 * <li> a source (reporter)
24 * <li> a type
25 * <li> a content
26 * </ul>
27 * For convenience, a free-form reference field is also provided. It could be
28 * used as e.g. a location marker in the event stream to distinguish between
29 * otherwise identical events.
30 *
31 * Notice that for performance reasons TmfEvent is NOT immutable. If a copy
32 * of the event is needed, use the copy constructor.
33 */
34 public class TmfEvent extends TmfData implements Cloneable {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39
40 public static final TmfEvent NullEvent = new TmfEvent();
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 protected ITmfTrace fParentTrace;
47 protected long fEventRank;
48 protected TmfTimestamp fEffectiveTimestamp;
49 protected TmfTimestamp fOriginalTimestamp;
50 protected TmfEventSource fSource;
51 protected TmfEventType fType;
52 protected TmfEventReference fReference;
53
54 // Content requires a reference to the parent event so it is initialized
55 // using setContent()
56 protected TmfEventContent fContent;
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61
62 /**
63 * @param originalTS the original timestamp
64 * @param effectiveTS the effective timestamp
65 * @param source the event source (generator)
66 * @param type the event type
67 * @param reference a free-form reference field
68 */
69 public TmfEvent(ITmfTrace trace, long rank, TmfTimestamp originalTS, TmfTimestamp effectiveTS,
70 TmfEventSource source, TmfEventType type, TmfEventReference reference)
71 {
72 fParentTrace = trace;
73 fEventRank = rank;
74 fOriginalTimestamp = originalTS;
75 fEffectiveTimestamp = effectiveTS;
76 fSource = source;
77 fType = type;
78 fReference = reference;
79 }
80
81 /**
82 * @param originalTS the original timestamp
83 * @param effectiveTS the effective timestamp
84 * @param source the event source (generator)
85 * @param type the event type
86 * @param reference a free-form reference field
87 */
88 public TmfEvent(TmfTimestamp originalTS, TmfTimestamp effectiveTS,
89 TmfEventSource source, TmfEventType type, TmfEventReference reference)
90 {
91 this(null, -1, originalTS, effectiveTS, source, type, reference);
92 }
93
94 /**
95 * @param timestamp the effective timestamp
96 * @param source the event source (generator)
97 * @param type the event type
98 * @param reference a free-form reference field
99 */
100 public TmfEvent(TmfTimestamp timestamp, TmfEventSource source,
101 TmfEventType type, TmfEventReference reference)
102 {
103 this(null, -1, timestamp, timestamp, source, type, reference);
104 }
105
106 /**
107 * Copy constructor
108 *
109 * @param other the original event
110 */
111 public TmfEvent(TmfEvent other) {
112 if (other == null)
113 throw new IllegalArgumentException();
114 fParentTrace = other.fParentTrace;
115 fEventRank = other.fEventRank;
116 fOriginalTimestamp = new TmfTimestamp(other.fOriginalTimestamp);
117 fEffectiveTimestamp = new TmfTimestamp(other.fEffectiveTimestamp);
118 fSource = new TmfEventSource(other.fSource);
119 fType = new TmfEventType(other.fType);
120 fContent = new TmfEventContent(other.fContent);
121 fReference = new TmfEventReference(other.fReference);
122 }
123
124 private TmfEvent() {
125 }
126
127 @Override
128 public boolean isNullRef() {
129 return this == NullEvent;
130 }
131
132 // ------------------------------------------------------------------------
133 // Accessors
134 // ------------------------------------------------------------------------
135
136 /**
137 * @return the parent trace
138 */
139 public ITmfTrace getParentTrace() {
140 return fParentTrace;
141 }
142
143 /**
144 * @return the event rank
145 */
146 public long getEventRank() {
147 return fEventRank;
148 }
149
150 /**
151 * @return the effective event timestamp
152 */
153 public TmfTimestamp getTimestamp() {
154 return fEffectiveTimestamp;
155 }
156
157 /**
158 * @return the original event timestamp
159 */
160 public TmfTimestamp getOriginalTimestamp() {
161 return fOriginalTimestamp;
162 }
163
164 /**
165 * @return the event source
166 */
167 public TmfEventSource getSource() {
168 return fSource;
169 }
170
171 /**
172 * @return the event type
173 */
174 public TmfEventType getType() {
175 return fType;
176 }
177
178 /**
179 * @return the event content
180 */
181 public TmfEventContent getContent() {
182 return fContent;
183 }
184
185 /**
186 * @return the event reference
187 */
188 public TmfEventReference getReference() {
189 return fReference;
190 }
191
192 /**
193 * @param content the new event content
194 */
195 public void setContent(TmfEventContent content) {
196 fContent = content;
197 }
198
199 /**
200 * @return the event raw text
201 */
202 public String getRawText() {
203 if (fContent != null && fContent.getContent() != null) {
204 return fContent.getContent().toString();
205 }
206 return ""; //$NON-NLS-1$
207 }
208
209 // ------------------------------------------------------------------------
210 // Object
211 // ------------------------------------------------------------------------
212
213 @Override
214 public int hashCode() {
215 int result = 17;
216 result = 37 * result + fSource.hashCode();
217 result = 37 * result + fType.hashCode();
218 result = 37 * result + fEffectiveTimestamp.hashCode();
219 return result;
220 }
221
222 @Override
223 public boolean equals(Object other) {
224 if (!(other instanceof TmfEvent))
225 return false;
226 TmfEvent o = (TmfEvent) other;
227 return fEffectiveTimestamp.equals(o.fEffectiveTimestamp) &&
228 fSource.equals(o.fSource) &&
229 fType.equals(o.fType) &&
230 fContent.equals(o.fContent);
231 }
232
233 @Override
234 @SuppressWarnings("nls")
235 public String toString() {
236 return "[TmfEvent(" + fEffectiveTimestamp + "," + fSource + "," + fType + "," + fContent + ")]";
237 }
238
239 @Override
240 public TmfEvent clone() {
241 TmfEvent clone = null;
242 try {
243 clone = (TmfEvent) super.clone();
244 clone.fParentTrace = fParentTrace;
245 clone.fEventRank = fEventRank;
246 clone.fOriginalTimestamp = fOriginalTimestamp.clone();
247 clone.fEffectiveTimestamp = fEffectiveTimestamp.clone();
248 clone.fSource = fSource.clone();
249 clone.fType = fType.clone();
250 clone.fReference = fReference.clone();
251 clone.fContent = fContent.clone();
252 }
253 catch (CloneNotSupportedException e) {
254 e.printStackTrace();
255 }
256
257 return clone;
258 }
259
260 }
This page took 0.036683 seconds and 5 git commands to generate.