1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Alexandre Montplaisir - Removed Cloneable, made immutable
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.event
;
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
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final String fName
;
39 private final Object fValue
;
40 private final ITmfEventField
[] fFields
;
42 private final String
[] fFieldNames
;
43 private final Map
<String
, ITmfEventField
> fNameMapping
;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
52 * @param name the event field id
53 * @param value the event field value
54 * @param fields the list of subfields
56 public TmfEventField(final String name
, final Object value
, final ITmfEventField
[] fields
) {
58 throw new IllegalArgumentException();
64 /* Fill the fFieldNames and fNameMapping structures */
65 final int nbFields
= (fFields
!= null) ? fFields
.length
: 0;
66 fFieldNames
= new String
[nbFields
];
67 fNameMapping
= new HashMap
<String
, ITmfEventField
>();
69 for (int i
= 0; i
< nbFields
; i
++) {
70 final String curName
= fFields
[i
].getName();
71 fFieldNames
[i
] = curName
;
72 fNameMapping
.put(curName
, fFields
[i
]);
79 * @param field the other event field
81 public TmfEventField(final TmfEventField field
) {
83 throw new IllegalArgumentException();
86 fValue
= field
.fValue
;
87 fFields
= field
.fFields
;
88 fFieldNames
= field
.fFieldNames
;
89 fNameMapping
= field
.fNameMapping
;
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
97 public String
getName() {
102 public Object
getValue() {
107 public String
[] getFieldNames() {
112 public String
getFieldName(final int index
) {
113 final ITmfEventField field
= getField(index
);
115 return field
.getName();
121 public ITmfEventField
[] getFields() {
122 return (fFields
!= null) ? fFields
: new ITmfEventField
[0];
126 public ITmfEventField
getField(final String name
) {
127 return fNameMapping
.get(name
);
131 public ITmfEventField
getField(final int index
) {
132 if (fFields
!= null && index
>= 0 && index
< fFields
.length
) {
133 return fFields
[index
];
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * Create a root field from a list of labels.
145 * @param labels the list of labels
146 * @return the (flat) root list
148 public final static ITmfEventField
makeRoot(final String
[] labels
) {
149 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
150 for (int i
= 0; i
< labels
.length
; i
++) {
151 fields
[i
] = new TmfEventField(labels
[i
], null, null);
153 // Return a new root field;
154 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, null, fields
);
157 // ------------------------------------------------------------------------
159 // ------------------------------------------------------------------------
162 public int hashCode() {
163 final int prime
= 31;
165 result
= prime
* result
+ fName
.hashCode();
166 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
171 public boolean equals(final Object obj
) {
178 if (!(obj
instanceof TmfEventField
)) {
181 final TmfEventField other
= (TmfEventField
) obj
;
182 if (!fName
.equals(other
.fName
)) {
185 if (fValue
== null) {
186 if (other
.fValue
!= null) {
189 } else if (!fValue
.equals(other
.fValue
)) {
196 public String
toString() {
197 StringBuilder ret
= new StringBuilder();
198 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
200 * If this field is a top-level "field container", we will print its
201 * sub-fields directly.
203 appendSubFields(ret
);
206 /* The field has its own values */
211 if (fFields
!= null && fFields
.length
> 0) {
213 * In addition to its own name/value, this field also has
216 ret
.append(" ["); //$NON-NLS-1$
217 appendSubFields(ret
);
221 return ret
.toString();
224 private void appendSubFields(StringBuilder sb
) {
225 ITmfEventField field
;
226 for (int i
= 0; i
< getFields().length
; i
++) {
227 field
= getFields()[i
];
229 sb
.append(", ");//$NON-NLS-1$
231 sb
.append(field
.toString());
239 public String
getFormattedValue() {
240 return getValue().toString();
This page took 0.036347 seconds and 5 git commands to generate.