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