Monster fix: TMF model update + corresponding LTTng adaptations + JUnits
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / event / LttngEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng.event;
14
15 import java.util.HashMap;
16
17 import org.eclipse.linuxtools.tmf.event.TmfEventContent;
18
19 /**
20 * <b><u>LttngEventContent</u></b><p>
21 *
22 * Lttng specific implementation of the TmfEventContent.<p>
23 */
24 public class LttngEventContent extends TmfEventContent {
25
26 // Hash map that contain the (parsed) fields. This is the actual payload of the event.
27 HashMap<String, LttngEventField> fFieldsMap = new HashMap<String, LttngEventField>();
28
29 /**
30 * Default constructor.<p>
31 *
32 *
33 */
34 public LttngEventContent() {
35 super(null, null);
36 }
37
38 /**
39 * Constructor with parameters.<p>
40 *
41 * @param thisParent Parent event for this content.
42 *
43 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
44 */
45 public LttngEventContent(LttngEvent thisParent) {
46 super(thisParent, null);
47 }
48
49 /**
50 * Constructor with parameters, with optional content.<p>
51 *
52 * @param thisParent Parent event for this content.
53 * @param thisContent Already parsed content.
54 *
55 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
56 */
57 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) {
58 super(thisParent, null);
59
60 fFieldsMap = thisContent;
61 }
62
63 /**
64 * Copy Constructor.<p>
65 *
66 * @param oldContent Content to copy from
67 */
68 public LttngEventContent(LttngEventContent oldContent) {
69 this((LttngEvent)oldContent.getEvent(), oldContent.getRawContent() );
70 }
71
72
73 public LttngEvent getEvent() {
74 return (LttngEvent)fParentEvent;
75 }
76
77 public void setEvent(LttngEvent newParent) {
78 fParentEvent = newParent;
79 }
80
81
82 // *** VERIFY ***
83 // These are not very useful, are they?
84 public LttngEventType getType() {
85 return (LttngEventType)fParentEvent.getType();
86 // return (LttngEventType)fEventType;
87 }
88 public void setType(LttngEventType newType) {
89 ((LttngEvent)fParentEvent).setType(newType);
90 // fEventType = newType;
91 }
92
93
94 // ***TODO***
95 // Find a better way to ensure content is sane!!
96 public void emptyContent() {
97 fFieldsMap.clear();
98 }
99
100 // ***VERIFY***
101 // A bit weird to return the _currently_parsed fields (unlike all like getFields() )
102 // Should we keep this?
103 /**
104 * Return currently parsed fields in an object array format.<p>
105 *
106 * @return Currently parsed fields.
107 */
108 @Override
109 public Object[] getContent() {
110 Object[] returnedContent = fFieldsMap.values().toArray( new Object[fFieldsMap.size()] );
111
112 return returnedContent;
113 }
114
115 /**
116 * Return currently parsed fields in the internal hashmap format.<p>
117 *
118 * @return Currently parsed fields.
119 */
120 public HashMap<String, LttngEventField> getRawContent() {
121 return fFieldsMap;
122 }
123
124 // @SuppressWarnings("unchecked")
125 // @Override
126 // public LttngEventField[] getFields() {
127 // LttngEventField tmpField = null;
128 //
129 // // *** TODO ***
130 // // SLOW! SLOW! SLOW! We should prevent the user to use this!!
131 // HashMap<String, Object> parsedContent = parseContent();
132 //
133 // String contentKey = null;
134 // Iterator<String> contentItr = parsedContent.keySet().iterator();
135 // while ( contentItr.hasNext() ) {
136 // contentKey = contentItr.next();
137 //
138 // tmpField = new LttngEventField(this, contentKey, parsedContent.get(contentKey));
139 // ((HashMap<String, LttngEventField>)fFields).put(contentKey, tmpField);
140 // }
141 //
142 // return fFields.values().toArray(new LttngEventField[fFields.size()]);
143 // }
144
145 /**
146 * Parse all fields and return them as an array of LttngFields.<p>
147 *
148 * Note : This function is heavy and should only be called if all fields are really needed.
149 *
150 * @return All fields.
151 *
152 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
153 */
154 @Override
155 public LttngEventField[] getFields() {
156 LttngEventField tmpField = null;
157
158 LttngEventType tmpType = (LttngEventType)fParentEvent.getType();
159
160 for ( int pos=0; pos<tmpType.getNbFields(); pos++ ) {
161 String name = tmpType.getLabel(pos);
162 Object newValue = ((LttngEvent)getEvent()).convertEventTmfToJni().parseFieldByName(name);
163
164 tmpField = new LttngEventField(this, name, newValue );
165 fFieldsMap.put(name, tmpField);
166 }
167 return fFieldsMap.values().toArray(new LttngEventField[fFieldsMap.size()]);
168 }
169
170 /**
171 * Parse a single field from its given position.<p>
172 *
173 * @return The parsed field or null.
174 *
175 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
176 */
177 @Override
178 public LttngEventField getField(int position) {
179 LttngEventField returnedField = null;
180 String label = fParentEvent.getType().getLabel(position);
181
182 if ( label != null ) {
183 returnedField = this.getField(label);
184 }
185
186 return returnedField;
187 }
188
189 /**
190 * Parse a single field from its given name.<p>
191 *
192 * @return The parsed field or null.
193 *
194 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
195 */
196 @Override
197 public LttngEventField getField(String name) {
198 // *** VERIFY ***
199 // Should we check if the field exists in LttngType before parsing?
200 // It could avoid calling parse for non-existent fields but would waste some cpu cycle on check?
201 LttngEventField returnedField = fFieldsMap.get(name);
202
203 if ( returnedField == null ) {
204 // *** VERIFY ***
205 // Should we really make sure we didn't get null before creating/inserting a field?
206 Object newValue = ((LttngEvent)getEvent()).convertEventTmfToJni().parseFieldByName(name);
207
208 if ( newValue!= null ) {
209 returnedField = new LttngEventField(this, name, newValue);
210 fFieldsMap.put(name, returnedField );
211 }
212 }
213
214 return returnedField;
215 }
216
217 // *** VERIFY ***
218 // *** Is this even useful?
219 @Override
220 protected void parseContent() {
221 fFields = getFields();
222 }
223
224 /**
225 * toString() method to print the content
226 *
227 * Note : this function parse all fields and so is very heavy to use.
228 */
229 @Override
230 public String toString() {
231 String returnedString = "";
232
233 LttngEventField[] allFields = getFields();
234
235 for ( int pos=0; pos < allFields.length; pos++) {
236 returnedString += allFields[pos].toString() + " ";
237 }
238
239 return returnedString;
240
241 }
242 }
This page took 0.067481 seconds and 5 git commands to generate.