1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated equals, clone and hashCode to consider StringBuffer values
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.trace
.text
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collections
;
20 import java
.util
.List
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
27 * Implementation of ITmfEventField for Text Traces.
31 public class TextTraceEventContent
implements ITmfEventField
{
33 private final @NonNull String fName
;
34 private final @NonNull List
<TextTraceEventContent
> fFields
;
36 private @Nullable Object fValue
;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
43 * Constructor for a root event content
46 * the array of field names
48 public TextTraceEventContent(String
[] fieldNames
) {
49 fName
= ITmfEventField
.ROOT_FIELD_ID
;
51 fFields
= new ArrayList
<>(fieldNames
.length
);
52 for (String fieldName
: fieldNames
) {
53 if (fieldName
== null) {
54 throw new IllegalArgumentException("Null field name not allowed"); //$NON-NLS-1$
56 fFields
.add(new TextTraceEventContent(fieldName
));
61 * Constructor for a subfield
64 * the array of field names
66 private TextTraceEventContent(@NonNull String fieldName
) {
69 fFields
= checkNotNull(Collections
.EMPTY_LIST
);
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 public String
getName() {
82 public Object
getValue() {
87 public List
<String
> getFieldNames() {
88 List
<String
> fieldNames
= new ArrayList
<>(fFields
.size());
89 for (TextTraceEventContent field
: fFields
) {
90 fieldNames
.add(field
.getName());
96 public List
<TextTraceEventContent
> getFields() {
97 return new ArrayList
<>(fFields
);
101 public ITmfEventField
getField(String name
) {
102 for (TextTraceEventContent field
: fFields
) {
103 if (field
.getName().equals(name
)) {
111 public String
getFormattedValue() {
112 Object value
= fValue
;
116 return value
.toString();
120 public ITmfEventField
getSubField(String
... names
) {
121 // There are no sub fields
122 if (names
.length
== 1) {
123 return getField(names
[0]);
128 // ------------------------------------------------------------------------
129 // Convenience getters and setters
130 // ------------------------------------------------------------------------
133 * Get a field name by index
136 * The index of the field
137 * @return The name of the field at that index
139 public String
getFieldName(int index
) {
140 if (index
>= 0 && index
< fFields
.size()) {
141 return fFields
.get(index
).getName();
147 * Get a field by index
150 * The index of the field
151 * @return The field object at the requested index
153 public ITmfEventField
getField(int index
) {
154 if (index
>= 0 && index
< fFields
.size()) {
155 return fFields
.get(index
);
161 * Get a subfield value by name
165 * @return field value object
167 public Object
getFieldValue(String name
) {
168 for (int i
= 0; i
< fFields
.size(); i
++) {
169 if (fFields
.get(i
).getName().equals(name
)) {
170 return fFields
.get(i
).getValue();
177 * Get a subfield value by index
181 * @return field value object
183 public Object
getFieldValue(int index
) {
184 if (index
>= 0 && index
< fFields
.size()) {
185 return fFields
.get(index
).getValue();
191 * Set the content value
196 public void setValue(Object value
) {
201 * Set a subfield value by name
208 public void setFieldValue(String name
, Object value
) {
209 for (int i
= 0; i
< fFields
.size(); i
++) {
210 if (fFields
.get(i
).getName().equals(name
)) {
211 fFields
.get(i
).fValue
= value
;
217 * Set a subfield value by index
224 public void setFieldValue(int index
, Object value
) {
225 if (index
>= 0 && index
< fFields
.size()) {
226 fFields
.get(index
).fValue
= value
;
230 // ------------------------------------------------------------------------
232 // ------------------------------------------------------------------------
235 public int hashCode() {
236 final int prime
= 31;
238 result
= prime
* result
+ fFields
.hashCode();
239 result
= prime
* result
+ fName
.hashCode();
240 int tmpHash
= 0; // initialize for fValue equals null;
241 Object value
= fValue
;
243 if (value
instanceof StringBuffer
) {
244 tmpHash
= value
.toString().hashCode();
246 tmpHash
= value
.hashCode();
249 result
= prime
* result
+ tmpHash
;
254 public boolean equals(Object obj
) {
261 if (getClass() != obj
.getClass()) {
264 TextTraceEventContent other
= (TextTraceEventContent
) obj
;
265 if (!fFields
.equals(other
.fFields
)) {
268 if (!fName
.equals(other
.fName
)) {
272 Object value
= fValue
;
274 if (other
.fValue
!= null) {
278 if ((value
instanceof StringBuffer
) && (other
.fValue
instanceof StringBuffer
)) {
279 Object otherValue
= other
.getValue();
280 if (otherValue
== null) {
283 if (!value
.toString().equals(otherValue
.toString())) {
286 } else if (!value
.equals(other
.fValue
)) {
294 public String
toString() {
295 StringBuilder sb
= new StringBuilder();
296 if (fName
== ITmfEventField
.ROOT_FIELD_ID
) {
297 for (int i
= 0; i
< getFields().size(); i
++) {
298 ITmfEventField field
= getFields().get(i
);
300 sb
.append(", "); //$NON-NLS-1$
302 sb
.append(field
.toString());
309 return sb
.toString();