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
.List
;
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
;
27 * Factory for CtfTmfEvent's.
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.
32 * @author Alexandre Montplaisir
35 public final class CtfTmfEventFactory
{
38 * Don't let anyone instantiate this class.
40 private CtfTmfEventFactory() {}
43 * Factory method to instantiate new {@link CtfTmfEvent}'s.
46 * CTF EventDefinition object corresponding to this trace event
48 * The path to the trace file
50 * The trace from which this event originates
51 * @return The newly-built CtfTmfEvent
53 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
54 String fileName
, CtfTmfTrace originTrace
) {
56 /* Prepare what to pass to CtfTmfEvent's constructor */
57 long ts
= eventDef
.getTimestamp();
58 CtfTmfTimestamp timestamp
= new CtfTmfTimestamp(
59 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
));
61 int sourceCPU
= eventDef
.getCPU();
63 ITmfEventField content
= new TmfEventField(
64 ITmfEventField
.ROOT_FIELD_ID
, parseFields(eventDef
));
66 String reference
= fileName
== null ? CtfTmfEvent
.NO_STREAM
: fileName
;
68 /* Construct and return the object */
69 CtfTmfEvent event
= new CtfTmfEvent(
71 ITmfContext
.UNKNOWN_RANK
,
76 eventDef
.getDeclaration()
81 /* Singleton instance of a null event */
82 private static CtfTmfEvent nullEvent
= null;
85 * Get an instance of a null event.
87 * @return An empty event
89 public static CtfTmfEvent
getNullEvent() {
90 if (nullEvent
== null) {
91 nullEvent
= new CtfTmfEvent();
97 * Extract the field information from the structDefinition haze-inducing
98 * mess, and put them into something ITmfEventField can cope with.
100 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
101 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
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
);
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
);
123 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);