Refactor TmfEventType and TmfEventField
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventContent.java
CommitLineData
8c8bf09f 1/*******************************************************************************
bbc1c411 2 * Copyright (c) 2009, 2012 Ericsson
8c8bf09f
ASL
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:
1f506a43 10 * Francois Chouinard - Initial API and implementation
bbc1c411 11 * Francois Chouinard - Updated as per TMF Event Model 1.0
8c8bf09f
ASL
12 *******************************************************************************/
13
6c13869b 14package org.eclipse.linuxtools.tmf.core.event;
8c8bf09f 15
8c8bf09f
ASL
16/**
17 * <b><u>TmfEventContent</u></b>
18 * <p>
bbc1c411 19 * A basic implementation of ITmfEventContent.
8c8bf09f 20 */
1a971e96 21public class TmfEventContent implements Cloneable {
8c8bf09f 22
bbc1c411 23 // ------------------------------------------------------------------------
24 // Constants
25 // ------------------------------------------------------------------------
26
27 // Default field IDs
4bf17f4a 28 public static final String FIELD_ID_TIMESTAMP = ":timestamp:"; //$NON-NLS-1$
bbc1c411 29 public static final String FIELD_ID_SOURCE = ":source:"; //$NON-NLS-1$
30 public static final String FIELD_ID_TYPE = ":type:"; //$NON-NLS-1$
4bf17f4a 31 public static final String FIELD_ID_REFERENCE = ":reference:"; //$NON-NLS-1$
bbc1c411 32 public static final String FIELD_ID_CONTENT = ":content:"; //$NON-NLS-1$
e2561baf 33
cbd4ad82 34 // ------------------------------------------------------------------------
8c8bf09f 35 // Attributes
cbd4ad82 36 // ------------------------------------------------------------------------
8c8bf09f 37
cbd4ad82
FC
38 protected TmfEvent fParentEvent;
39 protected Object fRawContent;
40 protected Object[] fFields;
8c8bf09f 41
cbd4ad82 42 // ------------------------------------------------------------------------
8c8bf09f 43 // Constructors
cbd4ad82 44 // ------------------------------------------------------------------------
8c8bf09f
ASL
45
46 /**
cbd4ad82
FC
47 * @param parent the parent event (owner)
48 * @param content the raw content
8c8bf09f 49 */
28b94d61
FC
50 public TmfEventContent(TmfEvent parent, Object content) {
51 fParentEvent = parent;
52 fRawContent = content;
8c8bf09f
ASL
53 }
54
28b94d61 55 /**
cbd4ad82 56 * @param other the original event content
28b94d61
FC
57 */
58 public TmfEventContent(TmfEventContent other) {
cbd4ad82
FC
59 if (other == null)
60 throw new IllegalArgumentException();
28b94d61
FC
61 fParentEvent = other.fParentEvent;
62 fRawContent = other.fRawContent;
63 fFields = other.fFields;
64 }
65
66 @SuppressWarnings("unused")
67 private TmfEventContent() {
cbd4ad82 68 throw new AssertionError();
28b94d61
FC
69 }
70
cbd4ad82 71 // ------------------------------------------------------------------------
8c8bf09f 72 // Accessors
cbd4ad82 73 // ------------------------------------------------------------------------
8c8bf09f
ASL
74
75 /**
28b94d61 76 * @return the parent (containing) event
8c8bf09f 77 */
28b94d61
FC
78 public TmfEvent getEvent() {
79 return fParentEvent;
8c8bf09f
ASL
80 }
81
82 /**
28b94d61 83 * @return the event type
8c8bf09f 84 */
28b94d61
FC
85 public TmfEventType getType() {
86 return fParentEvent.getType();
8c8bf09f
ASL
87 }
88
28b94d61
FC
89 /**
90 * @return the raw content
91 */
92 public Object getContent() {
93 return fRawContent;
94 }
8c8bf09f
ASL
95
96 /**
28b94d61
FC
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)
8c8bf09f 100 */
28b94d61
FC
101 public Object[] getFields() {
102 if (fFields == null) {
103 parseContent();
104 }
8c8bf09f
ASL
105 return fFields;
106 }
107
108 /**
cbd4ad82
FC
109 * @param id the field id
110 * @return the corresponding field
111 * @throws TmfNoSuchFieldException
8c8bf09f 112 */
4bf17f4a 113 public Object getField(String id) throws TmfNoSuchFieldException {
114 if (fFields == null) {
115 parseContent();
116 }
117 try {
118 return fFields[getType().getFieldIndex(id)];
e2561baf 119 } catch (TmfNoSuchFieldException e) {
4bf17f4a 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)) {
99005796 126 return fParentEvent.getSource();
4bf17f4a 127 } else if (id.equals(FIELD_ID_TYPE)) {
cbbcc354 128 return fParentEvent.getType().getId();
4bf17f4a 129 } else if (id.equals(FIELD_ID_REFERENCE)) {
4641c2f7 130 return fParentEvent.getReference();
4bf17f4a 131 }
132 throw e;
e2561baf 133 }
4bf17f4a 134 }
8c8bf09f 135
146a887c 136 /**
cbd4ad82
FC
137 * @param n the field index as per TmfEventType.getLabels()
138 * @return the corresponding field (null if non-existing)
146a887c 139 */
28b94d61
FC
140 public Object getField(int n) {
141 if (fFields == null) {
142 parseContent();
143 }
144 if (n >= 0 && n < fFields.length)
145 return fFields[n];
cbd4ad82 146
28b94d61
FC
147 return null;
148 }
149
cbd4ad82 150 // ------------------------------------------------------------------------
28b94d61 151 // Operators
cbd4ad82 152 // ------------------------------------------------------------------------
28b94d61 153
bbc1c411 154// /**
155// * @param event
156// */
157// public void setEvent(TmfEvent event) {
158// fParentEvent = event;
159// }
023761c4 160
28b94d61 161 /**
cbd4ad82
FC
162 * Parse the content into fields. By default, a single field (the raw
163 * content) is returned.
164 * Should be overridden.
28b94d61
FC
165 */
166 protected void parseContent() {
167 fFields = new Object[1];
168 fFields[0] = fRawContent;
146a887c 169 }
28b94d61 170
cbd4ad82
FC
171 // ------------------------------------------------------------------------
172 // Object
173 // ------------------------------------------------------------------------
174
175 @Override
176 public int hashCode() {
177 int result = 17;
cbd4ad82
FC
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;
0c841e0f
PT
187 if (fRawContent == null) {
188 return o.fRawContent == null;
189 }
cbd4ad82 190 return fRawContent.equals(o.fRawContent);
28b94d61
FC
191 }
192
193 @Override
3b38ea61 194 @SuppressWarnings("nls")
28b94d61
FC
195 public String toString() {
196 Object[] fields = getFields();
cbd4ad82 197 StringBuilder result = new StringBuilder("[TmfEventContent(");
28b94d61 198 for (int i = 0; i < fields.length; i++) {
cbd4ad82
FC
199 if (i > 0) result.append(",");
200 result.append(fields[i]);
28b94d61 201 }
cbd4ad82
FC
202 result.append(")]");
203 return result.toString();
28b94d61 204 }
146a887c 205
1a971e96
FC
206 @Override
207 public TmfEventContent clone() {
208 TmfEventContent clone = null;
209 try {
210 clone = (TmfEventContent) super.clone();
211 clone.fParentEvent = fParentEvent;
c87f4702
FC
212 clone.fRawContent = fRawContent;
213 clone.fFields = fFields;
1a971e96
FC
214 }
215 catch (CloneNotSupportedException e) {
216 e.printStackTrace();
217 }
218 return clone;
219 }
8c8bf09f 220}
This page took 0.042849 seconds and 5 git commands to generate.