1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
16 import java
.util
.Arrays
;
19 * <b><u>TmfEventContent</u></b>
21 * A basic implementation of ITmfEventContent where the raw content is a String.
23 public class TmfEventContent
implements ITmfEventContent
{
25 // ------------------------------------------------------------------------
27 // ------------------------------------------------------------------------
30 public static final String FIELD_ID_TIMESTAMP
= ":timestamp:"; //$NON-NLS-1$
31 public static final String FIELD_ID_SOURCE
= ":source:"; //$NON-NLS-1$
32 public static final String FIELD_ID_TYPE
= ":type:"; //$NON-NLS-1$
33 public static final String FIELD_ID_REFERENCE
= ":reference:"; //$NON-NLS-1$
34 public static final String FIELD_ID_CONTENT
= ":content:"; //$NON-NLS-1$
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 protected TmfEvent fParentEvent
;
41 protected String fRawContent
;
42 protected TmfEventField
[] fFields
;
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
51 @SuppressWarnings("unused")
52 private TmfEventContent() {
53 throw new AssertionError();
59 * @param parent the parent event (owner)
60 * @param content the raw content as a byte[]
62 public TmfEventContent(TmfEvent parent
, String content
) {
63 fParentEvent
= parent
;
64 fRawContent
= content
;
70 * @param content the original event content
72 public TmfEventContent(TmfEventContent content
) {
74 throw new IllegalArgumentException();
75 fParentEvent
= content
.fParentEvent
;
76 fRawContent
= content
.fRawContent
;
77 fFields
= content
.fFields
;
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * @return the parent (containing) event
87 public TmfEvent
getEvent() {
92 * @return the event type
94 public ITmfEventType
getType() {
95 return fParentEvent
.getType();
99 * @return the raw content
101 public Object
getRawContent() {
106 * @return the serialized content
108 public String
getFmtContent() {
113 * Returns the list of fields in the same order as TmfEventType.getLabels()
115 * @return the ordered set of fields (optional fields might be null)
117 public ITmfEventField
[] getFields() {
118 if (fFields
== null) {
125 * @param id the field id
126 * @return the corresponding field
127 * @throws TmfNoSuchFieldException
129 public ITmfEventField
getField(String id
) throws TmfNoSuchFieldException
{
130 if (fFields
== null) {
134 return fFields
[getType().getFieldIndex(id
)];
135 } catch (TmfNoSuchFieldException e
) {
136 // Required for filtering from default TmfEventsTable columns
137 if (id
.equals(FIELD_ID_CONTENT
)) {
138 return new TmfEventField(this, FIELD_ID_CONTENT
, toString());
139 } else if (id
.equals(FIELD_ID_TIMESTAMP
)) {
140 return new TmfEventField(this, FIELD_ID_TIMESTAMP
, fParentEvent
.getTimestamp().toString());
141 } else if (id
.equals(FIELD_ID_SOURCE
)) {
142 return new TmfEventField(this, FIELD_ID_SOURCE
, fParentEvent
.getSource());
143 } else if (id
.equals(FIELD_ID_TYPE
)) {
144 return new TmfEventField(this, FIELD_ID_TYPE
, fParentEvent
.getType().getId());
145 } else if (id
.equals(FIELD_ID_REFERENCE
)) {
146 return new TmfEventField(this, FIELD_ID_REFERENCE
, fParentEvent
.getReference());
153 * @param n the field index as per TmfEventType.getLabels()
154 * @return the corresponding field (null if non-existing)
156 public ITmfEventField
getField(int n
) {
157 if (fFields
== null) {
160 if (n
>= 0 && n
< fFields
.length
)
166 // ------------------------------------------------------------------------
168 // ------------------------------------------------------------------------
171 * Parse the content into fields. By default, a single field (the raw
172 * content) is returned.
174 * Should be overridden.
176 protected void parseContent() {
177 fFields
= new TmfEventField
[1];
178 fFields
[0] = new TmfEventField(this, FIELD_ID_CONTENT
, fRawContent
);
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 public ITmfEventContent
clone() {
187 TmfEventContent clone
= null;
189 clone
= (TmfEventContent
) super.clone();
190 clone
.fParentEvent
= fParentEvent
;
191 clone
.fRawContent
= fRawContent
;
192 clone
.fFields
= fFields
;
194 catch (CloneNotSupportedException e
) {
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
205 public int hashCode() {
206 final int prime
= 31;
208 result
= prime
* result
+ Arrays
.hashCode(fFields
);
209 result
= prime
* result
+ ((fRawContent
== null) ?
0 : fRawContent
.hashCode());
214 public boolean equals(Object obj
) {
219 if (getClass() != obj
.getClass())
221 TmfEventContent other
= (TmfEventContent
) obj
;
222 if (!Arrays
.equals(fFields
, other
.fFields
))
224 if (fRawContent
== null) {
225 if (other
.fRawContent
!= null)
227 } else if (!fRawContent
.equals(other
.fRawContent
))
233 @SuppressWarnings("nls")
234 public String
toString() {
235 return "TmfEventContent [fRawContent=" + fRawContent
+ ", fFields=" + Arrays
.toString(fFields
) + "]";