Merge branch 'master' into TmfEventModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.event;
15
16 import java.util.Arrays;
17
18 /**
19 * <b><u>TmfEventContent</u></b>
20 * <p>
21 * A basic implementation of ITmfEventContent where the raw content is a String.
22 */
23 public class TmfEventContent implements ITmfEventContent {
24
25 // ------------------------------------------------------------------------
26 // Constants
27 // ------------------------------------------------------------------------
28
29 // Default field IDs
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$
35
36 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39
40 protected TmfEvent fParentEvent;
41 protected String fRawContent;
42 protected TmfEventField[] fFields;
43
44 // ------------------------------------------------------------------------
45 // Constructors
46 // ------------------------------------------------------------------------
47
48 /**
49 * Default constructor
50 */
51 @SuppressWarnings("unused")
52 private TmfEventContent() {
53 throw new AssertionError();
54 }
55
56 /**
57 * Full constructor
58 *
59 * @param parent the parent event (owner)
60 * @param content the raw content as a byte[]
61 */
62 public TmfEventContent(TmfEvent parent, String content) {
63 fParentEvent = parent;
64 fRawContent = content;
65 }
66
67 /**
68 * Copy constructor
69 *
70 * @param content the original event content
71 */
72 public TmfEventContent(TmfEventContent content) {
73 if (content == null)
74 throw new IllegalArgumentException();
75 fParentEvent = content.fParentEvent;
76 fRawContent = content.fRawContent;
77 fFields = content.fFields;
78 }
79
80 // ------------------------------------------------------------------------
81 // Accessors
82 // ------------------------------------------------------------------------
83
84 /**
85 * @return the parent (containing) event
86 */
87 public TmfEvent getEvent() {
88 return fParentEvent;
89 }
90
91 /**
92 * @return the event type
93 */
94 public ITmfEventType getType() {
95 return fParentEvent.getType();
96 }
97
98 /**
99 * @return the raw content
100 */
101 public Object getRawContent() {
102 return fRawContent;
103 }
104
105 /**
106 * @return the serialized content
107 */
108 public String getFmtContent() {
109 return fRawContent;
110 }
111
112 /**
113 * Returns the list of fields in the same order as TmfEventType.getLabels()
114 *
115 * @return the ordered set of fields (optional fields might be null)
116 */
117 public ITmfEventField[] getFields() {
118 if (fFields == null) {
119 parseContent();
120 }
121 return fFields;
122 }
123
124 /**
125 * @param id the field id
126 * @return the corresponding field
127 * @throws TmfNoSuchFieldException
128 */
129 public ITmfEventField getField(String id) throws TmfNoSuchFieldException {
130 if (fFields == null) {
131 parseContent();
132 }
133 try {
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());
147 }
148 throw e;
149 }
150 }
151
152 /**
153 * @param n the field index as per TmfEventType.getLabels()
154 * @return the corresponding field (null if non-existing)
155 */
156 public ITmfEventField getField(int n) {
157 if (fFields == null) {
158 parseContent();
159 }
160 if (n >= 0 && n < fFields.length)
161 return fFields[n];
162
163 return null;
164 }
165
166 // ------------------------------------------------------------------------
167 // Operations
168 // ------------------------------------------------------------------------
169
170 /**
171 * Parse the content into fields. By default, a single field (the raw
172 * content) is returned.
173 *
174 * Should be overridden.
175 */
176 protected void parseContent() {
177 fFields = new TmfEventField[1];
178 fFields[0] = new TmfEventField(this, FIELD_ID_CONTENT, fRawContent);
179 }
180
181 // ------------------------------------------------------------------------
182 // Cloneable
183 // ------------------------------------------------------------------------
184
185 @Override
186 public ITmfEventContent clone() {
187 TmfEventContent clone = null;
188 try {
189 clone = (TmfEventContent) super.clone();
190 clone.fParentEvent = fParentEvent;
191 clone.fRawContent = fRawContent;
192 clone.fFields = fFields;
193 }
194 catch (CloneNotSupportedException e) {
195 e.printStackTrace();
196 }
197 return clone;
198 }
199
200 // ------------------------------------------------------------------------
201 // Object
202 // ------------------------------------------------------------------------
203
204 @Override
205 public int hashCode() {
206 final int prime = 31;
207 int result = 1;
208 result = prime * result + Arrays.hashCode(fFields);
209 result = prime * result + ((fRawContent == null) ? 0 : fRawContent.hashCode());
210 return result;
211 }
212
213 @Override
214 public boolean equals(Object obj) {
215 if (this == obj)
216 return true;
217 if (obj == null)
218 return false;
219 if (getClass() != obj.getClass())
220 return false;
221 TmfEventContent other = (TmfEventContent) obj;
222 if (!Arrays.equals(fFields, other.fFields))
223 return false;
224 if (fRawContent == null) {
225 if (other.fRawContent != null)
226 return false;
227 } else if (!fRawContent.equals(other.fRawContent))
228 return false;
229 return true;
230 }
231
232 @Override
233 @SuppressWarnings("nls")
234 public String toString() {
235 return "TmfEventContent [fRawContent=" + fRawContent + ", fFields=" + Arrays.toString(fFields) + "]";
236 }
237
238 }
This page took 0.03456 seconds and 5 git commands to generate.