* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation
+ * Francois Chouinard - Initial API and implementation
*******************************************************************************/
package org.eclipse.linuxtools.tmf.event;
* <p>
* The event content.
*/
-public class TmfEventContent {
+public class TmfEventContent implements Cloneable {
- // ========================================================================
+ // Default field ids
+ public static final String FIELD_ID_TIMESTAMP = ":timestamp:"; //$NON-NLS-1$
+ public static final String FIELD_ID_SOURCE = ":source:"; //$NON-NLS-1$
+ public static final String FIELD_ID_TYPE = ":type:"; //$NON-NLS-1$
+ public static final String FIELD_ID_REFERENCE = ":reference:"; //$NON-NLS-1$
+ public static final String FIELD_ID_CONTENT = ":content:"; //$NON-NLS-1$
+
+ // ------------------------------------------------------------------------
// Attributes
- // ========================================================================
+ // ------------------------------------------------------------------------
- private final TmfEventFormat fFormat;
- private final String fContent;
- private final int fNbFields;
- private TmfEventField[] fFields = null;
+ protected TmfEvent fParentEvent;
+ protected Object fRawContent;
+ protected Object[] fFields;
- // ========================================================================
+ // ------------------------------------------------------------------------
// Constructors
- // ========================================================================
+ // ------------------------------------------------------------------------
/**
- * @param content
- * @param format
+ * @param parent the parent event (owner)
+ * @param content the raw content
*/
- public TmfEventContent(Object content, TmfEventFormat format) {
- fFormat = format;
- fContent = content.toString();
- fNbFields = fFormat.getLabels().length;
+ public TmfEventContent(TmfEvent parent, Object content) {
+ fParentEvent = parent;
+ fRawContent = content;
}
- // ========================================================================
+ /**
+ * @param other the original event content
+ */
+ public TmfEventContent(TmfEventContent other) {
+ if (other == null)
+ throw new IllegalArgumentException();
+ fParentEvent = other.fParentEvent;
+ fRawContent = other.fRawContent;
+ fFields = other.fFields;
+ }
+
+ @SuppressWarnings("unused")
+ private TmfEventContent() {
+ throw new AssertionError();
+ }
+
+ // ------------------------------------------------------------------------
// Accessors
- // ========================================================================
+ // ------------------------------------------------------------------------
/**
- * @return
+ * @return the parent (containing) event
*/
- public String getContent() {
- return fContent;
+ public TmfEvent getEvent() {
+ return fParentEvent;
}
/**
- * @return
+ * @return the event type
*/
- public TmfEventFormat getFormat() {
- return fFormat;
+ public TmfEventType getType() {
+ return fParentEvent.getType();
}
- /**
- * @return
- */
- public int getNbFields() {
- return fNbFields;
- }
+ /**
+ * @return the raw content
+ */
+ public Object getContent() {
+ return fRawContent;
+ }
/**
- * @return
+ * Returns the list of fields in the same order as TmfEventType.getLabels()
+ *
+ * @return the ordered set of fields (optional fields might be null)
*/
- public TmfEventField[] getFields() {
- if (fFields == null) {
- fFields = fFormat.parse(fContent);
- }
+ public Object[] getFields() {
+ if (fFields == null) {
+ parseContent();
+ }
return fFields;
}
/**
- * @param id
- * @return
+ * @param id the field id
+ * @return the corresponding field
+ * @throws TmfNoSuchFieldException
*/
- public TmfEventField getField(int id) {
- assert id >= 0 && id < fNbFields;
+ public Object getField(String id) throws TmfNoSuchFieldException {
if (fFields == null) {
- fFields = fFormat.parse(fContent);
+ parseContent();
+ }
+ try {
+ return fFields[getType().getFieldIndex(id)];
+ } catch (TmfNoSuchFieldException e) {
+ // Required for filtering from default TmfEventsTable columns
+ if (id.equals(FIELD_ID_CONTENT)) {
+ return fParentEvent.getContent().toString();
+ } else if (id.equals(FIELD_ID_TIMESTAMP)) {
+ return new Long(fParentEvent.getTimestamp().getValue()).toString();
+ } else if (id.equals(FIELD_ID_SOURCE)) {
+ return fParentEvent.getSource().getSourceId().toString();
+ } else if (id.equals(FIELD_ID_TYPE)) {
+ return fParentEvent.getType().getTypeId().toString();
+ } else if (id.equals(FIELD_ID_REFERENCE)) {
+ return fParentEvent.getReference().getReference().toString();
+ }
+ throw e;
}
- return fFields[id];
+ }
+
+ /**
+ * @param n the field index as per TmfEventType.getLabels()
+ * @return the corresponding field (null if non-existing)
+ */
+ public Object getField(int n) {
+ if (fFields == null) {
+ parseContent();
+ }
+ if (n >= 0 && n < fFields.length)
+ return fFields[n];
+
+ return null;
}
+ // ------------------------------------------------------------------------
+ // Operators
+ // ------------------------------------------------------------------------
+
+ /**
+ * @param event
+ */
+ public void setEvent(TmfEvent event) {
+ fParentEvent = event;
+ }
+
+ /**
+ * Parse the content into fields. By default, a single field (the raw
+ * content) is returned.
+ * Should be overridden.
+ */
+ protected void parseContent() {
+ fFields = new Object[1];
+ fFields[0] = fRawContent;
+ }
+
+ // ------------------------------------------------------------------------
+ // Object
+ // ------------------------------------------------------------------------
+
+ @Override
+ public int hashCode() {
+ int result = 17;
+ result = 37 * result + ((fParentEvent != null) ? fParentEvent.hashCode() : 0);
+ result = 37 * result + ((fRawContent != null) ? fRawContent.hashCode() : 0);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof TmfEventContent))
+ return false;
+ TmfEventContent o = (TmfEventContent) other;
+ return fRawContent.equals(o.fRawContent);
+ }
+
+ @Override
+ @SuppressWarnings("nls")
+ public String toString() {
+ Object[] fields = getFields();
+ StringBuilder result = new StringBuilder("[TmfEventContent(");
+ for (int i = 0; i < fields.length; i++) {
+ if (i > 0) result.append(",");
+ result.append(fields[i]);
+ }
+ result.append(")]");
+ return result.toString();
+ }
+
+ @Override
+ public TmfEventContent clone() {
+ TmfEventContent clone = null;
+ try {
+ clone = (TmfEventContent) super.clone();
+ clone.fParentEvent = fParentEvent;
+ clone.fRawContent = fRawContent;
+ clone.fFields = fFields;
+ }
+ catch (CloneNotSupportedException e) {
+ e.printStackTrace();
+ }
+ return clone;
+ }
}