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
;
17 * <b><u>TmfEventContent</u></b>
19 * A basic implementation of ITmfEventContent.
21 public class TmfEventContent
implements Cloneable
{
23 // ------------------------------------------------------------------------
25 // ------------------------------------------------------------------------
28 public static final String FIELD_ID_TIMESTAMP
= ":timestamp:"; //$NON-NLS-1$
29 public static final String FIELD_ID_SOURCE
= ":source:"; //$NON-NLS-1$
30 public static final String FIELD_ID_TYPE
= ":type:"; //$NON-NLS-1$
31 public static final String FIELD_ID_REFERENCE
= ":reference:"; //$NON-NLS-1$
32 public static final String FIELD_ID_CONTENT
= ":content:"; //$NON-NLS-1$
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 protected TmfEvent fParentEvent
;
39 protected Object fRawContent
;
40 protected Object
[] fFields
;
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * @param parent the parent event (owner)
48 * @param content the raw content
50 public TmfEventContent(TmfEvent parent
, Object content
) {
51 fParentEvent
= parent
;
52 fRawContent
= content
;
56 * @param other the original event content
58 public TmfEventContent(TmfEventContent other
) {
60 throw new IllegalArgumentException();
61 fParentEvent
= other
.fParentEvent
;
62 fRawContent
= other
.fRawContent
;
63 fFields
= other
.fFields
;
66 @SuppressWarnings("unused")
67 private TmfEventContent() {
68 throw new AssertionError();
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * @return the parent (containing) event
78 public TmfEvent
getEvent() {
83 * @return the event type
85 public TmfEventType
getType() {
86 return fParentEvent
.getType();
90 * @return the raw content
92 public Object
getContent() {
97 * Returns the list of fields in the same order as TmfEventType.getLabels()
99 * @return the ordered set of fields (optional fields might be null)
101 public Object
[] getFields() {
102 if (fFields
== null) {
109 * @param id the field id
110 * @return the corresponding field
111 * @throws TmfNoSuchFieldException
113 public Object
getField(String id
) throws TmfNoSuchFieldException
{
114 if (fFields
== null) {
118 return fFields
[getType().getFieldIndex(id
)];
119 } catch (TmfNoSuchFieldException e
) {
120 // Required for filtering from default TmfEventsTable columns
121 if (id
.equals(FIELD_ID_CONTENT
)) {
122 return fParentEvent
.getContent().toString();
123 } else if (id
.equals(FIELD_ID_TIMESTAMP
)) {
124 return new Long(fParentEvent
.getTimestamp().getValue()).toString();
125 } else if (id
.equals(FIELD_ID_SOURCE
)) {
126 return fParentEvent
.getSource();
127 } else if (id
.equals(FIELD_ID_TYPE
)) {
128 return fParentEvent
.getType().getId();
129 } else if (id
.equals(FIELD_ID_REFERENCE
)) {
130 return fParentEvent
.getReference();
137 * @param n the field index as per TmfEventType.getLabels()
138 * @return the corresponding field (null if non-existing)
140 public Object
getField(int n
) {
141 if (fFields
== null) {
144 if (n
>= 0 && n
< fFields
.length
)
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
157 // public void setEvent(TmfEvent event) {
158 // fParentEvent = event;
162 * Parse the content into fields. By default, a single field (the raw
163 * content) is returned.
164 * Should be overridden.
166 protected void parseContent() {
167 fFields
= new Object
[1];
168 fFields
[0] = fRawContent
;
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 public int hashCode() {
178 result
= 37 * result
+ ((fRawContent
!= null) ? fRawContent
.hashCode() : 0);
183 public boolean equals(Object other
) {
184 if (!(other
instanceof TmfEventContent
))
186 TmfEventContent o
= (TmfEventContent
) other
;
187 if (fRawContent
== null) {
188 return o
.fRawContent
== null;
190 return fRawContent
.equals(o
.fRawContent
);
194 @SuppressWarnings("nls")
195 public String
toString() {
196 Object
[] fields
= getFields();
197 StringBuilder result
= new StringBuilder("[TmfEventContent(");
198 for (int i
= 0; i
< fields
.length
; i
++) {
199 if (i
> 0) result
.append(",");
200 result
.append(fields
[i
]);
203 return result
.toString();
207 public TmfEventContent
clone() {
208 TmfEventContent clone
= null;
210 clone
= (TmfEventContent
) super.clone();
211 clone
.fParentEvent
= fParentEvent
;
212 clone
.fRawContent
= fRawContent
;
213 clone
.fFields
= fFields
;
215 catch (CloneNotSupportedException e
) {