Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / TmfEventField.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
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 *******************************************************************************/
15
16 package org.eclipse.tracecompass.tmf.core.event;
17
18 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Map;
23
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;
28
29 import com.google.common.base.Joiner;
30 import com.google.common.collect.ImmutableMap;
31
32 /**
33 * A basic implementation of ITmfEventField.
34 * <p>
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.
37 *
38 * @version 1.0
39 * @author Francois Chouinard
40 *
41 * @see ITmfEvent
42 * @see ITmfEventType
43 */
44 public class TmfEventField implements ITmfEventField {
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
50 private final @NonNull String fName;
51 private final @Nullable Object fValue;
52 private final @NonNull Map<String, ITmfEventField> fFields;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 /**
59 * Full constructor
60 *
61 * @param name
62 * the event field id
63 * @param value
64 * the event field value
65 * @param fields
66 * the list of subfields
67 * @throws IllegalArgumentException
68 * If 'name' is null, or if 'fields' has duplicate field names.
69 */
70 public TmfEventField(@NonNull String name, @Nullable Object value, ITmfEventField @Nullable [] fields) {
71 fName = name;
72 fValue = value;
73
74 if (fields == null) {
75 fFields = ImmutableMap.of();
76 } else {
77 ImmutableMap.Builder<String, ITmfEventField> mapBuilder = new ImmutableMap.Builder<>();
78 Arrays.stream(fields).forEach(t -> mapBuilder.put(t.getName(), t));
79 fFields = mapBuilder.build();
80 }
81 }
82
83 /**
84 * Copy constructor
85 *
86 * @param field the other event field
87 */
88 public TmfEventField(final TmfEventField field) {
89 if (field == null) {
90 throw new IllegalArgumentException();
91 }
92 fName = field.fName;
93 fValue = field.fValue;
94 fFields = field.fFields;
95 }
96
97 // ------------------------------------------------------------------------
98 // ITmfEventField
99 // ------------------------------------------------------------------------
100
101 @Override
102 public String getName() {
103 return fName;
104 }
105
106 @Override
107 public Object getValue() {
108 return fValue;
109 }
110
111 @Override
112 public final Collection<String> getFieldNames() {
113 return fFields.keySet();
114 }
115
116 @Override
117 public final Collection<ITmfEventField> getFields() {
118 return fFields.values();
119 }
120
121 @Override
122 public ITmfEventField getField(final String... path) {
123 if (path.length == 1) {
124 return fFields.get(path[0]);
125 }
126 ITmfEventField field = this;
127 for (String name : path) {
128 field = field.getField(name);
129 if (field == null) {
130 return null;
131 }
132 }
133 return field;
134 }
135
136 // ------------------------------------------------------------------------
137 // Operations
138 // ------------------------------------------------------------------------
139
140 /**
141 * Create a root field from a list of labels.
142 *
143 * @param labels the list of labels
144 * @return the (flat) root list
145 */
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);
151 }
152 // Return a new root field;
153 return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields);
154 }
155
156 // ------------------------------------------------------------------------
157 // Object
158 // ------------------------------------------------------------------------
159
160 @Override
161 public int hashCode() {
162 final int prime = 31;
163 int result = 1;
164 result = prime * result + getName().hashCode();
165 result = prime * result + ObjectUtils.deepHashCode(getValue());
166 result = prime * result + fFields.hashCode();
167 return result;
168 }
169
170 @Override
171 public boolean equals(final Object obj) {
172 if (this == obj) {
173 return true;
174 }
175 if (obj == null) {
176 return false;
177 }
178
179 /* We only consider equals fields of the exact same class. */
180 if (!(this.getClass().equals(obj.getClass()))) {
181 return false;
182 }
183
184 final TmfEventField other = (TmfEventField) obj;
185
186 /* Check that the field names are the same. */
187 if (!NonNullUtils.equalsNullable(getName(), other.getName())) {
188 return false;
189 }
190
191 /*
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
194 * arrays.
195 */
196 if (!ObjectUtils.deepEquals(this.getValue(), other.getValue())) {
197 return false;
198 }
199
200 /* Check that sub-fields are the same. */
201 if (!fFields.equals(other.fFields)) {
202 return false;
203 }
204
205 return true;
206 }
207
208 @Override
209 public String toString() {
210 StringBuilder ret = new StringBuilder();
211 if (fName.equals(ITmfEventField.ROOT_FIELD_ID)) {
212 /*
213 * If this field is a top-level "field container", we will print its
214 * sub-fields directly.
215 */
216 appendSubFields(ret);
217
218 } else {
219 /* The field has its own values */
220 ret.append(fName);
221 ret.append('=');
222 ret.append(fValue);
223
224 if (!fFields.isEmpty()) {
225 /*
226 * In addition to its own name/value, this field also has
227 * sub-fields.
228 */
229 ret.append(" ["); //$NON-NLS-1$
230 appendSubFields(ret);
231 ret.append(']');
232 }
233 }
234 return ret.toString();
235 }
236
237 private void appendSubFields(StringBuilder sb) {
238 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
239 sb.append(joiner.join(getFields()));
240 }
241
242 @Override
243 public String getFormattedValue() {
244 return getValue().toString();
245 }
246
247 }
This page took 0.036047 seconds and 5 git commands to generate.