tmf: Streamline CtfTmfEventField creation
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventFactory.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
20 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
21 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
24 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
25
26 /**
27 * Factory for CtfTmfEvent's.
28 *
29 * This code was moved out of CtfTmfEvent to provide better separation between
30 * the parsing/instantiation of events, and the usual TMF API implementations.
31 *
32 * @author Alexandre Montplaisir
33 * @since 2.0
34 */
35 public final class CtfTmfEventFactory {
36
37 /**
38 * Don't let anyone instantiate this class.
39 */
40 private CtfTmfEventFactory() {}
41
42 /**
43 * Factory method to instantiate new {@link CtfTmfEvent}'s.
44 *
45 * @param eventDef
46 * CTF EventDefinition object corresponding to this trace event
47 * @param fileName
48 * The path to the trace file
49 * @param originTrace
50 * The trace from which this event originates
51 * @return The newly-built CtfTmfEvent
52 */
53 public static CtfTmfEvent createEvent(EventDefinition eventDef,
54 String fileName, CtfTmfTrace originTrace) {
55
56 /* Prepare what to pass to CtfTmfEvent's constructor */
57 long ts = eventDef.getTimestamp();
58 CtfTmfTimestamp timestamp = new CtfTmfTimestamp(
59 originTrace.getCTFTrace().timestampCyclesToNanos(ts));
60
61 int sourceCPU = eventDef.getCPU();
62
63 ITmfEventField content = new TmfEventField(
64 ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef));
65
66 String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
67
68 /* Construct and return the object */
69 CtfTmfEvent event = new CtfTmfEvent(
70 originTrace,
71 ITmfContext.UNKNOWN_RANK,
72 timestamp,
73 content,
74 reference,
75 sourceCPU,
76 eventDef.getDeclaration()
77 );
78 return event;
79 }
80
81 /* Singleton instance of a null event */
82 private static CtfTmfEvent nullEvent = null;
83
84 /**
85 * Get an instance of a null event.
86 *
87 * @return An empty event
88 */
89 public static CtfTmfEvent getNullEvent() {
90 if (nullEvent == null) {
91 nullEvent = new CtfTmfEvent();
92 }
93 return nullEvent;
94 }
95
96 /**
97 * Extract the field information from the structDefinition haze-inducing
98 * mess, and put them into something ITmfEventField can cope with.
99 */
100 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
101 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
102
103 StructDefinition structFields = eventDef.getFields();
104 for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) {
105 String curFieldName = entry.getKey();
106 Definition curFieldDef = entry.getValue();
107 CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
108 fields.add(curField);
109 }
110
111 /* Add context information as CtfTmfEventField */
112 StructDefinition structContext = eventDef.getContext();
113 if (structContext != null) {
114 for (Map.Entry<String, Definition> entry : structContext.getDefinitions().entrySet()) {
115 /* Prefix field name */
116 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey();
117 Definition curContextDef = entry.getValue();
118 CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
119 fields.add(curContext);
120 }
121 }
122
123 return fields.toArray(new CtfTmfEventField[fields.size()]);
124 }
125 }
This page took 0.033287 seconds and 6 git commands to generate.