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