05dbec9e76fba7eafda9a67bf1da45a7d2c30c4f
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(String name
, @Nullable Object value
, @Nullable ITmfEventField
[] fields
) {
69 throw new IllegalArgumentException();
75 fFields
= checkNotNull(ImmutableMap
.<String
, ITmfEventField
> of());
77 /* Java 8 streams will make this even more simple! */
78 ImmutableMap
.Builder
<String
, ITmfEventField
> mapBuilder
= new ImmutableMap
.Builder
<>();
79 for (ITmfEventField field
: fields
) {
80 final String curName
= field
.getName();
81 mapBuilder
.put(curName
, field
);
83 fFields
= checkNotNull(mapBuilder
.build());
90 * @param field the other event field
92 public TmfEventField(final TmfEventField field
) {
94 throw new IllegalArgumentException();
97 fValue
= field
.fValue
;
98 fFields
= field
.fFields
;
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 public String
getName() {
111 public Object
getValue() {
116 public Collection
<String
> getFieldNames() {
117 return fFields
.keySet();
121 public Collection
<ITmfEventField
> getFields() {
122 return fFields
.values();
126 public ITmfEventField
getField(final String
... path
) {
127 if (path
.length
== 1) {
128 return fFields
.get(path
[0]);
130 ITmfEventField field
= this;
131 for (String name
: path
) {
132 field
= field
.getField(name
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Create a root field from a list of labels.
147 * @param labels the list of labels
148 * @return the (flat) root list
150 public static final ITmfEventField
makeRoot(final String
[] labels
) {
151 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
152 for (int i
= 0; i
< labels
.length
; i
++) {
153 fields
[i
] = new TmfEventField(labels
[i
], null, null);
155 // Return a new root field;
156 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, null, fields
);
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 public int hashCode() {
165 Object value
= fValue
;
166 final int prime
= 31;
168 result
= prime
* result
+ fName
.hashCode();
169 result
= prime
* result
+ ((value
== null) ?
0 : value
.hashCode());
170 result
= prime
* result
+ fFields
.hashCode();
175 public boolean equals(final Object obj
) {
182 if (!(obj
instanceof TmfEventField
)) {
186 final TmfEventField other
= (TmfEventField
) obj
;
188 /* Check that 'fName' is the same */
189 if (!fName
.equals(other
.fName
)) {
193 /* Check that 'fValue' is the same */
194 Object value
= this.fValue
;
196 if (other
.fValue
!= null) {
199 } else if (!value
.equals(other
.fValue
)) {
203 /* Check that 'fFields' are the same */
204 if (!fFields
.equals(other
.fFields
)) {
212 public String
toString() {
213 StringBuilder ret
= new StringBuilder();
214 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
216 * If this field is a top-level "field container", we will print its
217 * sub-fields directly.
219 appendSubFields(ret
);
222 /* The field has its own values */
227 if (!fFields
.isEmpty()) {
229 * In addition to its own name/value, this field also has
232 ret
.append(" ["); //$NON-NLS-1$
233 appendSubFields(ret
);
237 return ret
.toString();
240 private void appendSubFields(StringBuilder sb
) {
241 Joiner joiner
= Joiner
.on(", ").skipNulls(); //$NON-NLS-1$
242 sb
.append(joiner
.join(getFields()));
246 public String
getFormattedValue() {
247 return getValue().toString();
This page took 0.048051 seconds and 4 git commands to generate.