Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / text / TextTraceEventContent.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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.tracecompass.tmf.core.trace.text;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
23
24 /**
25 * Implementation of ITmfEventField for Text Traces.
26 */
27 public class TextTraceEventContent implements ITmfEventField {
28
29 private final @NonNull String fName;
30 private final @NonNull List<TextTraceEventContent> fFields;
31
32 private @Nullable Object fValue;
33
34 // ------------------------------------------------------------------------
35 // Constructors
36 // ------------------------------------------------------------------------
37
38 /**
39 * Constructor for a root event content. Subfields with the specified field
40 * names are created and initialized with a null value.
41 *
42 * @param fieldNames
43 * the array of non-null field names
44 * @throws IllegalArgumentException
45 * if any one of the field names is null
46 */
47 public TextTraceEventContent(String @NonNull [] fieldNames) {
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));
56 }
57 }
58
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
65 * @since 1.0
66 */
67 public TextTraceEventContent(int initialCapacity) {
68 fName = ITmfEventField.ROOT_FIELD_ID;
69 fValue = null;
70 fFields = new ArrayList<>(initialCapacity);
71 }
72
73 /**
74 * Constructor for a subfield
75 *
76 * @param fieldName
77 * the subfield name
78 */
79 private TextTraceEventContent(@NonNull String fieldName) {
80 fName = fieldName;
81 fValue = null;
82 fFields = Collections.EMPTY_LIST;
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
100 public List<String> getFieldNames() {
101 List<String> fieldNames = new ArrayList<>(fFields.size());
102 for (TextTraceEventContent field : fFields) {
103 fieldNames.add(field.getName());
104 }
105 return fieldNames;
106 }
107
108 @Override
109 public List<TextTraceEventContent> getFields() {
110 return new ArrayList<>(fFields);
111 }
112
113 @Override
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 }
124 }
125 }
126 return null;
127 }
128
129 @Override
130 public String getFormattedValue() {
131 Object value = fValue;
132 if (value == null) {
133 return null;
134 }
135 return value.toString();
136 }
137
138 // ------------------------------------------------------------------------
139 // Convenience getters and setters
140 // ------------------------------------------------------------------------
141
142 /**
143 * Get a field name by index.
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 /**
157 * Get a field by index.
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
170 /**
171 * Get a subfield value by name.
172 *
173 * @param name
174 * a subfield name
175 * @return field value object
176 */
177 public Object getFieldValue(@NonNull String name) {
178 for (int i = 0; i < fFields.size(); i++) {
179 if (fFields.get(i).getName().equals(name)) {
180 return fFields.get(i).getValue();
181 }
182 }
183 return null;
184 }
185
186 /**
187 * Get a subfield value by index.
188 *
189 * @param index
190 * a subfield index
191 * @return field value object
192 */
193 public Object getFieldValue(int index) {
194 if (index >= 0 && index < fFields.size()) {
195 return fFields.get(index).getValue();
196 }
197 return null;
198 }
199
200 /**
201 * Set the content value.
202 *
203 * @param value
204 * the content value
205 */
206 public void setValue(Object value) {
207 fValue = value;
208 }
209
210 /**
211 * Set a subfield value by name. Adds the subfield if it is new.
212 *
213 * @param name
214 * a subfield name
215 * @param value
216 * the subfield value
217 */
218 public void setFieldValue(@NonNull String name, Object value) {
219 TextTraceEventContent field = null;
220 for (int i = 0; i < fFields.size(); i++) {
221 if (fFields.get(i).getName().equals(name)) {
222 field = fFields.get(i);
223 field.setValue(value);
224 }
225 }
226 if (field == null) {
227 field = new TextTraceEventContent(name);
228 field.setValue(value);
229 fFields.add(field);
230 }
231 }
232
233 /**
234 * Set a subfield value by index.
235 *
236 * @param index
237 * a subfield index
238 * @param value
239 * the subfield value
240 */
241 public void setFieldValue(int index, Object value) {
242 if (index >= 0 && index < fFields.size()) {
243 fFields.get(index).fValue = value;
244 }
245 }
246
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
256 * @since 1.0
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
264 // ------------------------------------------------------------------------
265 // Object
266 // ------------------------------------------------------------------------
267
268 @Override
269 public int hashCode() {
270 final int prime = 31;
271 int result = 1;
272 result = prime * result + fFields.hashCode();
273 result = prime * result + fName.hashCode();
274 int tmpHash = 0; // initialize for fValue equals null;
275 Object value = fValue;
276 if (value != null) {
277 if (value instanceof StringBuffer) {
278 tmpHash = value.toString().hashCode();
279 } else {
280 tmpHash = value.hashCode();
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;
299 if (!fFields.equals(other.fFields)) {
300 return false;
301 }
302 if (!fName.equals(other.fName)) {
303 return false;
304 }
305
306 Object value = fValue;
307 if (value == null) {
308 if (other.fValue != null) {
309 return false;
310 }
311 } else {
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())) {
318 return false;
319 }
320 } else if (!value.equals(other.fValue)) {
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) {
331 for (int i = 0; i < getFields().size(); i++) {
332 ITmfEventField field = getFields().get(i);
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.055424 seconds and 6 git commands to generate.