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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
16 * <b><u>TmfEventContent</u></b>
20 public class TmfEventContent
implements Cloneable
{
23 public static final String FIELD_ID_TIMESTAMP
= ":timestamp:"; //$NON-NLS-1$
24 public static final String FIELD_ID_SOURCE
= ":source:"; //$NON-NLS-1$
25 public static final String FIELD_ID_TYPE
= ":type:"; //$NON-NLS-1$
26 public static final String FIELD_ID_REFERENCE
= ":reference:"; //$NON-NLS-1$
27 public static final String FIELD_ID_CONTENT
= ":content:"; //$NON-NLS-1$
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 protected TmfEvent fParentEvent
;
34 protected Object fRawContent
;
35 protected Object
[] fFields
;
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * @param parent the parent event (owner)
43 * @param content the raw content
45 public TmfEventContent(TmfEvent parent
, Object content
) {
46 fParentEvent
= parent
;
47 fRawContent
= content
;
51 * @param other the original event content
53 public TmfEventContent(TmfEventContent other
) {
55 throw new IllegalArgumentException();
56 fParentEvent
= other
.fParentEvent
;
57 fRawContent
= other
.fRawContent
;
58 fFields
= other
.fFields
;
61 @SuppressWarnings("unused")
62 private TmfEventContent() {
63 throw new AssertionError();
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * @return the parent (containing) event
73 public TmfEvent
getEvent() {
78 * @return the event type
80 public TmfEventType
getType() {
81 return fParentEvent
.getType();
85 * @return the raw content
87 public Object
getContent() {
92 * Returns the list of fields in the same order as TmfEventType.getLabels()
94 * @return the ordered set of fields (optional fields might be null)
96 public Object
[] getFields() {
97 if (fFields
== null) {
104 * @param id the field id
105 * @return the corresponding field
106 * @throws TmfNoSuchFieldException
108 public Object
getField(String id
) throws TmfNoSuchFieldException
{
109 if (fFields
== null) {
113 return fFields
[getType().getFieldIndex(id
)];
114 } catch (TmfNoSuchFieldException e
) {
115 // Required for filtering from default TmfEventsTable columns
116 if (id
.equals(FIELD_ID_CONTENT
)) {
117 return fParentEvent
.getContent().toString();
118 } else if (id
.equals(FIELD_ID_TIMESTAMP
)) {
119 return new Long(fParentEvent
.getTimestamp().getValue()).toString();
120 } else if (id
.equals(FIELD_ID_SOURCE
)) {
121 return fParentEvent
.getSource().getSourceId().toString();
122 } else if (id
.equals(FIELD_ID_TYPE
)) {
123 return fParentEvent
.getType().getTypeId().toString();
124 } else if (id
.equals(FIELD_ID_REFERENCE
)) {
125 return fParentEvent
.getReference().getReference().toString();
132 * @param n the field index as per TmfEventType.getLabels()
133 * @return the corresponding field (null if non-existing)
135 public Object
getField(int n
) {
136 if (fFields
== null) {
139 if (n
>= 0 && n
< fFields
.length
)
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
152 public void setEvent(TmfEvent event
) {
153 fParentEvent
= event
;
157 * Parse the content into fields. By default, a single field (the raw
158 * content) is returned.
159 * Should be overridden.
161 protected void parseContent() {
162 fFields
= new Object
[1];
163 fFields
[0] = fRawContent
;
166 // ------------------------------------------------------------------------
168 // ------------------------------------------------------------------------
171 public int hashCode() {
173 result
= 37 * result
+ ((fParentEvent
!= null) ? fParentEvent
.hashCode() : 0);
174 result
= 37 * result
+ ((fRawContent
!= null) ? fRawContent
.hashCode() : 0);
179 public boolean equals(Object other
) {
180 if (!(other
instanceof TmfEventContent
))
182 TmfEventContent o
= (TmfEventContent
) other
;
183 return fRawContent
.equals(o
.fRawContent
);
187 @SuppressWarnings("nls")
188 public String
toString() {
189 Object
[] fields
= getFields();
190 StringBuilder result
= new StringBuilder("[TmfEventContent(");
191 for (int i
= 0; i
< fields
.length
; i
++) {
192 if (i
> 0) result
.append(",");
193 result
.append(fields
[i
]);
196 return result
.toString();
200 public TmfEventContent
clone() {
201 TmfEventContent clone
= null;
203 clone
= (TmfEventContent
) super.clone();
204 clone
.fParentEvent
= fParentEvent
;
205 clone
.fRawContent
= fRawContent
;
206 clone
.fFields
= fFields
;
208 catch (CloneNotSupportedException e
) {