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
.linuxtools
.tmf
.core
.trace
.text
;
16 import java
.util
.Arrays
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
21 * Implementation of ITmfEventField for Text Traces.
25 public class TextTraceEventContent
implements ITmfEventField
, Cloneable
{
28 private Object fValue
;
29 private TextTraceEventContent
[] fFields
;
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
36 * Constructor for a root event content
39 * the array of field names
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
]);
50 * Constructor for a subfield
53 * the array of field names
55 private TextTraceEventContent(String fieldName
) {
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 public String
getName() {
69 public Object
getValue() {
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();
83 public String
getFieldName(int index
) {
84 if (index
>= 0 && index
< fFields
.length
) {
85 return fFields
[index
].getName();
91 public ITmfEventField
[] getFields() {
96 public ITmfEventField
getField(String name
) {
97 for (int i
= 0; i
< fFields
.length
; i
++) {
98 if (fFields
[i
].getName().equals(name
)) {
106 public ITmfEventField
getField(int index
) {
107 if (index
>= 0 && index
< fFields
.length
) {
108 return fFields
[index
];
114 public String
getFormattedValue() {
115 return fValue
.toString();
119 public ITmfEventField
getSubField(String
[] names
) {
120 ITmfEventField field
= this;
121 for (String name
: names
) {
122 field
= field
.getField(name
);
130 // ------------------------------------------------------------------------
131 // Convenience getters and setters
132 // ------------------------------------------------------------------------
135 * Get a subfield value by name
139 * @return field value object
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();
151 * Get a subfield value by index
155 * @return field value object
157 public Object
getFieldValue(int index
) {
158 if (index
>= 0 && index
< fFields
.length
) {
159 return fFields
[index
].getValue();
165 * Set the content value
170 public void setValue(Object value
) {
175 * Set a subfield value by name
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
;
191 * Set a subfield value by index
198 public void setFieldValue(int index
, Object value
) {
199 if (index
>= 0 && index
< fFields
.length
) {
200 fFields
[index
].fValue
= value
;
204 // ------------------------------------------------------------------------
206 // ------------------------------------------------------------------------
209 public TextTraceEventContent
clone() {
210 TextTraceEventContent clone
= null;
212 clone
= (TextTraceEventContent
) super.clone();
214 if (fValue
instanceof StringBuffer
) {
215 StringBuffer value
= new StringBuffer(fValue
.toString());
216 clone
.fValue
= value
;
218 clone
.fValue
= fValue
;
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();
226 } catch (CloneNotSupportedException e
) {
231 // ------------------------------------------------------------------------
233 // ------------------------------------------------------------------------
236 public int hashCode() {
237 final int prime
= 31;
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();
246 tmpHash
= fValue
.hashCode();
249 result
= prime
* result
+ tmpHash
;
254 public boolean equals(Object obj
) {
261 if (getClass() != obj
.getClass()) {
264 TextTraceEventContent other
= (TextTraceEventContent
) obj
;
265 if (!Arrays
.equals(fFields
, other
.fFields
)) {
269 if (other
.fName
!= null) {
272 } else if (!fName
.equals(other
.fName
)) {
275 if (fValue
== null) {
276 if (other
.fValue
!= null) {
280 if ((fValue
instanceof StringBuffer
) && (other
.fValue
instanceof StringBuffer
)) {
281 if (!fValue
.toString().equals(other
.fValue
.toString())) {
284 } else if (!fValue
.equals(other
.fValue
)) {
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
];
298 sb
.append(", "); //$NON-NLS-1$
300 sb
.append(field
.toString());
307 return sb
.toString();