Re-structure LTTng sub-project as per the Linux Tools guidelines
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.event;
14
15 /**
16 * <b><u>TmfEventContent</u></b>
17 * <p>
18 * The event content.
19 */
20 public class TmfEventContent implements Cloneable {
21
22 // Default field ids
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$
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 protected TmfEvent fParentEvent;
34 protected Object fRawContent;
35 protected Object[] fFields;
36
37 // ------------------------------------------------------------------------
38 // Constructors
39 // ------------------------------------------------------------------------
40
41 /**
42 * @param parent the parent event (owner)
43 * @param content the raw content
44 */
45 public TmfEventContent(TmfEvent parent, Object content) {
46 fParentEvent = parent;
47 fRawContent = content;
48 }
49
50 /**
51 * @param other the original event content
52 */
53 public TmfEventContent(TmfEventContent other) {
54 if (other == null)
55 throw new IllegalArgumentException();
56 fParentEvent = other.fParentEvent;
57 fRawContent = other.fRawContent;
58 fFields = other.fFields;
59 }
60
61 @SuppressWarnings("unused")
62 private TmfEventContent() {
63 throw new AssertionError();
64 }
65
66 // ------------------------------------------------------------------------
67 // Accessors
68 // ------------------------------------------------------------------------
69
70 /**
71 * @return the parent (containing) event
72 */
73 public TmfEvent getEvent() {
74 return fParentEvent;
75 }
76
77 /**
78 * @return the event type
79 */
80 public TmfEventType getType() {
81 return fParentEvent.getType();
82 }
83
84 /**
85 * @return the raw content
86 */
87 public Object getContent() {
88 return fRawContent;
89 }
90
91 /**
92 * Returns the list of fields in the same order as TmfEventType.getLabels()
93 *
94 * @return the ordered set of fields (optional fields might be null)
95 */
96 public Object[] getFields() {
97 if (fFields == null) {
98 parseContent();
99 }
100 return fFields;
101 }
102
103 /**
104 * @param id the field id
105 * @return the corresponding field
106 * @throws TmfNoSuchFieldException
107 */
108 public Object getField(String id) throws TmfNoSuchFieldException {
109 if (fFields == null) {
110 parseContent();
111 }
112 try {
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();
126 }
127 throw e;
128 }
129 }
130
131 /**
132 * @param n the field index as per TmfEventType.getLabels()
133 * @return the corresponding field (null if non-existing)
134 */
135 public Object getField(int n) {
136 if (fFields == null) {
137 parseContent();
138 }
139 if (n >= 0 && n < fFields.length)
140 return fFields[n];
141
142 return null;
143 }
144
145 // ------------------------------------------------------------------------
146 // Operators
147 // ------------------------------------------------------------------------
148
149 /**
150 * @param event
151 */
152 public void setEvent(TmfEvent event) {
153 fParentEvent = event;
154 }
155
156 /**
157 * Parse the content into fields. By default, a single field (the raw
158 * content) is returned.
159 * Should be overridden.
160 */
161 protected void parseContent() {
162 fFields = new Object[1];
163 fFields[0] = fRawContent;
164 }
165
166 // ------------------------------------------------------------------------
167 // Object
168 // ------------------------------------------------------------------------
169
170 @Override
171 public int hashCode() {
172 int result = 17;
173 result = 37 * result + ((fParentEvent != null) ? fParentEvent.hashCode() : 0);
174 result = 37 * result + ((fRawContent != null) ? fRawContent.hashCode() : 0);
175 return result;
176 }
177
178 @Override
179 public boolean equals(Object other) {
180 if (!(other instanceof TmfEventContent))
181 return false;
182 TmfEventContent o = (TmfEventContent) other;
183 return fRawContent.equals(o.fRawContent);
184 }
185
186 @Override
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]);
194 }
195 result.append(")]");
196 return result.toString();
197 }
198
199 @Override
200 public TmfEventContent clone() {
201 TmfEventContent clone = null;
202 try {
203 clone = (TmfEventContent) super.clone();
204 clone.fParentEvent = fParentEvent;
205 clone.fRawContent = fRawContent;
206 clone.fFields = fFields;
207 }
208 catch (CloneNotSupportedException e) {
209 e.printStackTrace();
210 }
211 return clone;
212 }
213 }
This page took 0.0365 seconds and 5 git commands to generate.