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
.Arrays
;
21 import java
.util
.Collection
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
27 import org
.eclipse
.tracecompass
.common
.core
.ObjectUtils
;
29 import com
.google
.common
.base
.Joiner
;
30 import com
.google
.common
.collect
.ImmutableMap
;
33 * A basic implementation of ITmfEventField.
35 * Non-value fields are structural (i.e. used to represent the event structure
36 * including optional fields) while the valued fields are actual event fields.
39 * @author Francois Chouinard
44 public class TmfEventField
implements ITmfEventField
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private final @NonNull String fName
;
51 private final @Nullable Object fValue
;
52 private final @NonNull Map
<String
, ITmfEventField
> fFields
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
64 * the event field value
66 * the list of subfields
67 * @throws IllegalArgumentException
68 * If 'name' is null, or if 'fields' has duplicate field names.
70 public TmfEventField(@NonNull String name
, @Nullable Object value
, ITmfEventField
@Nullable [] fields
) {
75 fFields
= ImmutableMap
.of();
77 ImmutableMap
.Builder
<String
, ITmfEventField
> mapBuilder
= new ImmutableMap
.Builder
<>();
78 Arrays
.stream(fields
).forEach(t
-> mapBuilder
.put(t
.getName(), t
));
79 fFields
= mapBuilder
.build();
86 * @param field the other event field
88 public TmfEventField(final TmfEventField field
) {
90 throw new IllegalArgumentException();
93 fValue
= field
.fValue
;
94 fFields
= field
.fFields
;
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 public String
getName() {
107 public Object
getValue() {
112 public final Collection
<String
> getFieldNames() {
113 return fFields
.keySet();
117 public final Collection
<ITmfEventField
> getFields() {
118 return fFields
.values();
122 public ITmfEventField
getField(final String
... path
) {
123 if (path
.length
== 1) {
124 return fFields
.get(path
[0]);
126 ITmfEventField field
= this;
127 for (String name
: path
) {
128 field
= field
.getField(name
);
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 * Create a root field from a list of labels.
143 * @param labels the list of labels
144 * @return the (flat) root list
146 public static final @NonNull ITmfEventField
makeRoot(final String
[] labels
) {
147 final ITmfEventField
[] fields
= new ITmfEventField
[labels
.length
];
148 for (int i
= 0; i
< labels
.length
; i
++) {
149 String label
= checkNotNull(labels
[i
]);
150 fields
[i
] = new TmfEventField(label
, null, null);
152 // Return a new root field;
153 return new TmfEventField(ITmfEventField
.ROOT_FIELD_ID
, null, fields
);
156 // ------------------------------------------------------------------------
158 // ------------------------------------------------------------------------
161 public int hashCode() {
162 final int prime
= 31;
164 result
= prime
* result
+ getName().hashCode();
165 result
= prime
* result
+ ObjectUtils
.deepHashCode(getValue());
166 result
= prime
* result
+ fFields
.hashCode();
171 public boolean equals(final Object obj
) {
179 /* We only consider equals fields of the exact same class. */
180 if (!(this.getClass().equals(obj
.getClass()))) {
184 final TmfEventField other
= (TmfEventField
) obj
;
186 /* Check that the field names are the same. */
187 if (!NonNullUtils
.equalsNullable(getName(), other
.getName())) {
192 * Check that the field values are the same. We use ObjectUtils to
193 * handle cases where the Object values may be primitive and/or nested
196 if (!ObjectUtils
.deepEquals(this.getValue(), other
.getValue())) {
200 /* Check that sub-fields are the same. */
201 if (!fFields
.equals(other
.fFields
)) {
209 public String
toString() {
210 StringBuilder ret
= new StringBuilder();
211 if (fName
.equals(ITmfEventField
.ROOT_FIELD_ID
)) {
213 * If this field is a top-level "field container", we will print its
214 * sub-fields directly.
216 appendSubFields(ret
);
219 /* The field has its own values */
224 if (!fFields
.isEmpty()) {
226 * In addition to its own name/value, this field also has
229 ret
.append(" ["); //$NON-NLS-1$
230 appendSubFields(ret
);
234 return ret
.toString();
237 private void appendSubFields(StringBuilder sb
) {
238 Joiner joiner
= Joiner
.on(", ").skipNulls(); //$NON-NLS-1$
239 sb
.append(joiner
.join(getFields()));
243 public String
getFormattedValue() {
244 return getValue().toString();
This page took 0.035437 seconds and 5 git commands to generate.