temporary re-factoring project
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / event / LttngEventContent.java
CommitLineData
5d10d135
ASL
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
13package org.eclipse.linuxtools.lttng.event;
14
15import java.util.HashMap;
16
5d10d135 17import org.eclipse.linuxtools.tmf.event.TmfEventContent;
5d10d135
ASL
18
19/**
20 * <b><u>LttngEventContent</u></b><p>
21 *
22 * Lttng specific implementation of the TmfEventContent.<p>
23 */
24public 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 @Override
74 public LttngEvent getEvent() {
75 return (LttngEvent)fParentEvent;
76 }
77
78 public void setEvent(LttngEvent newParent) {
79 fParentEvent = newParent;
80 }
81
82
83 // *** VERIFY ***
84 // These are not very useful, are they?
85 @Override
86 public LttngEventType getType() {
87 return (LttngEventType)fParentEvent.getType();
88144d4a 88// return (LttngEventType)fEventType;
5d10d135
ASL
89 }
90 public void setType(LttngEventType newType) {
91 ((LttngEvent)fParentEvent).setType(newType);
88144d4a 92// fEventType = newType;
5d10d135
ASL
93 }
94
95
96 // ***TODO***
97 // Find a better way to ensure content is sane!!
98 public void emptyContent() {
99 fFieldsMap.clear();
100 }
101
102 // ***VERIFY***
88144d4a 103 // A bit weird to return the _currently_parsed fields (unlike all like getFields() )
5d10d135
ASL
104 // Should we keep this?
105 /**
106 * Return currently parsed fields in an object array format.<p>
107 *
108 * @return Currently parsed fields.
109 */
110 @Override
111 public Object[] getContent() {
112 Object[] returnedContent = fFieldsMap.values().toArray( new Object[fFieldsMap.size()] );
113
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> getRawContent() {
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
88144d4a
ASL
157 public LttngEventField[] getFields() {
158 LttngEventField tmpField = null;
159
160 LttngEventType tmpType = (LttngEventType)fParentEvent.getType();
161
162 for ( int pos=0; pos<tmpType.getNbFields(); pos++ ) {
163 String name = tmpType.getLabel(pos);
164 Object newValue = ((LttngEvent)getEvent()).convertEventTmfToJni().parseFieldByName(name);
165
166 tmpField = new LttngEventField(this, name, newValue );
167 fFieldsMap.put(name, tmpField);
5d10d135
ASL
168 }
169 return fFieldsMap.values().toArray(new LttngEventField[fFieldsMap.size()]);
170 }
171
172 /**
173 * Parse a single field from its given position.<p>
174 *
175 * @return The parsed field or null.
176 *
177 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
178 */
179 @Override
180 public LttngEventField getField(int position) {
181 LttngEventField returnedField = null;
88144d4a
ASL
182 String label = fParentEvent.getType().getLabel(position);
183
184 if ( label != null ) {
185 returnedField = this.getField(label);
186 }
5d10d135
ASL
187
188 return returnedField;
189 }
190
191 /**
192 * Parse a single field from its given name.<p>
193 *
194 * @return The parsed field or null.
195 *
196 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
197 */
198 @Override
88144d4a
ASL
199 public LttngEventField getField(String name) {
200 // *** VERIFY ***
5d10d135
ASL
201 // Should we check if the field exists in LttngType before parsing?
202 // It could avoid calling parse for non-existent fields but would waste some cpu cycle on check?
203 LttngEventField returnedField = fFieldsMap.get(name);
204
205 if ( returnedField == null ) {
206 // *** VERIFY ***
207 // Should we really make sure we didn't get null before creating/inserting a field?
88144d4a
ASL
208 Object newValue = ((LttngEvent)getEvent()).convertEventTmfToJni().parseFieldByName(name);
209
210 if ( newValue!= null ) {
211 returnedField = new LttngEventField(this, name, newValue);
212 fFieldsMap.put(name, returnedField );
213 }
5d10d135
ASL
214 }
215
216 return returnedField;
217 }
218
219 // *** VERIFY ***
220 // *** Is this even useful?
221 @Override
222 protected void parseContent() {
223 fFields = getFields();
224 }
225
226 /**
227 * toString() method to print the content
228 *
229 * Note : this function parse all fields and so is very heavy to use.
230 */
231 @Override
232 public String toString() {
88144d4a
ASL
233 String returnedString = "";
234
5d10d135
ASL
235 LttngEventField[] allFields = getFields();
236
5d10d135 237 for ( int pos=0; pos < allFields.length; pos++) {
88144d4a 238 returnedString += allFields[pos].toString() + " ";
5d10d135
ASL
239 }
240
88144d4a 241 return returnedString;
5d10d135
ASL
242
243 }
244}
This page took 0.035006 seconds and 5 git commands to generate.