1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Event Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
16 import java
.util
.Arrays
;
17 import java
.util
.HashMap
;
21 * A basic implementation of ITmfEventField.
23 * Non-value fields are structural (i.e. used to represent the event structure
24 * including optional fields) while the valued fields are actual event fields.
27 * @author Francois Chouinard
32 public class TmfEventField
implements ITmfEventField
, Cloneable
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
39 private Object fValue
;
40 private ITmfEventField
[] fFields
;
42 private String
[] fFieldNames
;
43 private Map
<String
, ITmfEventField
> fNameMapping
;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
52 @SuppressWarnings("unused")
53 private TmfEventField() {
57 * Constructor for a structural field
59 * @param name the event field id
60 * @param fields the list of subfields
62 public TmfEventField(final String name
, final ITmfEventField
[] fields
) {
63 this(name
, null, fields
);
67 * Constructor for a terminal field (no subfields)
69 * @param name the event field id
70 * @param value the event field value
72 public TmfEventField(final String name
, final Object value
) {
73 this(name
, value
, null);
79 * @param name the event field id
80 * @param value the event field value
81 * @param fields the list of subfields
83 public TmfEventField(final String name
, final Object value
, final ITmfEventField
[] fields
) {
85 throw new IllegalArgumentException();
89 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
96 * @param field the other event field
98 public TmfEventField(final TmfEventField field
) {
100 throw new IllegalArgumentException();
103 fValue
= field
.fValue
;
104 fFields
= field
.fFields
;
105 fFieldNames
= field
.fFieldNames
;
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
114 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
117 public String
getName() {
122 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
125 public Object
getValue() {
130 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
133 public String
[] getFieldNames() {
134 return Arrays
.copyOf(fFieldNames
, fFieldNames
.length
);
138 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
141 public String
getFieldName(final int index
) {
142 final ITmfEventField field
= getField(index
);
144 return field
.getName();
150 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
153 public ITmfEventField
[] getFields() {
154 return (fFields
!= null) ? Arrays
.copyOf(fFields
, fFields
.length
) : new ITmfEventField
[0];
158 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
161 public ITmfEventField
getField(final String name
) {
162 return fNameMapping
.get(name
);
166 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
169 public ITmfEventField
getField(final int index
) {
170 if (fFields
!= null && index
>= 0 && index
< fFields
.length
) {
171 return fFields
[index
];
176 // ------------------------------------------------------------------------
177 // Convenience setters
178 // ------------------------------------------------------------------------
181 * @param value new field raw value
182 * @param fields the corresponding fields
184 protected void setValue(final Object value
, final ITmfEventField
[] fields
) {
186 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
195 * Create a root field from a list of labels.
197 * @param labels the list of labels
198 * @return the (flat) root list
200 public final static ITmfEventField
makeRoot(final String
[] labels
) {
201 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
202 for (int i
= 0; i
< labels
.length
; i
++) {
203 fields
[i
] = new TmfEventField(labels
[i
], null);
205 // Return a new root field;
206 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, fields
);
210 * Populate the subfield names and the name map
212 private void populateStructs() {
213 final int nbFields
= (fFields
!= null) ? fFields
.length
: 0;
214 fFieldNames
= new String
[nbFields
];
215 fNameMapping
= new HashMap
<String
, ITmfEventField
>();
216 for (int i
= 0; i
< nbFields
; i
++) {
217 final String name
= fFields
[i
].getName();
218 fFieldNames
[i
] = name
;
219 fNameMapping
.put(name
, fFields
[i
]);
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
228 * @see java.lang.Object#clone()
231 public TmfEventField
clone() {
232 TmfEventField clone
= null;
234 clone
= (TmfEventField
) super.clone();
236 clone
.fValue
= fValue
;
237 clone
.fFields
= (fFields
!= null) ? fFields
.clone() : null;
238 clone
.populateStructs();
239 } catch (final CloneNotSupportedException e
) {
244 // ------------------------------------------------------------------------
246 // ------------------------------------------------------------------------
249 * @see java.lang.Object#hashCode()
252 public int hashCode() {
253 final int prime
= 31;
255 result
= prime
* result
+ fName
.hashCode();
256 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
261 * @see java.lang.Object#equals(java.lang.Object)
264 public boolean equals(final Object obj
) {
271 if (!(obj
instanceof TmfEventField
)) {
274 final TmfEventField other
= (TmfEventField
) obj
;
275 if (!fName
.equals(other
.fName
)) {
278 if (fValue
== null) {
279 if (other
.fValue
!= null) {
282 } else if (!fValue
.equals(other
.fValue
)) {
289 * @see java.lang.Object#toString()
292 public String
toString() {
293 StringBuilder ret
= new StringBuilder();
294 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
296 * If this field is a top-level "field container", we will print its
297 * sub-fields directly.
299 appendSubFields(ret
);
302 /* The field has its own values */
307 if (fFields
!= null && fFields
.length
> 0) {
309 * In addition to its own name/value, this field also has
312 ret
.append(" ["); //$NON-NLS-1$
313 appendSubFields(ret
);
317 return ret
.toString();
320 private void appendSubFields(StringBuilder sb
) {
321 ITmfEventField field
;
322 for (int i
= 0; i
< getFields().length
; i
++) {
323 field
= getFields()[i
];
325 sb
.append(", ");//$NON-NLS-1$
327 sb
.append(field
.toString());