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