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 fields the list of subfields
57 public TmfEventField(final String name
, final 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(final String name
, final Object value
) {
68 this(name
, value
, null);
74 * @param name the event field id
75 * @param value the event field value
76 * @param fields the list of subfields
78 public TmfEventField(final String name
, final Object value
, final ITmfEventField
[] fields
) {
80 throw new IllegalArgumentException();
83 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
90 * @param field the other event field
92 public TmfEventField(final TmfEventField field
) {
94 throw new IllegalArgumentException();
96 fValue
= field
.fValue
;
97 fFields
= field
.fFields
;
98 fFieldNames
= field
.fFieldNames
;
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
110 public String
getName() {
115 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
118 public Object
getValue() {
123 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
126 public String
[] getFieldNames() {
127 return Arrays
.copyOf(fFieldNames
, fFieldNames
.length
);
131 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
134 public String
getFieldName(final int index
) {
135 final ITmfEventField field
= getField(index
);
137 return field
.getName();
142 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
145 public ITmfEventField
[] getFields() {
146 return (fFields
!= null) ? Arrays
.copyOf(fFields
, fFields
.length
) : null;
150 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
153 public ITmfEventField
getField(final String name
) {
154 return fNameMapping
.get(name
);
158 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
161 public ITmfEventField
getField(final int index
) {
162 if (fFields
!= null && index
>= 0 && index
< fFields
.length
)
163 return fFields
[index
];
167 // ------------------------------------------------------------------------
168 // Convenience setters
169 // ------------------------------------------------------------------------
172 * @param value new field raw value
173 * @param fields the corresponding fields
175 protected void setValue(final Object value
, final ITmfEventField
[] fields
) {
177 fFields
= (fields
!= null) ? Arrays
.copyOf(fields
, fields
.length
) : null;
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * Create a root field from a list of labels.
188 * @param labels the list of labels
189 * @return the (flat) root list
191 public final static ITmfEventField
makeRoot(final String
[] labels
) {
192 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
193 for (int i
= 0; i
< labels
.length
; i
++)
194 fields
[i
] = new TmfEventField(labels
[i
], null);
195 final ITmfEventField rootField
= new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, fields
);
200 * Populate the subfield names and the name map
202 private void populateStructs() {
203 final int nbFields
= (fFields
!= null) ? fFields
.length
: 0;
204 fFieldNames
= new String
[nbFields
];
205 fNameMapping
= new HashMap
<String
, ITmfEventField
>();
206 for (int i
= 0; i
< nbFields
; i
++) {
207 final String name
= fFields
[i
].getName();
208 fFieldNames
[i
] = name
;
209 fNameMapping
.put(name
, fFields
[i
]);
213 // ------------------------------------------------------------------------
215 // ------------------------------------------------------------------------
218 * @see java.lang.Object#clone()
221 public TmfEventField
clone() {
222 TmfEventField clone
= null;
224 clone
= (TmfEventField
) super.clone();
226 clone
.fValue
= fValue
;
227 clone
.fFields
= (fFields
!= null) ? fFields
.clone() : null;
228 clone
.populateStructs();
229 } catch (final CloneNotSupportedException e
) {
234 // ------------------------------------------------------------------------
236 // ------------------------------------------------------------------------
239 * @see java.lang.Object#hashCode()
242 public int hashCode() {
243 final int prime
= 31;
245 result
= prime
* result
+ fName
.hashCode();
246 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
251 * @see java.lang.Object#equals(java.lang.Object)
254 public boolean equals(final Object obj
) {
259 if (!(obj
instanceof TmfEventField
))
261 final TmfEventField other
= (TmfEventField
) obj
;
262 if (!fName
.equals(other
.fName
))
264 if (fValue
== null) {
265 if (other
.fValue
!= null)
267 } else if (!fValue
.equals(other
.fValue
))
273 * @see java.lang.Object#toString()
276 @SuppressWarnings("nls")
277 public String
toString() {
278 return "TmfEventField [fFieldId=" + fName
+ ", fValue=" + fValue
+ "]";