Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / text / TextTraceEventContent.java
CommitLineData
bcb8c2cb 1/*******************************************************************************
97de0bca 2 * Copyright (c) 2012, 2015 Ericsson
bcb8c2cb
PT
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
2bdf0193 14package org.eclipse.tracecompass.tmf.core.trace.text;
bcb8c2cb 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;
2bdf0193 22import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
bcb8c2cb
PT
23
24/**
25 * Implementation of ITmfEventField for Text Traces.
bcb8c2cb 26 */
b742c196 27public class TextTraceEventContent implements ITmfEventField {
bcb8c2cb 28
b742c196
AM
29 private final @NonNull String fName;
30 private final @NonNull List<TextTraceEventContent> fFields;
31
32 private @Nullable Object fValue;
bcb8c2cb
PT
33
34 // ------------------------------------------------------------------------
35 // Constructors
36 // ------------------------------------------------------------------------
37
38 /**
e604cdf6
PT
39 * Constructor for a root event content. Subfields with the specified field
40 * names are created and initialized with a null value.
bcb8c2cb
PT
41 *
42 * @param fieldNames
e604cdf6
PT
43 * the array of non-null field names
44 * @throws IllegalArgumentException
45 * if any one of the field names is null
bcb8c2cb 46 */
4c4e2816 47 public TextTraceEventContent(String @NonNull [] fieldNames) {
b742c196
AM
48 fName = ITmfEventField.ROOT_FIELD_ID;
49 fValue = null;
50 fFields = new ArrayList<>(fieldNames.length);
51 for (String fieldName : fieldNames) {
52 if (fieldName == null) {
53 throw new IllegalArgumentException("Null field name not allowed"); //$NON-NLS-1$
54 }
55 fFields.add(new TextTraceEventContent(fieldName));
bcb8c2cb
PT
56 }
57 }
58
e604cdf6
PT
59 /**
60 * Constructor for an initial capacity. This should be the expected number
61 * of fields.
62 *
63 * @param initialCapacity
64 * the initial capacity of the field list
dbc7991d 65 * @since 1.0
e604cdf6
PT
66 */
67 public TextTraceEventContent(int initialCapacity) {
68 fName = ITmfEventField.ROOT_FIELD_ID;
69 fValue = null;
70 fFields = new ArrayList<>(initialCapacity);
71 }
72
bcb8c2cb
PT
73 /**
74 * Constructor for a subfield
75 *
e604cdf6
PT
76 * @param fieldName
77 * the subfield name
bcb8c2cb 78 */
b742c196 79 private TextTraceEventContent(@NonNull String fieldName) {
bcb8c2cb 80 fName = fieldName;
b742c196 81 fValue = null;
aa353506 82 fFields = Collections.EMPTY_LIST;
bcb8c2cb
PT
83 }
84
85 // ------------------------------------------------------------------------
86 // ITmfEventField
87 // ------------------------------------------------------------------------
88
89 @Override
90 public String getName() {
91 return fName;
92 }
93
94 @Override
95 public Object getValue() {
96 return fValue;
97 }
98
99 @Override
b742c196
AM
100 public List<String> getFieldNames() {
101 List<String> fieldNames = new ArrayList<>(fFields.size());
102 for (TextTraceEventContent field : fFields) {
103 fieldNames.add(field.getName());
bcb8c2cb
PT
104 }
105 return fieldNames;
106 }
107
bcb8c2cb 108 @Override
b742c196
AM
109 public List<TextTraceEventContent> getFields() {
110 return new ArrayList<>(fFields);
bcb8c2cb
PT
111 }
112
113 @Override
97de0bca
PT
114 public ITmfEventField getField(String... path) {
115 if (path.length == 0) {
116 return this;
117 }
118 // There are no sub fields
119 if (path.length == 1) {
120 for (TextTraceEventContent field : fFields) {
121 if (field.getName().equals(path[0])) {
122 return field;
123 }
bcb8c2cb
PT
124 }
125 }
126 return null;
127 }
128
bcb8c2cb
PT
129 @Override
130 public String getFormattedValue() {
b742c196
AM
131 Object value = fValue;
132 if (value == null) {
133 return null;
134 }
135 return value.toString();
bcb8c2cb
PT
136 }
137
bcb8c2cb
PT
138 // ------------------------------------------------------------------------
139 // Convenience getters and setters
140 // ------------------------------------------------------------------------
141
99f21044 142 /**
e604cdf6 143 * Get a field name by index.
99f21044
AM
144 *
145 * @param index
146 * The index of the field
147 * @return The name of the field at that index
148 */
149 public String getFieldName(int index) {
150 if (index >= 0 && index < fFields.size()) {
151 return fFields.get(index).getName();
152 }
153 return null;
154 }
155
156 /**
e604cdf6 157 * Get a field by index.
99f21044
AM
158 *
159 * @param index
160 * The index of the field
161 * @return The field object at the requested index
162 */
163 public ITmfEventField getField(int index) {
164 if (index >= 0 && index < fFields.size()) {
165 return fFields.get(index);
166 }
167 return null;
168 }
169
bcb8c2cb 170 /**
e604cdf6 171 * Get a subfield value by name.
bcb8c2cb
PT
172 *
173 * @param name
174 * a subfield name
175 * @return field value object
176 */
e604cdf6 177 public Object getFieldValue(@NonNull String name) {
b742c196
AM
178 for (int i = 0; i < fFields.size(); i++) {
179 if (fFields.get(i).getName().equals(name)) {
180 return fFields.get(i).getValue();
bcb8c2cb
PT
181 }
182 }
183 return null;
184 }
185
186 /**
e604cdf6 187 * Get a subfield value by index.
bcb8c2cb
PT
188 *
189 * @param index
190 * a subfield index
191 * @return field value object
192 */
193 public Object getFieldValue(int index) {
b742c196
AM
194 if (index >= 0 && index < fFields.size()) {
195 return fFields.get(index).getValue();
bcb8c2cb
PT
196 }
197 return null;
198 }
199
200 /**
e604cdf6 201 * Set the content value.
bcb8c2cb
PT
202 *
203 * @param value
204 * the content value
205 */
206 public void setValue(Object value) {
207 fValue = value;
208 }
209
210 /**
e604cdf6 211 * Set a subfield value by name. Adds the subfield if it is new.
bcb8c2cb
PT
212 *
213 * @param name
214 * a subfield name
215 * @param value
216 * the subfield value
217 */
e604cdf6
PT
218 public void setFieldValue(@NonNull String name, Object value) {
219 TextTraceEventContent field = null;
b742c196
AM
220 for (int i = 0; i < fFields.size(); i++) {
221 if (fFields.get(i).getName().equals(name)) {
e604cdf6
PT
222 field = fFields.get(i);
223 field.setValue(value);
bcb8c2cb
PT
224 }
225 }
e604cdf6
PT
226 if (field == null) {
227 field = new TextTraceEventContent(name);
228 field.setValue(value);
229 fFields.add(field);
230 }
bcb8c2cb
PT
231 }
232
233 /**
e604cdf6 234 * Set a subfield value by index.
bcb8c2cb
PT
235 *
236 * @param index
237 * a subfield index
238 * @param value
239 * the subfield value
240 */
241 public void setFieldValue(int index, Object value) {
b742c196
AM
242 if (index >= 0 && index < fFields.size()) {
243 fFields.get(index).fValue = value;
bcb8c2cb
PT
244 }
245 }
246
e604cdf6
PT
247 /**
248 * Add a new subfield unconditionally and set its value. Note: This can
249 * create a duplicate subfield. If the subfield already exists, use
250 * {@link #setFieldValue(String, Object)} instead.
251 *
252 * @param name
253 * a subfield name
254 * @param value
255 * the subfield value
dbc7991d 256 * @since 1.0
e604cdf6
PT
257 */
258 public void addField(@NonNull String name, Object value) {
259 TextTraceEventContent field = new TextTraceEventContent(name);
260 field.setValue(value);
261 fFields.add(field);
262 }
263
bcb8c2cb
PT
264 // ------------------------------------------------------------------------
265 // Object
266 // ------------------------------------------------------------------------
267
268 @Override
269 public int hashCode() {
270 final int prime = 31;
271 int result = 1;
b742c196
AM
272 result = prime * result + fFields.hashCode();
273 result = prime * result + fName.hashCode();
bcb8c2cb 274 int tmpHash = 0; // initialize for fValue equals null;
b742c196
AM
275 Object value = fValue;
276 if (value != null) {
277 if (value instanceof StringBuffer) {
278 tmpHash = value.toString().hashCode();
bcb8c2cb 279 } else {
b742c196 280 tmpHash = value.hashCode();
bcb8c2cb
PT
281 }
282 }
283 result = prime * result + tmpHash;
284 return result;
285 }
286
287 @Override
288 public boolean equals(Object obj) {
289 if (this == obj) {
290 return true;
291 }
292 if (obj == null) {
293 return false;
294 }
295 if (getClass() != obj.getClass()) {
296 return false;
297 }
298 TextTraceEventContent other = (TextTraceEventContent) obj;
b742c196 299 if (!fFields.equals(other.fFields)) {
bcb8c2cb
PT
300 return false;
301 }
b742c196 302 if (!fName.equals(other.fName)) {
bcb8c2cb
PT
303 return false;
304 }
b742c196
AM
305
306 Object value = fValue;
307 if (value == null) {
bcb8c2cb
PT
308 if (other.fValue != null) {
309 return false;
310 }
311 } else {
b742c196
AM
312 if ((value instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) {
313 Object otherValue = other.getValue();
314 if (otherValue == null) {
315 return false;
316 }
317 if (!value.toString().equals(otherValue.toString())) {
bcb8c2cb
PT
318 return false;
319 }
b742c196 320 } else if (!value.equals(other.fValue)) {
bcb8c2cb
PT
321 return false;
322 }
323 }
324 return true;
325 }
326
327 @Override
328 public String toString() {
329 StringBuilder sb = new StringBuilder();
330 if (fName == ITmfEventField.ROOT_FIELD_ID) {
b742c196
AM
331 for (int i = 0; i < getFields().size(); i++) {
332 ITmfEventField field = getFields().get(i);
bcb8c2cb
PT
333 if (i != 0) {
334 sb.append(", "); //$NON-NLS-1$
335 }
336 sb.append(field.toString());
337 }
338 } else {
339 sb.append(fName);
340 sb.append('=');
341 sb.append(fValue);
342 }
343 return sb.toString();
344 }
345
346}
This page took 0.096963 seconds and 5 git commands to generate.