1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 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 * Patrick Tasse - Remove getSubField
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.core
.event
;
18 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
20 import java
.util
.Collection
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import com
.google
.common
.base
.Joiner
;
27 import com
.google
.common
.collect
.ImmutableMap
;
30 * A basic implementation of ITmfEventField.
32 * Non-value fields are structural (i.e. used to represent the event structure
33 * including optional fields) while the valued fields are actual event fields.
36 * @author Francois Chouinard
41 public class TmfEventField
implements ITmfEventField
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private final @NonNull String fName
;
48 private final @Nullable Object fValue
;
49 private final @NonNull Map
<String
, ITmfEventField
> fFields
;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
61 * the event field value
63 * the list of subfields
64 * @throws IllegalArgumentException
65 * If 'name' is null, or if 'fields' has duplicate field names.
67 public TmfEventField(@NonNull String name
, @Nullable Object value
, @Nullable ITmfEventField
[] fields
) {
72 fFields
= checkNotNull(ImmutableMap
.<String
, ITmfEventField
> of());
74 /* Java 8 streams will make this even more simple! */
75 ImmutableMap
.Builder
<String
, ITmfEventField
> mapBuilder
= new ImmutableMap
.Builder
<>();
76 for (ITmfEventField field
: fields
) {
77 final String curName
= field
.getName();
78 mapBuilder
.put(curName
, field
);
80 fFields
= checkNotNull(mapBuilder
.build());
87 * @param field the other event field
89 public TmfEventField(final TmfEventField field
) {
91 throw new IllegalArgumentException();
94 fValue
= field
.fValue
;
95 fFields
= field
.fFields
;
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 public String
getName() {
108 public Object
getValue() {
113 public Collection
<String
> getFieldNames() {
114 return checkNotNull(fFields
.keySet());
118 public Collection
<ITmfEventField
> getFields() {
119 return checkNotNull(fFields
.values());
123 public ITmfEventField
getField(final String
... path
) {
124 if (path
.length
== 1) {
125 return fFields
.get(path
[0]);
127 ITmfEventField field
= this;
128 for (String name
: path
) {
129 field
= field
.getField(name
);
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 * Create a root field from a list of labels.
144 * @param labels the list of labels
145 * @return the (flat) root list
147 public static final ITmfEventField
makeRoot(final String
[] labels
) {
148 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
149 for (int i
= 0; i
< labels
.length
; i
++) {
150 String label
= checkNotNull(labels
[i
]);
151 fields
[i
] = new TmfEventField(label
, 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 Object value
= fValue
;
164 final int prime
= 31;
166 result
= prime
* result
+ fName
.hashCode();
167 result
= prime
* result
+ ((value
== null) ?
0 : value
.hashCode());
168 result
= prime
* result
+ fFields
.hashCode();
173 public boolean equals(final Object obj
) {
180 if (!(obj
instanceof TmfEventField
)) {
184 final TmfEventField other
= (TmfEventField
) obj
;
186 /* Check that 'fName' is the same */
187 if (!fName
.equals(other
.fName
)) {
191 /* Check that 'fValue' is the same */
192 Object value
= this.fValue
;
194 if (other
.fValue
!= null) {
197 } else if (!value
.equals(other
.fValue
)) {
201 /* Check that 'fFields' are the same */
202 if (!fFields
.equals(other
.fFields
)) {
210 public String
toString() {
211 StringBuilder ret
= new StringBuilder();
212 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
214 * If this field is a top-level "field container", we will print its
215 * sub-fields directly.
217 appendSubFields(ret
);
220 /* The field has its own values */
225 if (!fFields
.isEmpty()) {
227 * In addition to its own name/value, this field also has
230 ret
.append(" ["); //$NON-NLS-1$
231 appendSubFields(ret
);
235 return ret
.toString();
238 private void appendSubFields(StringBuilder sb
) {
239 Joiner joiner
= Joiner
.on(", ").skipNulls(); //$NON-NLS-1$
240 sb
.append(joiner
.join(getFields()));
244 public String
getFormattedValue() {
245 return getValue().toString();
This page took 0.037428 seconds and 5 git commands to generate.