Fix some core.event API stuff
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core / src / org / eclipse / linuxtools / lttng / core / 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.core.event;
14
15 import java.util.HashMap;
16
17 import org.eclipse.linuxtools.lttng.jni.JniEvent;
18 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
19 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
20 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
21
22 /**
23 * <b><u>LttngEventContent</u></b><p>
24 *
25 * Lttng specific implementation of the TmfEventContent.<p>
26 */
27 public class LttngEventContent extends TmfEventField {
28
29 private LttngEvent fParentEvent;
30
31 // Hash map that contain the (parsed) fields. This is the actual payload of the event.
32 private HashMap<String, LttngEventField> fFieldsMap = new HashMap<String, LttngEventField>();
33
34 /**
35 * Default constructor.<p>
36 *
37 */
38 public LttngEventContent() {
39 super(ITmfEventField.ROOT_FIELD_ID, null);
40 }
41
42 /**
43 * Constructor with parameters.<p>
44 *
45 * @param thisParent Parent event for this content.
46 *
47 * @see org.eclipse.linuxtools.lttng.core.event.LttngEvent
48 */
49 public LttngEventContent(LttngEvent thisParent) {
50 super(ITmfEventField.ROOT_FIELD_ID, null);
51 fParentEvent = thisParent;
52 }
53
54 /**
55 * Constructor with parameters, with optional content.<p>
56 *
57 * @param thisParent Parent event for this content.
58 * @param thisContent Already parsed content.
59 *
60 * @see org.eclipse.linuxtools.lttng.core.event.LttngEvent
61 */
62 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) {
63 super(ITmfEventField.ROOT_FIELD_ID, null);
64 fParentEvent = thisParent;
65 fFieldsMap = thisContent;
66 }
67
68 /**
69 * Copy Constructor.<p>
70 *
71 * @param oldContent Content to copy from
72 */
73 public LttngEventContent(LttngEventContent oldContent) {
74 this((LttngEvent) oldContent.getEvent(), oldContent.getMapContent());
75 }
76
77 public LttngEvent getEvent() {
78 return fParentEvent;
79 }
80
81 public void setEvent(LttngEvent newParent) {
82 fParentEvent = newParent;
83 }
84
85
86 // *** VERIFY ***
87 // These are not very useful, are they?
88
89 // public LttngEventType getType() {
90 // return (LttngEventType)fParentEvent.getType();
91 // }
92
93 // public void setType(LttngEventType newType) {
94 // ((LttngEvent)fParentEvent).setType(newType);
95 // }
96
97
98 // ***TODO***
99 // Find a better way to ensure content is sane!!
100 public void emptyContent() {
101 fFieldsMap.clear();
102 }
103
104 // ***VERIFY***
105 // A bit weird to return the _currently_parsed fields (unlike all fields like getFields() )
106 // Should we keep this?
107 /**
108 * Return currently parsed fields in an object array format.<p>
109 *
110 * @return Currently parsed fields.
111 */
112 public Object[] getRawContent() {
113 Object[] returnedContent = fFieldsMap.values().toArray(new Object[fFieldsMap.size()]);
114 return returnedContent;
115 }
116
117 /**
118 * Return currently parsed fields in the internal hashmap format.<p>
119 *
120 * @return Currently parsed fields.
121 */
122 public HashMap<String, LttngEventField> getMapContent() {
123 return fFieldsMap;
124 }
125
126 // @SuppressWarnings("unchecked")
127 // @Override
128 // public LttngEventField[] getFields() {
129 // LttngEventField tmpField = null;
130 //
131 // // *** TODO ***
132 // // SLOW! SLOW! SLOW! We should prevent the user to use this!!
133 // HashMap<String, Object> parsedContent = parseContent();
134 //
135 // String contentKey = null;
136 // Iterator<String> contentItr = parsedContent.keySet().iterator();
137 // while ( contentItr.hasNext() ) {
138 // contentKey = contentItr.next();
139 //
140 // tmpField = new LttngEventField(this, contentKey, parsedContent.get(contentKey));
141 // ((HashMap<String, LttngEventField>)fFields).put(contentKey, tmpField);
142 // }
143 //
144 // return fFields.values().toArray(new LttngEventField[fFields.size()]);
145 // }
146
147 /**
148 * Parse all fields and return them as an array of LttngFields.<p>
149 *
150 * Note : This function is heavy and should only be called if all fields are really needed.
151 *
152 * @return All fields.
153 *
154 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
155 */
156 @Override
157 public synchronized LttngEventField[] getFields() {
158 int nbFields = fParentEvent.getType().getFieldNames().length;
159
160 if (fFieldsMap.size() < nbFields) {
161 LttngEventField tmpField = null;
162 LttngEventType tmpType = (LttngEventType)fParentEvent.getType();
163
164 for (int pos=0; pos < nbFields; pos++) {
165 String name = null;
166 LttngEvent lttngTmpEvent = (LttngEvent)getEvent(); //added for easier debugging
167 JniEvent tmpEvent = (lttngTmpEvent).convertEventTmfToJni();
168
169 // tmpEvent == null probably mean there is a discrepancy between Eclipse and C library
170 // An error was probably printed in convertEventTmfToJni() already, but keep in mind this is SERIOUS
171 if ( tmpEvent != null ) {
172 // try {
173 name = tmpType.getFieldName(pos);
174
175 Object newValue = tmpEvent.parseFieldByName(name);
176 tmpField = new LttngEventField(name, newValue, null);
177 fFieldsMap.put(name, tmpField);
178 // }
179 // catch (TmfNoSuchFieldException e) {
180 // System.out.println("Invalid field position requested : " + pos + ", ignoring (getFields)."); //$NON-NLS-1$//$NON-NLS-2$
181 // }
182 }
183 }
184 }
185 return fFieldsMap.values().toArray(new LttngEventField[fFieldsMap.size()]);
186 }
187
188 /**
189 * Parse a single field from its given position.<p>
190 *
191 * @return The parsed field or null.
192 *
193 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
194 */
195 @Override
196 public LttngEventField getField(int position) {
197 LttngEventField returnedField = null;
198 String label = null;
199 // try {
200 label = fParentEvent.getType().getFieldName(position);
201 returnedField = (LttngEventField) this.getField(label);
202 // }
203 // catch (TmfNoSuchFieldException e) {
204 // System.out.println("Invalid field position requested : " + position + ", ignoring (getField)."); //$NON-NLS-1$//$NON-NLS-2$
205 // }
206
207 return returnedField;
208 }
209
210 /**
211 * Parse a single field from its given name.<p>
212 *
213 * @return The parsed field or null.
214 *
215 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
216 */
217 @Override
218 public synchronized LttngEventField getField(String name) {
219
220 // Check for generic table header fields
221 if (name.equals(LttngEventType.CONTENT_LABEL) || name.equals(ITmfEvent.EVENT_FIELD_CONTENT)) {
222 return new LttngEventField(toString());
223 } else if (name.equals(LttngEventType.MARKER_LABEL) || name.equals(ITmfEvent.EVENT_FIELD_TYPE)) {
224 return new LttngEventField(fParentEvent.getType().getName());
225 } else if (name.equals(LttngEventType.TRACE_LABEL) || name.equals(ITmfEvent.EVENT_FIELD_REFERENCE)) {
226 return new LttngEventField(fParentEvent.getReference());
227 } else if (name.equals(LttngEventType.TIMESTAMP_LABEL) || name.equals(ITmfEvent.EVENT_FIELD_TIMESTAMP)) {
228 return new LttngEventField(fParentEvent.getTimestamp().toString());
229 } else if (name.equals(ITmfEvent.EVENT_FIELD_SOURCE)) {
230 return new LttngEventField(fParentEvent.getSource());
231 }
232
233 // *** VERIFY ***
234 // Should we check if the field exists in LttngType before parsing?
235 // It could avoid calling parse for non-existent fields but would waste some cpu cycle on check?
236 LttngEventField returnedField = fFieldsMap.get(name);
237
238 if ( returnedField == null ) {
239 // *** VERIFY ***
240 // Should we really make sure we didn't get null before creating/inserting a field?
241 JniEvent tmpEvent = ((LttngEvent)getEvent()).convertEventTmfToJni();
242
243 if ( tmpEvent != null) {
244 Object newValue = tmpEvent.parseFieldByName(name);
245
246 if ( newValue!= null ) {
247 returnedField = new LttngEventField(name, newValue);
248 returnedField = new LttngEventField(name, newValue);
249 fFieldsMap.put(name, returnedField );
250 }
251 }
252 }
253
254 return returnedField;
255 }
256
257 // // *** VERIFY ***
258 // // *** Is this even useful?
259 // protected void parseContent() {
260 // fSubfields = getFields();
261 // }
262
263 /**
264 * toString() method to print the content
265 *
266 * Note : this function parse all fields and so is very heavy to use.
267 */
268 @Override
269 @SuppressWarnings("nls")
270 public String toString() {
271 LttngEventField[] allFields = getFields();
272
273 StringBuffer strBuffer = new StringBuffer();
274 for ( int pos=0; pos < allFields.length; pos++) {
275 if (pos != 0) strBuffer.append(",");
276 strBuffer.append(allFields[pos].toString());
277 }
278
279 return strBuffer.toString();
280 }
281
282 @Override
283 public LttngEventContent clone() {
284 LttngEventContent clone = (LttngEventContent) super.clone();
285 LttngEventField[] fields = getFields();
286 LttngEventField[] subfields = new LttngEventField[fields.length];
287 for (int i = 0; i < fields.length; i++) {
288 subfields[i] = (LttngEventField) fields[i].clone();
289 }
290 clone.setValue(getValue(), subfields);
291 clone.fFieldsMap = new HashMap<String, LttngEventField>();
292 for (String key : fFieldsMap.keySet()) {
293 clone.fFieldsMap.put(new String(key), ((LttngEventField) fFieldsMap.get(key)).clone());
294 }
295 return clone;
296 }
297
298 }
This page took 0.039079 seconds and 5 git commands to generate.