1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
19 import java
.util
.Map
.Entry
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 * Factory for CtfTmfEvent's.
31 * This code was moved out of CtfTmfEvent to provide better separation between
32 * the parsing/instantiation of events, and the usual TMF API implementations.
34 * @author Alexandre Montplaisir
37 public final class CtfTmfEventFactory
{
40 * Don't let anyone instantiate this class.
42 private CtfTmfEventFactory() {}
45 * Factory method to instantiate new {@link CtfTmfEvent}'s.
48 * CTF EventDefinition object corresponding to this trace event
50 * The path to the trace file
52 * The trace from which this event originates
53 * @return The newly-built CtfTmfEvent
55 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
56 String fileName
, CtfTmfTrace originTrace
) {
58 /* Prepare what to pass to CtfTmfEvent's constructor */
59 long ts
= eventDef
.getTimestamp();
60 CtfTmfTimestamp timestamp
= new CtfTmfTimestamp(
61 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
));
63 int sourceCPU
= eventDef
.getCPU();
65 ITmfEventField content
= new TmfEventField(
66 ITmfEventField
.ROOT_FIELD_ID
, parseFields(eventDef
));
68 String reference
= fileName
== null ? CtfTmfEvent
.NO_STREAM
: fileName
;
70 /* Construct and return the object */
71 CtfTmfEvent event
= new CtfTmfEvent(
73 ITmfContext
.UNKNOWN_RANK
,
78 eventDef
.getDeclaration()
83 /* Singleton instance of a null event */
84 private static CtfTmfEvent nullEvent
= null;
87 * Get an instance of a null event.
89 * @return An empty event
91 public static CtfTmfEvent
getNullEvent() {
92 if (nullEvent
== null) {
93 nullEvent
= new CtfTmfEvent();
99 * Extract the field information from the structDefinition haze-inducing
100 * mess, and put them into something ITmfEventField can cope with.
102 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
103 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
105 StructDefinition structFields
= eventDef
.getFields();
106 Map
<String
, Definition
> definitions
= structFields
.getDefinitions();
107 String curFieldName
= null;
108 Definition curFieldDef
;
109 CtfTmfEventField curField
;
110 Iterator
<Entry
<String
, Definition
>> it
= definitions
.entrySet().iterator();
111 while(it
.hasNext()) {
112 Entry
<String
, Definition
> entry
= it
.next();
113 curFieldName
= entry
.getKey();
114 curFieldDef
= entry
.getValue();
115 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
116 fields
.add(curField
);
119 /* Add context information as CtfTmfEventField */
120 StructDefinition structContext
= eventDef
.getContext();
121 if (structContext
!= null) {
122 definitions
= structContext
.getDefinitions();
123 String curContextName
;
124 Definition curContextDef
;
125 CtfTmfEventField curContext
;
126 it
= definitions
.entrySet().iterator();
127 while(it
.hasNext()) {
128 Entry
<String
, Definition
> entry
= it
.next();
129 /* Prefix field name */
130 curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ entry
.getKey();
131 curContextDef
= entry
.getValue();
132 curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
133 fields
.add(curContext
);
136 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);