1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.event
;
15 import java
.util
.HashMap
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventContent
;
20 * <b><u>LttngEventContent</u></b><p>
22 * Lttng specific implementation of the TmfEventContent.<p>
24 public class LttngEventContent
extends TmfEventContent
{
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
>();
30 * Default constructor.<p>
34 public LttngEventContent() {
39 * Constructor with parameters.<p>
41 * @param thisParent Parent event for this content.
43 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
45 public LttngEventContent(LttngEvent thisParent
) {
46 super(thisParent
, null);
50 * Constructor with parameters, with optional content.<p>
52 * @param thisParent Parent event for this content.
53 * @param thisContent Already parsed content.
55 * @see org.eclipse.linuxtools.lttng.event.LttngEvent
57 public LttngEventContent(LttngEvent thisParent
, HashMap
<String
, LttngEventField
> thisContent
) {
58 super(thisParent
, null);
60 fFieldsMap
= thisContent
;
64 * Copy Constructor.<p>
66 * @param oldContent Content to copy from
68 public LttngEventContent(LttngEventContent oldContent
) {
69 this((LttngEvent
)oldContent
.getEvent(), oldContent
.getRawContent() );
73 public LttngEvent
getEvent() {
74 return (LttngEvent
)fParentEvent
;
77 public void setEvent(LttngEvent newParent
) {
78 fParentEvent
= newParent
;
83 // These are not very useful, are they?
84 public LttngEventType
getType() {
85 return (LttngEventType
)fParentEvent
.getType();
86 // return (LttngEventType)fEventType;
88 public void setType(LttngEventType newType
) {
89 ((LttngEvent
)fParentEvent
).setType(newType
);
90 // fEventType = newType;
95 // Find a better way to ensure content is sane!!
96 public void emptyContent() {
101 // A bit weird to return the _currently_parsed fields (unlike all like getFields() )
102 // Should we keep this?
104 * Return currently parsed fields in an object array format.<p>
106 * @return Currently parsed fields.
109 public Object
[] getContent() {
110 Object
[] returnedContent
= fFieldsMap
.values().toArray( new Object
[fFieldsMap
.size()] );
112 return returnedContent
;
116 * Return currently parsed fields in the internal hashmap format.<p>
118 * @return Currently parsed fields.
120 public HashMap
<String
, LttngEventField
> getRawContent() {
124 // @SuppressWarnings("unchecked")
126 // public LttngEventField[] getFields() {
127 // LttngEventField tmpField = null;
130 // // SLOW! SLOW! SLOW! We should prevent the user to use this!!
131 // HashMap<String, Object> parsedContent = parseContent();
133 // String contentKey = null;
134 // Iterator<String> contentItr = parsedContent.keySet().iterator();
135 // while ( contentItr.hasNext() ) {
136 // contentKey = contentItr.next();
138 // tmpField = new LttngEventField(this, contentKey, parsedContent.get(contentKey));
139 // ((HashMap<String, LttngEventField>)fFields).put(contentKey, tmpField);
142 // return fFields.values().toArray(new LttngEventField[fFields.size()]);
146 * Parse all fields and return them as an array of LttngFields.<p>
148 * Note : This function is heavy and should only be called if all fields are really needed.
150 * @return All fields.
152 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
155 public LttngEventField
[] getFields() {
156 LttngEventField tmpField
= null;
158 LttngEventType tmpType
= (LttngEventType
)fParentEvent
.getType();
160 for ( int pos
=0; pos
<tmpType
.getNbFields(); pos
++ ) {
161 String name
= tmpType
.getLabel(pos
);
162 Object newValue
= ((LttngEvent
)getEvent()).convertEventTmfToJni().parseFieldByName(name
);
164 tmpField
= new LttngEventField(this, name
, newValue
);
165 fFieldsMap
.put(name
, tmpField
);
167 return fFieldsMap
.values().toArray(new LttngEventField
[fFieldsMap
.size()]);
171 * Parse a single field from its given position.<p>
173 * @return The parsed field or null.
175 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
178 public LttngEventField
getField(int position
) {
179 LttngEventField returnedField
= null;
180 String label
= fParentEvent
.getType().getLabel(position
);
182 if ( label
!= null ) {
183 returnedField
= this.getField(label
);
186 return returnedField
;
190 * Parse a single field from its given name.<p>
192 * @return The parsed field or null.
194 * @see @see org.eclipse.linuxtools.lttng.event.LttngEventField
197 public LttngEventField
getField(String name
) {
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
);
203 if ( returnedField
== null ) {
205 // Should we really make sure we didn't get null before creating/inserting a field?
206 Object newValue
= ((LttngEvent
)getEvent()).convertEventTmfToJni().parseFieldByName(name
);
208 if ( newValue
!= null ) {
209 returnedField
= new LttngEventField(this, name
, newValue
);
210 fFieldsMap
.put(name
, returnedField
);
214 return returnedField
;
218 // *** Is this even useful?
220 protected void parseContent() {
221 fFields
= getFields();
225 * toString() method to print the content
227 * Note : this function parse all fields and so is very heavy to use.
230 public String
toString() {
231 String returnedString
= "";
233 LttngEventField
[] allFields
= getFields();
235 for ( int pos
=0; pos
< allFields
.length
; pos
++) {
236 returnedString
+= allFields
[pos
].toString() + " ";
239 return returnedString
;