| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2015 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, updated as per TMF Event Model 1.0 |
| 11 | * Alexandre Montplaisir - Made immutable, consolidated constructors |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.tmf.core.event; |
| 15 | |
| 16 | import org.eclipse.core.runtime.PlatformObject; |
| 17 | import org.eclipse.jdt.annotation.NonNull; |
| 18 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 19 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
| 20 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
| 21 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
| 22 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 23 | |
| 24 | /** |
| 25 | * A basic implementation of ITmfEvent. |
| 26 | * |
| 27 | * @author Francois Chouinard |
| 28 | * |
| 29 | * @see ITmfTimestamp |
| 30 | * @see ITmfEventType |
| 31 | * @see ITmfEventField |
| 32 | * @see ITmfTrace |
| 33 | */ |
| 34 | public class TmfEvent extends PlatformObject implements ITmfEvent { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Attributes |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | private final ITmfTrace fTrace; |
| 41 | private final long fRank; |
| 42 | private final @NonNull ITmfTimestamp fTimestamp; |
| 43 | private final ITmfEventType fType; |
| 44 | private final ITmfEventField fContent; |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constructors |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | /** |
| 51 | * Default constructor. Is required for extension points, but should not be |
| 52 | * used normally. |
| 53 | * |
| 54 | * @deprecated Do not use, extension-point use only. Use |
| 55 | * {@link #TmfEvent(ITmfTrace, long, ITmfTimestamp, ITmfEventType, ITmfEventField)} |
| 56 | * instead. |
| 57 | */ |
| 58 | @Deprecated |
| 59 | public TmfEvent() { |
| 60 | this(null, ITmfContext.UNKNOWN_RANK, null, null, null); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Full constructor |
| 65 | * |
| 66 | * @param trace |
| 67 | * the parent trace |
| 68 | * @param rank |
| 69 | * the event rank (in the trace). You can use |
| 70 | * {@link ITmfContext#UNKNOWN_RANK} as default value |
| 71 | * @param timestamp |
| 72 | * the event timestamp |
| 73 | * @param type |
| 74 | * the event type |
| 75 | * @param content |
| 76 | * the event content (payload) |
| 77 | */ |
| 78 | public TmfEvent(final ITmfTrace trace, |
| 79 | final long rank, |
| 80 | final ITmfTimestamp timestamp, |
| 81 | final ITmfEventType type, |
| 82 | final ITmfEventField content) { |
| 83 | fTrace = trace; |
| 84 | fRank = rank; |
| 85 | if (timestamp != null) { |
| 86 | fTimestamp = timestamp; |
| 87 | } else { |
| 88 | fTimestamp = TmfTimestamp.ZERO; |
| 89 | } |
| 90 | fType = type; |
| 91 | fContent = content; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Copy constructor |
| 96 | * |
| 97 | * @param event the original event |
| 98 | */ |
| 99 | public TmfEvent(final @NonNull ITmfEvent event) { |
| 100 | fTrace = event.getTrace(); |
| 101 | fRank = event.getRank(); |
| 102 | fTimestamp = event.getTimestamp(); |
| 103 | fType = event.getType(); |
| 104 | fContent = event.getContent(); |
| 105 | } |
| 106 | |
| 107 | // ------------------------------------------------------------------------ |
| 108 | // ITmfEvent |
| 109 | // ------------------------------------------------------------------------ |
| 110 | |
| 111 | @Override |
| 112 | public ITmfTrace getTrace() { |
| 113 | ITmfTrace trace = fTrace; |
| 114 | if (trace == null) { |
| 115 | throw new IllegalStateException("Null traces are only allowed on special kind of events and getTrace() should not be called on them"); //$NON-NLS-1$ |
| 116 | } |
| 117 | return trace; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public long getRank() { |
| 122 | return fRank; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public ITmfTimestamp getTimestamp() { |
| 127 | return fTimestamp; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ITmfEventType getType() { |
| 132 | return fType; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public ITmfEventField getContent() { |
| 137 | return fContent; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @since 1.0 |
| 142 | */ |
| 143 | @Override |
| 144 | public String getName() { |
| 145 | ITmfEventType type = getType(); |
| 146 | if (type != null) { |
| 147 | return type.getName(); |
| 148 | } |
| 149 | return ""; //$NON-NLS-1$ |
| 150 | } |
| 151 | |
| 152 | // ------------------------------------------------------------------------ |
| 153 | // Object |
| 154 | // ------------------------------------------------------------------------ |
| 155 | |
| 156 | @Override |
| 157 | public int hashCode() { |
| 158 | final int prime = 31; |
| 159 | int result = 1; |
| 160 | result = prime * result + ((fTrace == null) ? 0 : getTrace().hashCode()); |
| 161 | result = prime * result + (int) (getRank() ^ (getRank() >>> 32)); |
| 162 | result = prime * result + getTimestamp().hashCode(); |
| 163 | result = prime * result + ((getType() == null) ? 0 : getType().hashCode()); |
| 164 | result = prime * result + ((getContent() == null) ? 0 : getContent().hashCode()); |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean equals(final Object obj) { |
| 170 | if (this == obj) { |
| 171 | return true; |
| 172 | } |
| 173 | if (obj == null) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /* Two events must be of the exact same class to be equal */ |
| 178 | if (!(this.getClass().equals(obj.getClass()))) { |
| 179 | return false; |
| 180 | } |
| 181 | final TmfEvent other = (TmfEvent) obj; |
| 182 | |
| 183 | if (fTrace == null) { |
| 184 | if (other.fTrace != null) { |
| 185 | return false; |
| 186 | } |
| 187 | } else if (!getTrace().equals(other.getTrace())) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (getRank() != other.getRank()) { |
| 192 | return false; |
| 193 | } |
| 194 | if (!getTimestamp().equals(other.getTimestamp())) { |
| 195 | return false; |
| 196 | } |
| 197 | if (!NonNullUtils.equalsNullable(getType(), other.getType())) { |
| 198 | return false; |
| 199 | } |
| 200 | if (!NonNullUtils.equalsNullable(getContent(), other.getContent())) { |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | @SuppressWarnings("nls") |
| 208 | public String toString() { |
| 209 | return getClass().getSimpleName() + " [fTimestamp=" + getTimestamp() |
| 210 | + ", fTrace=" + getTrace() + ", fRank=" + getRank() |
| 211 | + ", fType=" + getType() + ", fContent=" + getContent() |
| 212 | + "]"; |
| 213 | } |
| 214 | |
| 215 | } |