| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2012, 2013 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 | * Alexandre Montplaisir - Made immutable |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.core.event; |
| 16 | |
| 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 | * |
| 24 | * @version 1.0 |
| 25 | * @author Francois Chouinard |
| 26 | * |
| 27 | * @see ITmfTimestamp |
| 28 | * @see ITmfEventType |
| 29 | * @see ITmfEventField |
| 30 | * @see ITmfTrace |
| 31 | */ |
| 32 | public class TmfEvent implements ITmfEvent { |
| 33 | |
| 34 | // ------------------------------------------------------------------------ |
| 35 | // Attributes |
| 36 | // ------------------------------------------------------------------------ |
| 37 | |
| 38 | private final ITmfTrace fTrace; |
| 39 | private final long fRank; |
| 40 | private final ITmfTimestamp fTimestamp; |
| 41 | private final String fSource; |
| 42 | private final ITmfEventType fType; |
| 43 | private final ITmfEventField fContent; |
| 44 | private final String fReference; |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constructors |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | /** |
| 51 | * Default constructor. All fields have their default value (null) and the |
| 52 | * event rank is set to TmfContext.UNKNOWN_RANK. |
| 53 | */ |
| 54 | public TmfEvent() { |
| 55 | this(null, ITmfContext.UNKNOWN_RANK, null, null, null, null, null); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Standard constructor. The event rank will be set to TmfContext.UNKNOWN_RANK. |
| 60 | * |
| 61 | * @param trace the parent trace |
| 62 | * @param timestamp the event timestamp |
| 63 | * @param source the event source |
| 64 | * @param type the event type |
| 65 | * @param content the event content (payload) |
| 66 | * @param reference the event reference |
| 67 | |
| 68 | */ |
| 69 | public TmfEvent(final ITmfTrace trace, final ITmfTimestamp timestamp, final String source, |
| 70 | final ITmfEventType type, final ITmfEventField content, final String reference) |
| 71 | { |
| 72 | this(trace, ITmfContext.UNKNOWN_RANK, timestamp, source, type, content, reference); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Full constructor |
| 77 | * |
| 78 | * @param trace the parent trace |
| 79 | * @param rank the event rank (in the trace) |
| 80 | * @param timestamp the event timestamp |
| 81 | * @param source the event source |
| 82 | * @param type the event type |
| 83 | * @param content the event content (payload) |
| 84 | * @param reference the event reference |
| 85 | */ |
| 86 | public TmfEvent(final ITmfTrace trace, final long rank, final ITmfTimestamp timestamp, final String source, |
| 87 | final ITmfEventType type, final ITmfEventField content, final String reference) |
| 88 | { |
| 89 | fTrace = trace; |
| 90 | fRank = rank; |
| 91 | fTimestamp = timestamp; |
| 92 | fSource = source; |
| 93 | fType = type; |
| 94 | fContent = content; |
| 95 | fReference = reference; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Copy constructor |
| 100 | * |
| 101 | * @param event the original event |
| 102 | */ |
| 103 | public TmfEvent(final ITmfEvent event) { |
| 104 | if (event == null) { |
| 105 | throw new IllegalArgumentException(); |
| 106 | } |
| 107 | fTrace = event.getTrace(); |
| 108 | fRank = event.getRank(); |
| 109 | fTimestamp = event.getTimestamp(); |
| 110 | fSource = event.getSource(); |
| 111 | fType = event.getType(); |
| 112 | fContent = event.getContent(); |
| 113 | fReference = event.getReference(); |
| 114 | } |
| 115 | |
| 116 | // ------------------------------------------------------------------------ |
| 117 | // ITmfEvent |
| 118 | // ------------------------------------------------------------------------ |
| 119 | |
| 120 | @Override |
| 121 | public ITmfTrace getTrace() { |
| 122 | return fTrace; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public long getRank() { |
| 127 | return fRank; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ITmfTimestamp getTimestamp() { |
| 132 | return fTimestamp; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String getSource() { |
| 137 | return fSource; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public ITmfEventType getType() { |
| 142 | return fType; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public ITmfEventField getContent() { |
| 147 | return fContent; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public String getReference() { |
| 152 | return fReference; |
| 153 | } |
| 154 | |
| 155 | // ------------------------------------------------------------------------ |
| 156 | // Object |
| 157 | // ------------------------------------------------------------------------ |
| 158 | |
| 159 | @Override |
| 160 | public int hashCode() { |
| 161 | final int prime = 31; |
| 162 | int result = 1; |
| 163 | result = prime * result + ((fTrace == null) ? 0 : fTrace.hashCode()); |
| 164 | result = prime * result + (int) (fRank ^ (fRank >>> 32)); |
| 165 | result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode()); |
| 166 | result = prime * result + ((fSource == null) ? 0 : fSource.hashCode()); |
| 167 | result = prime * result + ((fType == null) ? 0 : fType.hashCode()); |
| 168 | result = prime * result + ((fContent == null) ? 0 : fContent.hashCode()); |
| 169 | result = prime * result + ((fReference == null) ? 0 : fReference.hashCode()); |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(final Object obj) { |
| 175 | if (this == obj) { |
| 176 | return true; |
| 177 | } |
| 178 | if (obj == null) { |
| 179 | return false; |
| 180 | } |
| 181 | if (!(obj instanceof TmfEvent)) { |
| 182 | return false; |
| 183 | } |
| 184 | final TmfEvent other = (TmfEvent) obj; |
| 185 | if (fTrace == null) { |
| 186 | if (other.fTrace != null) { |
| 187 | return false; |
| 188 | } |
| 189 | } else if (!fTrace.equals(other.fTrace)) { |
| 190 | return false; |
| 191 | } |
| 192 | if (fRank != other.fRank) { |
| 193 | return false; |
| 194 | } |
| 195 | if (fTimestamp == null) { |
| 196 | if (other.fTimestamp != null) { |
| 197 | return false; |
| 198 | } |
| 199 | } else if (!fTimestamp.equals(other.fTimestamp)) { |
| 200 | return false; |
| 201 | } |
| 202 | if (fSource == null) { |
| 203 | if (other.fSource != null) { |
| 204 | return false; |
| 205 | } |
| 206 | } else if (!fSource.equals(other.fSource)) { |
| 207 | return false; |
| 208 | } |
| 209 | if (fType == null) { |
| 210 | if (other.fType != null) { |
| 211 | return false; |
| 212 | } |
| 213 | } else if (!fType.equals(other.fType)) { |
| 214 | return false; |
| 215 | } |
| 216 | if (fContent == null) { |
| 217 | if (other.fContent != null) { |
| 218 | return false; |
| 219 | } |
| 220 | } else if (!fContent.equals(other.fContent)) { |
| 221 | return false; |
| 222 | } |
| 223 | if (fReference == null) { |
| 224 | if (other.fReference != null) { |
| 225 | return false; |
| 226 | } |
| 227 | } else if (!fReference.equals(other.fReference)) { |
| 228 | return false; |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | @SuppressWarnings("nls") |
| 235 | public String toString() { |
| 236 | return getClass().getSimpleName() + " [fTimestamp=" + getTimestamp() |
| 237 | + ", fTrace=" + getTrace() + ", fRank=" + getRank() |
| 238 | + ", fSource=" + getSource() + ", fType=" + getType() |
| 239 | + ", fContent=" + getContent() + ", fReference=" + getReference() |
| 240 | + "]"; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @since 2.0 |
| 245 | */ |
| 246 | @Override |
| 247 | public Object getAdapter(Class adapter) { |
| 248 | if (adapter == IPropertySource.class) { |
| 249 | return new TmfEventPropertySource(this); |
| 250 | } |
| 251 | return null; |
| 252 | } |
| 253 | } |