| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others |
| 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: Matthew Khouzam - Initial API and implementation |
| 10 | * Contributors: Simon Marchi - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.ctf.core.event; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 20 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
| 21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
| 22 | import org.eclipse.linuxtools.ctf.core.trace.Stream; |
| 23 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; |
| 24 | |
| 25 | /** |
| 26 | * Representation of one type of event. A bit like "int" or "long" but for trace |
| 27 | * events. |
| 28 | */ |
| 29 | public class EventDeclaration implements IEventDeclaration { |
| 30 | |
| 31 | /** Id of lost events */ |
| 32 | public static final long LOST_EVENT_ID = -1L; |
| 33 | |
| 34 | /** Id of events when not set */ |
| 35 | public static final long UNSET_EVENT_ID = -2L; |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // Attributes |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
| 41 | /** |
| 42 | * Name of the event |
| 43 | */ |
| 44 | private String name; |
| 45 | |
| 46 | /** |
| 47 | * Event context structure declaration |
| 48 | */ |
| 49 | private StructDeclaration context = null; |
| 50 | |
| 51 | /** |
| 52 | * Event fields structure declaration |
| 53 | */ |
| 54 | private StructDeclaration fields = null; |
| 55 | |
| 56 | /** |
| 57 | * Event id (can be null if only event in the stream). |
| 58 | */ |
| 59 | private Long id = UNSET_EVENT_ID; |
| 60 | |
| 61 | /** |
| 62 | * Stream to which belongs this event. |
| 63 | */ |
| 64 | private Stream stream = null; |
| 65 | |
| 66 | /** |
| 67 | * Loglevel of an event |
| 68 | */ |
| 69 | private long logLevel; |
| 70 | |
| 71 | /** Map of this event type's custom CTF attributes */ |
| 72 | private final Map<String, String> customAttributes = new HashMap<String, String>(); |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | // Constructors |
| 76 | // ------------------------------------------------------------------------ |
| 77 | |
| 78 | /** |
| 79 | * Default constructor. Use the setters afterwards to set the fields |
| 80 | * accordingly. |
| 81 | */ |
| 82 | public EventDeclaration() { |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public EventDefinition createDefinition(StreamInputReader streamInputReader) { |
| 87 | EventDefinition event = new EventDefinition(this, streamInputReader); |
| 88 | |
| 89 | if (context != null) { |
| 90 | event.setContext(context.createDefinition(event, "context")); //$NON-NLS-1$ |
| 91 | } |
| 92 | |
| 93 | if (this.fields != null) { |
| 94 | event.setFields(this.fields.createDefinition(event, "fields")); //$NON-NLS-1$ |
| 95 | } |
| 96 | |
| 97 | return event; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a "lost" event. This is a synthetic event that is there to show |
| 102 | * that there should be something there. |
| 103 | * |
| 104 | * @return the lost event |
| 105 | */ |
| 106 | public static synchronized EventDeclaration getLostEventDeclaration() { |
| 107 | EventDeclaration lostEvent = new EventDeclaration(); |
| 108 | lostEvent.fields = new StructDeclaration(1); |
| 109 | lostEvent.id = LOST_EVENT_ID; |
| 110 | lostEvent.name = "Lost event"; //$NON-NLS-1$ |
| 111 | return lostEvent; |
| 112 | } |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
| 115 | // Getters/Setters/Predicates |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
| 118 | /** |
| 119 | * Sets a name for an event Declaration |
| 120 | * |
| 121 | * @param name |
| 122 | * the name |
| 123 | */ |
| 124 | public void setName(String name) { |
| 125 | this.name = name; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String getName() { |
| 130 | return name; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sets the context for an event declaration (see CTF specification) |
| 135 | * |
| 136 | * @param context |
| 137 | * the context in structdeclaration format |
| 138 | */ |
| 139 | public void setContext(StructDeclaration context) { |
| 140 | this.context = context; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Sets the fields of an event declaration |
| 145 | * |
| 146 | * @param fields |
| 147 | * the fields in structdeclaration format |
| 148 | */ |
| 149 | public void setFields(StructDeclaration fields) { |
| 150 | this.fields = fields; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public StructDeclaration getFields() { |
| 155 | return fields; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public StructDeclaration getContext() { |
| 160 | return context; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the id of am event declaration |
| 165 | * |
| 166 | * @param id |
| 167 | * the id |
| 168 | */ |
| 169 | public void setId(long id) { |
| 170 | this.id = id; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Long getId() { |
| 175 | return id; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sets the stream of am event declaration |
| 180 | * |
| 181 | * @param stream |
| 182 | * the stream |
| 183 | * @since 2.0 |
| 184 | */ |
| 185 | public void setStream(Stream stream) { |
| 186 | this.stream = stream; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public Stream getStream() { |
| 191 | return stream; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Is the name of the event declaration set |
| 196 | * |
| 197 | * @return is the name set? |
| 198 | */ |
| 199 | public boolean nameIsSet() { |
| 200 | return name != null; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Is the context set |
| 205 | * |
| 206 | * @return is the context set |
| 207 | */ |
| 208 | public boolean contextIsSet() { |
| 209 | return context != null; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Is a field set? |
| 214 | * |
| 215 | * @return Is the field set? |
| 216 | */ |
| 217 | public boolean fieldsIsSet() { |
| 218 | return fields != null; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Is the id set? |
| 223 | * |
| 224 | * @return is the id set? |
| 225 | */ |
| 226 | public boolean idIsSet() { |
| 227 | return (id != null && id != UNSET_EVENT_ID); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Is the stream set? |
| 232 | * |
| 233 | * @return is the stream set? |
| 234 | */ |
| 235 | public boolean streamIsSet() { |
| 236 | return stream != null; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public long getLogLevel() { |
| 241 | return logLevel; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets the log level |
| 246 | * |
| 247 | * @param level |
| 248 | * the log level |
| 249 | */ |
| 250 | public void setLogLevel(long level) { |
| 251 | logLevel = level; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public Set<String> getCustomAttributes() { |
| 256 | return customAttributes.keySet(); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public String getCustomAttribute(String key) { |
| 261 | return customAttributes.get(key); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets a custom attribute value. |
| 266 | * |
| 267 | * @param key |
| 268 | * the key of the attribute |
| 269 | * @param value |
| 270 | * the value of the attribute |
| 271 | * @since 2.0 |
| 272 | */ |
| 273 | public void setCustomAttribute(String key, String value) { |
| 274 | customAttributes.put(key, value); |
| 275 | } |
| 276 | |
| 277 | // ------------------------------------------------------------------------ |
| 278 | // Operations |
| 279 | // ------------------------------------------------------------------------ |
| 280 | |
| 281 | @Override |
| 282 | public boolean equals(Object obj) { |
| 283 | if (this == obj) { |
| 284 | return true; |
| 285 | } |
| 286 | if (obj == null) { |
| 287 | return false; |
| 288 | } |
| 289 | if (!(obj instanceof EventDeclaration)) { |
| 290 | return false; |
| 291 | } |
| 292 | EventDeclaration other = (EventDeclaration) obj; |
| 293 | if (context == null) { |
| 294 | if (other.context != null) { |
| 295 | return false; |
| 296 | } |
| 297 | } else if (!context.equals(other.context)) { |
| 298 | return false; |
| 299 | } |
| 300 | if (fields == null) { |
| 301 | if (other.fields != null) { |
| 302 | return false; |
| 303 | } |
| 304 | } else if (!fields.equals(other.fields)) { |
| 305 | return false; |
| 306 | } |
| 307 | if (id == null) { |
| 308 | if (other.id != null) { |
| 309 | return false; |
| 310 | } |
| 311 | } else if (!id.equals(other.id)) { |
| 312 | return false; |
| 313 | } |
| 314 | if (name == null) { |
| 315 | if (other.name != null) { |
| 316 | return false; |
| 317 | } |
| 318 | } else if (!name.equals(other.name)) { |
| 319 | return false; |
| 320 | } |
| 321 | if (stream == null) { |
| 322 | if (other.stream != null) { |
| 323 | return false; |
| 324 | } |
| 325 | } else if (!stream.equals(other.stream)) { |
| 326 | return false; |
| 327 | } |
| 328 | if (!customAttributes.equals(other.customAttributes)) { |
| 329 | return false; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public int hashCode() { |
| 336 | final int prime = 31; |
| 337 | int result = 1; |
| 338 | result = (prime * result) |
| 339 | + ((context == null) ? 0 : context.hashCode()); |
| 340 | result = (prime * result) + ((fields == null) ? 0 : fields.hashCode()); |
| 341 | result = (prime * result) + ((id == null) ? 0 : id.hashCode()); |
| 342 | result = (prime * result) + ((name == null) ? 0 : name.hashCode()); |
| 343 | result = (prime * result) + ((stream == null) ? 0 : stream.hashCode()); |
| 344 | result = (prime * result) + customAttributes.hashCode(); |
| 345 | return result; |
| 346 | } |
| 347 | |
| 348 | } |