1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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
.Collection
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import com
.google
.common
.base
.Joiner
;
23 import com
.google
.common
.collect
.ImmutableMap
;
26 * A basic implementation of ITmfEventField.
28 * Non-value fields are structural (i.e. used to represent the event structure
29 * including optional fields) while the valued fields are actual event fields.
32 * @author Francois Chouinard
37 public class TmfEventField
implements ITmfEventField
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private final @NonNull String fName
;
44 private final @Nullable Object fValue
;
45 private final @NonNull ImmutableMap
<String
, ITmfEventField
> fFields
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
57 * the event field value
59 * the list of subfields
60 * @throws IllegalArgumentException
61 * If 'name' is null, or if 'fields' has duplicate field names.
63 @SuppressWarnings("null") /* ImmutableMap methods do not return @NonNull */
64 public TmfEventField(String name
, @Nullable Object value
, @Nullable ITmfEventField
[] fields
) {
66 throw new IllegalArgumentException();
72 fFields
= ImmutableMap
.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
= 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() {
116 public Collection
<String
> getFieldNames() {
117 return fFields
.keySet();
124 public Collection
<ITmfEventField
> getFields() {
125 return fFields
.values();
129 public ITmfEventField
getField(final String name
) {
130 return fFields
.get(name
);
137 public ITmfEventField
getSubField(final String
... names
) {
138 ITmfEventField field
= this;
139 for (String name
: names
) {
140 field
= field
.getField(name
);
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Create a root field from a list of labels.
155 * @param labels the list of labels
156 * @return the (flat) root list
158 public final static ITmfEventField
makeRoot(final String
[] labels
) {
159 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
160 for (int i
= 0; i
< labels
.length
; i
++) {
161 fields
[i
] = new TmfEventField(labels
[i
], null, null);
163 // Return a new root field;
164 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, null, fields
);
167 // ------------------------------------------------------------------------
169 // ------------------------------------------------------------------------
172 public int hashCode() {
173 Object value
= fValue
;
174 final int prime
= 31;
176 result
= prime
* result
+ fName
.hashCode();
177 result
= prime
* result
+ ((value
== null) ?
0 : value
.hashCode());
178 result
= prime
* result
+ fFields
.hashCode();
183 public boolean equals(final Object obj
) {
190 if (!(obj
instanceof TmfEventField
)) {
194 final TmfEventField other
= (TmfEventField
) obj
;
196 /* Check that 'fName' is the same */
197 if (!fName
.equals(other
.fName
)) {
201 /* Check that 'fValue' is the same */
202 Object value
= this.fValue
;
204 if (other
.fValue
!= null) {
207 } else if (!value
.equals(other
.fValue
)) {
211 /* Check that 'fFields' are the same */
212 if (!fFields
.equals(other
.fFields
)) {
220 public String
toString() {
221 StringBuilder ret
= new StringBuilder();
222 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
224 * If this field is a top-level "field container", we will print its
225 * sub-fields directly.
227 appendSubFields(ret
);
230 /* The field has its own values */
235 if (!fFields
.isEmpty()) {
237 * In addition to its own name/value, this field also has
240 ret
.append(" ["); //$NON-NLS-1$
241 appendSubFields(ret
);
245 return ret
.toString();
248 private void appendSubFields(StringBuilder sb
) {
249 Joiner joiner
= Joiner
.on(", ").skipNulls(); //$NON-NLS-1$
250 sb
.append(joiner
.join(getFields()));
257 public String
getFormattedValue() {
258 return getValue().toString();
This page took 0.037521 seconds and 5 git commands to generate.