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.
28 * @author Francois Chouinard
33 public class TmfEventField
implements ITmfEventField
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 private Object fValue
;
41 private ITmfEventField
[] fFields
;
43 private String
[] fFieldNames
;
44 private Map
<String
, ITmfEventField
> fNameMapping
;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
53 @SuppressWarnings("unused")
54 private TmfEventField() {
58 * Constructor for a structural field
60 * @param name the event field id
61 * @param fields the list of subfields
63 public TmfEventField(final String name
, final ITmfEventField
[] fields
) {
64 this(name
, null, fields
);
68 * Constructor for a terminal field (no subfields)
70 * @param name the event field id
71 * @param value the event field value
73 public TmfEventField(final String name
, final Object value
) {
74 this(name
, value
, null);
80 * @param name the event field id
81 * @param value the event field value
82 * @param fields the list of subfields
84 public TmfEventField(final String name
, final Object value
, final ITmfEventField
[] fields
) {
86 throw new IllegalArgumentException();
90 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
97 * @param field the other event field
99 public TmfEventField(final TmfEventField field
) {
101 throw new IllegalArgumentException();
104 fValue
= field
.fValue
;
105 fFields
= field
.fFields
;
106 fFieldNames
= field
.fFieldNames
;
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
115 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
118 public String
getName() {
123 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
126 public Object
getValue() {
131 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
134 public String
[] getFieldNames() {
135 return Arrays
.copyOf(fFieldNames
, fFieldNames
.length
);
139 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
142 public String
getFieldName(final int index
) {
143 final ITmfEventField field
= getField(index
);
145 return field
.getName();
151 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
154 public ITmfEventField
[] getFields() {
155 return (fFields
!= null) ? Arrays
.copyOf(fFields
, fFields
.length
) : null;
159 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
162 public ITmfEventField
getField(final String name
) {
163 return fNameMapping
.get(name
);
167 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
170 public ITmfEventField
getField(final int index
) {
171 if (fFields
!= null && index
>= 0 && index
< fFields
.length
) {
172 return fFields
[index
];
177 // ------------------------------------------------------------------------
178 // Convenience setters
179 // ------------------------------------------------------------------------
182 * @param value new field raw value
183 * @param fields the corresponding fields
185 protected void setValue(final Object value
, final ITmfEventField
[] fields
) {
187 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
196 * Create a root field from a list of labels.
198 * @param labels the list of labels
199 * @return the (flat) root list
201 public final static ITmfEventField
makeRoot(final String
[] labels
) {
202 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
203 for (int i
= 0; i
< labels
.length
; i
++) {
204 fields
[i
] = new TmfEventField(labels
[i
], null);
206 // Return a new root field;
207 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, fields
);
211 * Populate the subfield names and the name map
213 private void populateStructs() {
214 final int nbFields
= (fFields
!= null) ? fFields
.length
: 0;
215 fFieldNames
= new String
[nbFields
];
216 fNameMapping
= new HashMap
<String
, ITmfEventField
>();
217 for (int i
= 0; i
< nbFields
; i
++) {
218 final String name
= fFields
[i
].getName();
219 fFieldNames
[i
] = name
;
220 fNameMapping
.put(name
, fFields
[i
]);
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
229 * @see java.lang.Object#clone()
232 public TmfEventField
clone() {
233 TmfEventField clone
= null;
235 clone
= (TmfEventField
) super.clone();
237 clone
.fValue
= fValue
;
238 clone
.fFields
= (fFields
!= null) ? fFields
.clone() : null;
239 clone
.populateStructs();
240 } catch (final CloneNotSupportedException e
) {
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 * @see java.lang.Object#hashCode()
253 public int hashCode() {
254 final int prime
= 31;
256 result
= prime
* result
+ fName
.hashCode();
257 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
262 * @see java.lang.Object#equals(java.lang.Object)
265 public boolean equals(final Object obj
) {
272 if (!(obj
instanceof TmfEventField
)) {
275 final TmfEventField other
= (TmfEventField
) obj
;
276 if (!fName
.equals(other
.fName
)) {
279 if (fValue
== null) {
280 if (other
.fValue
!= null) {
283 } else if (!fValue
.equals(other
.fValue
)) {
290 * @see java.lang.Object#toString()
293 @SuppressWarnings("nls")
294 public String
toString() {
295 return "TmfEventField [fFieldId=" + fName
+ ", fValue=" + fValue
+ "]";