tmf: Add TextTrace abstract class with trace validation status
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / text / TextTraceEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated equals, clone and hashCode to consider StringBuffer values
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.trace.text;
15
16 import java.util.Arrays;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
19
20 /**
21 * Implementation of ITmfEventField for Text Traces.
22 *
23 * @since 3.0
24 */
25 public class TextTraceEventContent implements ITmfEventField, Cloneable {
26
27 private String fName;
28 private Object fValue;
29 private TextTraceEventContent[] fFields;
30
31 // ------------------------------------------------------------------------
32 // Constructors
33 // ------------------------------------------------------------------------
34
35 /**
36 * Constructor for a root event content
37 *
38 * @param fieldNames
39 * the array of field names
40 */
41 public TextTraceEventContent(String[] fieldNames) {
42 this(ITmfEventField.ROOT_FIELD_ID);
43 fFields = new TextTraceEventContent[fieldNames.length];
44 for (int i = 0; i < fFields.length; i++) {
45 fFields[i] = new TextTraceEventContent(fieldNames[i]);
46 }
47 }
48
49 /**
50 * Constructor for a subfield
51 *
52 * @param fieldNames
53 * the array of field names
54 */
55 private TextTraceEventContent(String fieldName) {
56 fName = fieldName;
57 }
58
59 // ------------------------------------------------------------------------
60 // ITmfEventField
61 // ------------------------------------------------------------------------
62
63 @Override
64 public String getName() {
65 return fName;
66 }
67
68 @Override
69 public Object getValue() {
70 return fValue;
71 }
72
73 @Override
74 public String[] getFieldNames() {
75 String[] fieldNames = new String[fFields.length];
76 for (int i = 0; i < fieldNames.length; i++) {
77 fieldNames[i] = fFields[i].getName();
78 }
79 return fieldNames;
80 }
81
82 @Override
83 public String getFieldName(int index) {
84 if (index >= 0 && index < fFields.length) {
85 return fFields[index].getName();
86 }
87 return null;
88 }
89
90 @Override
91 public ITmfEventField[] getFields() {
92 return fFields;
93 }
94
95 @Override
96 public ITmfEventField getField(String name) {
97 for (int i = 0; i < fFields.length; i++) {
98 if (fFields[i].getName().equals(name)) {
99 return fFields[i];
100 }
101 }
102 return null;
103 }
104
105 @Override
106 public ITmfEventField getField(int index) {
107 if (index >= 0 && index < fFields.length) {
108 return fFields[index];
109 }
110 return null;
111 }
112
113 @Override
114 public String getFormattedValue() {
115 return fValue.toString();
116 }
117
118 @Override
119 public ITmfEventField getSubField(String[] names) {
120 ITmfEventField field = this;
121 for (String name : names) {
122 field = field.getField(name);
123 if (field == null) {
124 return null;
125 }
126 }
127 return field;
128 }
129
130 // ------------------------------------------------------------------------
131 // Convenience getters and setters
132 // ------------------------------------------------------------------------
133
134 /**
135 * Get a subfield value by name
136 *
137 * @param name
138 * a subfield name
139 * @return field value object
140 */
141 public Object getFieldValue(String name) {
142 for (int i = 0; i < fFields.length; i++) {
143 if (fFields[i].getName().equals(name)) {
144 return fFields[i].getValue();
145 }
146 }
147 return null;
148 }
149
150 /**
151 * Get a subfield value by index
152 *
153 * @param index
154 * a subfield index
155 * @return field value object
156 */
157 public Object getFieldValue(int index) {
158 if (index >= 0 && index < fFields.length) {
159 return fFields[index].getValue();
160 }
161 return null;
162 }
163
164 /**
165 * Set the content value
166 *
167 * @param value
168 * the content value
169 */
170 public void setValue(Object value) {
171 fValue = value;
172 }
173
174 /**
175 * Set a subfield value by name
176 *
177 * @param name
178 * a subfield name
179 * @param value
180 * the subfield value
181 */
182 public void setFieldValue(String name, Object value) {
183 for (int i = 0; i < fFields.length; i++) {
184 if (fFields[i].getName().equals(name)) {
185 fFields[i].fValue = value;
186 }
187 }
188 }
189
190 /**
191 * Set a subfield value by index
192 *
193 * @param index
194 * a subfield index
195 * @param value
196 * the subfield value
197 */
198 public void setFieldValue(int index, Object value) {
199 if (index >= 0 && index < fFields.length) {
200 fFields[index].fValue = value;
201 }
202 }
203
204 // ------------------------------------------------------------------------
205 // Cloneable
206 // ------------------------------------------------------------------------
207
208 @Override
209 public TextTraceEventContent clone() {
210 TextTraceEventContent clone = null;
211 try {
212 clone = (TextTraceEventContent) super.clone();
213 clone.fName = fName;
214 if (fValue instanceof StringBuffer) {
215 StringBuffer value = new StringBuffer(fValue.toString());
216 clone.fValue = value;
217 } else {
218 clone.fValue = fValue;
219 }
220 clone.fFields = (fFields != null) ? fFields.clone() : null;
221 if (fFields != null) {
222 for (int i = 0; i < fFields.length; i++) {
223 clone.fFields[i] = fFields[i].clone();
224 }
225 }
226 } catch (CloneNotSupportedException e) {
227 }
228 return clone;
229 }
230
231 // ------------------------------------------------------------------------
232 // Object
233 // ------------------------------------------------------------------------
234
235 @Override
236 public int hashCode() {
237 final int prime = 31;
238 int result = 1;
239 result = prime * result + Arrays.hashCode(fFields);
240 result = prime * result + ((fName == null) ? 0 : fName.hashCode());
241 int tmpHash = 0; // initialize for fValue equals null;
242 if (fValue != null) {
243 if (fValue instanceof StringBuffer) {
244 tmpHash = fValue.toString().hashCode();
245 } else {
246 tmpHash = fValue.hashCode();
247 }
248 }
249 result = prime * result + tmpHash;
250 return result;
251 }
252
253 @Override
254 public boolean equals(Object obj) {
255 if (this == obj) {
256 return true;
257 }
258 if (obj == null) {
259 return false;
260 }
261 if (getClass() != obj.getClass()) {
262 return false;
263 }
264 TextTraceEventContent other = (TextTraceEventContent) obj;
265 if (!Arrays.equals(fFields, other.fFields)) {
266 return false;
267 }
268 if (fName == null) {
269 if (other.fName != null) {
270 return false;
271 }
272 } else if (!fName.equals(other.fName)) {
273 return false;
274 }
275 if (fValue == null) {
276 if (other.fValue != null) {
277 return false;
278 }
279 } else {
280 if ((fValue instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) {
281 if (!fValue.toString().equals(other.fValue.toString())) {
282 return false;
283 }
284 } else if (!fValue.equals(other.fValue)) {
285 return false;
286 }
287 }
288 return true;
289 }
290
291 @Override
292 public String toString() {
293 StringBuilder sb = new StringBuilder();
294 if (fName == ITmfEventField.ROOT_FIELD_ID) {
295 for (int i = 0; i < getFields().length; i++) {
296 ITmfEventField field = getFields()[i];
297 if (i != 0) {
298 sb.append(", "); //$NON-NLS-1$
299 }
300 sb.append(field.toString());
301 }
302 } else {
303 sb.append(fName);
304 sb.append('=');
305 sb.append(fValue);
306 }
307 return sb.toString();
308 }
309
310 }
This page took 0.037625 seconds and 5 git commands to generate.