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 * <b><u>TmfEventField</u></b>
23 * A basic implementation of ITmfEventField. Non-value fields are structural
24 * (i.e. used to represent the event structure including optional fields) while
25 * the valued fields are actual event fields.
27 public class TmfEventField
implements ITmfEventField
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 private Object fValue
;
35 private ITmfEventField
[] fFields
;
37 private String
[] fFieldNames
;
38 private Map
<String
, ITmfEventField
> fNameMapping
;
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
47 @SuppressWarnings("unused")
48 private TmfEventField() {
52 * Constructor for a structural field
54 * @param name the event field id
55 * @param subfields the list of subfields
57 public TmfEventField(String name
, ITmfEventField
[] fields
) {
58 this(name
, null, fields
);
62 * Constructor for a terminal field (i.e. no subfields)
64 * @param name the event field id
65 * @param value the event field value
67 public TmfEventField(String name
, Object value
) {
68 this(name
, value
, null);
74 * @param name the event field id
75 * @param value the event field value
76 * @param subfields the list of subfields
78 public TmfEventField(String name
, Object value
, ITmfEventField
[] fields
) {
80 throw new IllegalArgumentException();
84 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
91 * @param field the other event field
93 public TmfEventField(TmfEventField field
) {
95 throw new IllegalArgumentException();
97 fValue
= field
.fValue
;
98 fFields
= field
.fFields
;
99 fFieldNames
= field
.fFieldNames
;
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
111 public String
getName() {
116 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
119 public Object
getValue() {
124 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
127 public String
[] getFieldNames() {
128 return Arrays
.copyOf(fFieldNames
, fFieldNames
.length
);
132 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
135 public String
getFieldName(int index
) {
136 ITmfEventField field
= getField(index
);
138 return field
.getName();
144 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
147 public ITmfEventField
[] getFields() {
148 return (fFields
!= null) ? Arrays
.copyOf(fFields
, fFields
.length
) : null;
152 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
155 public ITmfEventField
getField(String name
) {
156 return fNameMapping
.get(name
);
160 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
163 public ITmfEventField
getField(int index
) {
164 if (fFields
!= null && index
>= 0 && index
< fFields
.length
)
165 return fFields
[index
];
169 // ------------------------------------------------------------------------
170 // Convenience setters
171 // ------------------------------------------------------------------------
174 * @param value new field raw value
175 * @param fields the corresponding fields
177 protected void setValue(Object value
, ITmfEventField
[] fields
) {
179 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
188 * Create a root field from a list of labels.
190 * @param labels the list of labels
191 * @return the (flat) root list
193 public final static ITmfEventField
makeRoot(String
[] labels
) {
194 ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
195 for (int i
= 0; i
< labels
.length
; i
++) {
196 fields
[i
] = new TmfEventField(labels
[i
], null);
198 ITmfEventField rootField
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, fields
);
203 * Populate the subfield names and the name map
205 private void populateStructs() {
206 int nbFields
= (fFields
!= null) ? fFields
.length
: 0;
207 fFieldNames
= new String
[nbFields
];
208 fNameMapping
= new HashMap
<String
, ITmfEventField
>();
209 for (int i
= 0; i
< nbFields
; i
++) {
210 String name
= fFields
[i
].getName();
211 fFieldNames
[i
] = name
;
212 fNameMapping
.put(name
, fFields
[i
]);
216 // ------------------------------------------------------------------------
218 // ------------------------------------------------------------------------
221 * @see java.lang.Object#clone()
224 public TmfEventField
clone() {
225 TmfEventField clone
= null;
227 clone
= (TmfEventField
) super.clone();
229 clone
.fValue
= fValue
;
230 clone
.fFields
= (fFields
!= null) ? fFields
.clone() : null;
231 clone
.populateStructs();
232 } catch (CloneNotSupportedException e
) {
237 // ------------------------------------------------------------------------
239 // ------------------------------------------------------------------------
242 * @see java.lang.Object#hashCode()
245 public int hashCode() {
246 final int prime
= 31;
248 result
= prime
* result
+ fName
.hashCode();
249 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
254 * @see java.lang.Object#equals(java.lang.Object)
257 public boolean equals(Object obj
) {
262 if (!(obj
instanceof TmfEventField
))
264 TmfEventField other
= (TmfEventField
) obj
;
265 if (!fName
.equals(other
.fName
))
267 if (fValue
== null) {
268 if (other
.fValue
!= null)
270 } else if (!fValue
.equals(other
.fValue
))
276 * @see java.lang.Object#toString()
279 @SuppressWarnings("nls")
280 public String
toString() {
281 return "TmfEventField [fFieldId=" + fName
+ ", fValue=" + fValue
+ "]";