Refactor TmfEventType and TmfEventField
[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 /**
17 * <b><u>TmfEventContent</u></b>
18 * <p>
19 * A basic implementation of ITmfEventContent.
20 */
21 public class TmfEventContent implements Cloneable {
22
23 // ------------------------------------------------------------------------
24 // Constants
25 // ------------------------------------------------------------------------
26
27 // Default field IDs
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$
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 protected TmfEvent fParentEvent;
39 protected Object fRawContent;
40 protected Object[] fFields;
41
42 // ------------------------------------------------------------------------
43 // Constructors
44 // ------------------------------------------------------------------------
45
46 /**
47 * @param parent the parent event (owner)
48 * @param content the raw content
49 */
50 public TmfEventContent(TmfEvent parent, Object content) {
51 fParentEvent = parent;
52 fRawContent = content;
53 }
54
55 /**
56 * @param other the original event content
57 */
58 public TmfEventContent(TmfEventContent other) {
59 if (other == null)
60 throw new IllegalArgumentException();
61 fParentEvent = other.fParentEvent;
62 fRawContent = other.fRawContent;
63 fFields = other.fFields;
64 }
65
66 @SuppressWarnings("unused")
67 private TmfEventContent() {
68 throw new AssertionError();
69 }
70
71 // ------------------------------------------------------------------------
72 // Accessors
73 // ------------------------------------------------------------------------
74
75 /**
76 * @return the parent (containing) event
77 */
78 public TmfEvent getEvent() {
79 return fParentEvent;
80 }
81
82 /**
83 * @return the event type
84 */
85 public TmfEventType getType() {
86 return fParentEvent.getType();
87 }
88
89 /**
90 * @return the raw content
91 */
92 public Object getContent() {
93 return fRawContent;
94 }
95
96 /**
97 * Returns the list of fields in the same order as TmfEventType.getLabels()
98 *
99 * @return the ordered set of fields (optional fields might be null)
100 */
101 public Object[] getFields() {
102 if (fFields == null) {
103 parseContent();
104 }
105 return fFields;
106 }
107
108 /**
109 * @param id the field id
110 * @return the corresponding field
111 * @throws TmfNoSuchFieldException
112 */
113 public Object getField(String id) throws TmfNoSuchFieldException {
114 if (fFields == null) {
115 parseContent();
116 }
117 try {
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();
131 }
132 throw e;
133 }
134 }
135
136 /**
137 * @param n the field index as per TmfEventType.getLabels()
138 * @return the corresponding field (null if non-existing)
139 */
140 public Object getField(int n) {
141 if (fFields == null) {
142 parseContent();
143 }
144 if (n >= 0 && n < fFields.length)
145 return fFields[n];
146
147 return null;
148 }
149
150 // ------------------------------------------------------------------------
151 // Operators
152 // ------------------------------------------------------------------------
153
154 // /**
155 // * @param event
156 // */
157 // public void setEvent(TmfEvent event) {
158 // fParentEvent = event;
159 // }
160
161 /**
162 * Parse the content into fields. By default, a single field (the raw
163 * content) is returned.
164 * Should be overridden.
165 */
166 protected void parseContent() {
167 fFields = new Object[1];
168 fFields[0] = fRawContent;
169 }
170
171 // ------------------------------------------------------------------------
172 // Object
173 // ------------------------------------------------------------------------
174
175 @Override
176 public int hashCode() {
177 int result = 17;
178 result = 37 * result + ((fRawContent != null) ? fRawContent.hashCode() : 0);
179 return result;
180 }
181
182 @Override
183 public boolean equals(Object other) {
184 if (!(other instanceof TmfEventContent))
185 return false;
186 TmfEventContent o = (TmfEventContent) other;
187 if (fRawContent == null) {
188 return o.fRawContent == null;
189 }
190 return fRawContent.equals(o.fRawContent);
191 }
192
193 @Override
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]);
201 }
202 result.append(")]");
203 return result.toString();
204 }
205
206 @Override
207 public TmfEventContent clone() {
208 TmfEventContent clone = null;
209 try {
210 clone = (TmfEventContent) super.clone();
211 clone.fParentEvent = fParentEvent;
212 clone.fRawContent = fRawContent;
213 clone.fFields = fFields;
214 }
215 catch (CloneNotSupportedException e) {
216 e.printStackTrace();
217 }
218 return clone;
219 }
220 }
This page took 0.036169 seconds and 5 git commands to generate.