Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.lttng.event; | |
14 | ||
28b94d61 FC |
15 | import java.util.HashMap; |
16 | ||
5d10d135 | 17 | import org.eclipse.linuxtools.tmf.event.TmfEventContent; |
5d10d135 ASL |
18 | |
19 | /** | |
07d9e2ee FC |
20 | * <b><u>LttngEventContent</u></b><p> |
21 | * | |
22 | * Lttng specific implementation of the TmfEventContent.<p> | |
5d10d135 ASL |
23 | */ |
24 | public class LttngEventContent extends TmfEventContent { | |
25 | ||
28b94d61 FC |
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 | ||
5d10d135 | 29 | /** |
28b94d61 | 30 | * Default constructor.<p> |
07d9e2ee | 31 | * |
5d10d135 | 32 | * |
5d10d135 | 33 | */ |
28b94d61 FC |
34 | public LttngEventContent() { |
35 | super(null, null); | |
5d10d135 | 36 | } |
28b94d61 | 37 | |
5d10d135 | 38 | /** |
07d9e2ee | 39 | * Constructor with parameters.<p> |
5d10d135 | 40 | * |
28b94d61 FC |
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 | |
5d10d135 | 56 | */ |
28b94d61 FC |
57 | public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) { |
58 | super(thisParent, null); | |
3fbd810a | 59 | |
28b94d61 | 60 | fFieldsMap = thisContent; |
5d10d135 ASL |
61 | } |
62 | ||
3fbd810a | 63 | /** |
07d9e2ee | 64 | * Copy Constructor.<p> |
3fbd810a | 65 | * |
07d9e2ee | 66 | * @param oldContent Content to copy from |
3fbd810a FC |
67 | */ |
68 | public LttngEventContent(LttngEventContent oldContent) { | |
28b94d61 FC |
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; | |
3fbd810a FC |
91 | } |
92 | ||
28b94d61 FC |
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? | |
07d9e2ee | 103 | /** |
28b94d61 | 104 | * Return currently parsed fields in an object array format.<p> |
07d9e2ee | 105 | * |
28b94d61 | 106 | * @return Currently parsed fields. |
5d10d135 ASL |
107 | */ |
108 | @Override | |
28b94d61 FC |
109 | public Object[] getContent() { |
110 | Object[] returnedContent = fFieldsMap.values().toArray( new Object[fFieldsMap.size()] ); | |
5d10d135 | 111 | |
28b94d61 | 112 | return returnedContent; |
5d10d135 | 113 | } |
28b94d61 | 114 | |
07d9e2ee | 115 | /** |
28b94d61 | 116 | * Return currently parsed fields in the internal hashmap format.<p> |
07d9e2ee | 117 | * |
28b94d61 FC |
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> | |
07d9e2ee | 147 | * |
28b94d61 | 148 | * Note : This function is heavy and should only be called if all fields are really needed. |
07d9e2ee | 149 | * |
28b94d61 | 150 | * @return All fields. |
07d9e2ee | 151 | * |
28b94d61 | 152 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 ASL |
153 | */ |
154 | @Override | |
28b94d61 FC |
155 | public LttngEventField[] getFields() { |
156 | LttngEventField tmpField = null; | |
88144d4a | 157 | |
28b94d61 FC |
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); | |
07d9e2ee | 163 | |
28b94d61 FC |
164 | tmpField = new LttngEventField(this, name, newValue ); |
165 | fFieldsMap.put(name, tmpField); | |
5d10d135 | 166 | } |
28b94d61 | 167 | return fFieldsMap.values().toArray(new LttngEventField[fFieldsMap.size()]); |
5d10d135 ASL |
168 | } |
169 | ||
170 | /** | |
28b94d61 | 171 | * Parse a single field from its given position.<p> |
07d9e2ee | 172 | * |
28b94d61 | 173 | * @return The parsed field or null. |
07d9e2ee | 174 | * |
28b94d61 | 175 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 | 176 | */ |
28b94d61 FC |
177 | @Override |
178 | public LttngEventField getField(int position) { | |
179 | LttngEventField returnedField = null; | |
180 | String label = fParentEvent.getType().getLabel(position); | |
88144d4a | 181 | |
28b94d61 FC |
182 | if ( label != null ) { |
183 | returnedField = this.getField(label); | |
88144d4a | 184 | } |
28b94d61 FC |
185 | |
186 | return returnedField; | |
5d10d135 ASL |
187 | } |
188 | ||
189 | /** | |
28b94d61 | 190 | * Parse a single field from its given name.<p> |
07d9e2ee | 191 | * |
28b94d61 | 192 | * @return The parsed field or null. |
07d9e2ee | 193 | * |
28b94d61 | 194 | * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField |
5d10d135 | 195 | */ |
28b94d61 FC |
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); | |
5d10d135 | 202 | |
28b94d61 FC |
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); | |
07d9e2ee | 207 | |
28b94d61 FC |
208 | if ( newValue!= null ) { |
209 | returnedField = new LttngEventField(this, name, newValue); | |
210 | fFieldsMap.put(name, returnedField ); | |
07d9e2ee | 211 | } |
5d10d135 | 212 | } |
28b94d61 | 213 | |
5d10d135 ASL |
214 | return returnedField; |
215 | } | |
216 | ||
28b94d61 FC |
217 | // *** VERIFY *** |
218 | // *** Is this even useful? | |
219 | @Override | |
220 | protected void parseContent() { | |
221 | fFields = getFields(); | |
222 | } | |
223 | ||
5d10d135 | 224 | /** |
28b94d61 | 225 | * toString() method to print the content |
5d10d135 | 226 | * |
28b94d61 | 227 | * Note : this function parse all fields and so is very heavy to use. |
5d10d135 | 228 | */ |
6d848cce | 229 | @Override |
28b94d61 FC |
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 | ||
5d10d135 ASL |
241 | } |
242 | } |