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 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
100 public String
getName() {
105 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
108 public Object
getValue() {
113 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
116 public String
[] getFieldNames() {
121 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
124 public String
getFieldName(final int index
) {
125 final ITmfEventField field
= getField(index
);
127 return field
.getName();
133 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
136 public ITmfEventField
[] getFields() {
137 return (fFields
!= null) ? fFields
: new ITmfEventField
[0];
141 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
144 public ITmfEventField
getField(final String name
) {
145 return fNameMapping
.get(name
);
149 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
152 public ITmfEventField
getField(final int index
) {
153 if (fFields
!= null && index
>= 0 && index
< fFields
.length
) {
154 return fFields
[index
];
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 * Create a root field from a list of labels.
166 * @param labels the list of labels
167 * @return the (flat) root list
169 public final static ITmfEventField
makeRoot(final String
[] labels
) {
170 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
171 for (int i
= 0; i
< labels
.length
; i
++) {
172 fields
[i
] = new TmfEventField(labels
[i
], null, null);
174 // Return a new root field;
175 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, null, fields
);
178 // ------------------------------------------------------------------------
180 // ------------------------------------------------------------------------
183 * @see java.lang.Object#hashCode()
186 public int hashCode() {
187 final int prime
= 31;
189 result
= prime
* result
+ fName
.hashCode();
190 result
= prime
* result
+ ((fValue
== null) ?
0 : fValue
.hashCode());
195 * @see java.lang.Object#equals(java.lang.Object)
198 public boolean equals(final Object obj
) {
205 if (!(obj
instanceof TmfEventField
)) {
208 final TmfEventField other
= (TmfEventField
) obj
;
209 if (!fName
.equals(other
.fName
)) {
212 if (fValue
== null) {
213 if (other
.fValue
!= null) {
216 } else if (!fValue
.equals(other
.fValue
)) {
223 * @see java.lang.Object#toString()
226 public String
toString() {
227 StringBuilder ret
= new StringBuilder();
228 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
230 * If this field is a top-level "field container", we will print its
231 * sub-fields directly.
233 appendSubFields(ret
);
236 /* The field has its own values */
241 if (fFields
!= null && fFields
.length
> 0) {
243 * In addition to its own name/value, this field also has
246 ret
.append(" ["); //$NON-NLS-1$
247 appendSubFields(ret
);
251 return ret
.toString();
254 private void appendSubFields(StringBuilder sb
) {
255 ITmfEventField field
;
256 for (int i
= 0; i
< getFields().length
; i
++) {
257 field
= getFields()[i
];
259 sb
.append(", ");//$NON-NLS-1$
261 sb
.append(field
.toString());
269 public String
getFormattedValue() {
270 return getValue().toString();
This page took 0.036293 seconds and 5 git commands to generate.