Analysis: Add unit tests for the graph base classes
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / TmfEventField.java
CommitLineData
8c8bf09f 1/*******************************************************************************
97de0bca 2 * Copyright (c) 2009, 2015 Ericsson
306dc902 3 *
cbbcc354 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
8c8bf09f
ASL
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
306dc902 8 *
8c8bf09f 9 * Contributors:
1f506a43 10 * Francois Chouinard - Initial API and implementation
bbc1c411 11 * Francois Chouinard - Updated as per TMF Event Model 1.0
80349bf7 12 * Alexandre Montplaisir - Removed Cloneable, made immutable
97de0bca 13 * Patrick Tasse - Remove getSubField
8c8bf09f
ASL
14 *******************************************************************************/
15
2bdf0193 16package org.eclipse.tracecompass.tmf.core.event;
8c8bf09f 17
5db5a3a4
AM
18import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
b742c196 20import java.util.Collection;
5db5a3a4 21import java.util.Map;
b742c196
AM
22
23import org.eclipse.jdt.annotation.NonNull;
24import org.eclipse.jdt.annotation.Nullable;
25
26import com.google.common.base.Joiner;
27import com.google.common.collect.ImmutableMap;
4c564a2d 28
8c8bf09f 29/**
b9e37ffd 30 * A basic implementation of ITmfEventField.
8c8bf09f 31 * <p>
b9e37ffd
FC
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.
306dc902 34 *
b9e37ffd
FC
35 * @version 1.0
36 * @author Francois Chouinard
306dc902 37 *
b9e37ffd 38 * @see ITmfEvent
f7703ed6 39 * @see ITmfEventType
8c8bf09f 40 */
80349bf7 41public class TmfEventField implements ITmfEventField {
8c8bf09f 42
cbd4ad82 43 // ------------------------------------------------------------------------
8c8bf09f 44 // Attributes
cbd4ad82 45 // ------------------------------------------------------------------------
8c8bf09f 46
b742c196
AM
47 private final @NonNull String fName;
48 private final @Nullable Object fValue;
5db5a3a4 49 private final @NonNull Map<String, ITmfEventField> fFields;
085d898f 50
cbd4ad82 51 // ------------------------------------------------------------------------
8c8bf09f 52 // Constructors
cbd4ad82
FC
53 // ------------------------------------------------------------------------
54
8c8bf09f 55 /**
cbbcc354 56 * Full constructor
306dc902 57 *
b742c196
AM
58 * @param name
59 * the event field id
60 * @param value
61 * the event field value
62 * @param fields
63 * the list of subfields
64 * @throws IllegalArgumentException
65 * If 'name' is null, or if 'fields' has duplicate field names.
8c8bf09f 66 */
fafdd006 67 public TmfEventField(@NonNull String name, @Nullable Object value, @Nullable ITmfEventField[] fields) {
4c564a2d 68 fName = name;
cbbcc354 69 fValue = value;
80349bf7 70
b742c196 71 if (fields == null) {
5db5a3a4 72 fFields = checkNotNull(ImmutableMap.<String, ITmfEventField> of());
b742c196
AM
73 } else {
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);
79 }
5db5a3a4 80 fFields = checkNotNull(mapBuilder.build());
80349bf7 81 }
28b94d61
FC
82 }
83
84 /**
cbbcc354 85 * Copy constructor
306dc902 86 *
cbbcc354 87 * @param field the other event field
28b94d61 88 */
085d898f 89 public TmfEventField(final TmfEventField field) {
b9e37ffd 90 if (field == null) {
085d898f 91 throw new IllegalArgumentException();
b9e37ffd 92 }
085d898f
FC
93 fName = field.fName;
94 fValue = field.fValue;
95 fFields = field.fFields;
28b94d61
FC
96 }
97
cbd4ad82 98 // ------------------------------------------------------------------------
cbbcc354 99 // ITmfEventField
cbd4ad82 100 // ------------------------------------------------------------------------
8c8bf09f 101
d7dbf09a 102 @Override
4c564a2d
FC
103 public String getName() {
104 return fName;
28b94d61
FC
105 }
106
d7dbf09a 107 @Override
8c8bf09f
ASL
108 public Object getValue() {
109 return fValue;
110 }
111
d7dbf09a 112 @Override
b742c196 113 public Collection<String> getFieldNames() {
fafdd006 114 return checkNotNull(fFields.keySet());
4c564a2d
FC
115 }
116
d7dbf09a 117 @Override
b742c196 118 public Collection<ITmfEventField> getFields() {
fafdd006 119 return checkNotNull(fFields.values());
4c564a2d
FC
120 }
121
d7dbf09a 122 @Override
97de0bca
PT
123 public ITmfEventField getField(final String... path) {
124 if (path.length == 1) {
125 return fFields.get(path[0]);
126 }
6c204912 127 ITmfEventField field = this;
97de0bca 128 for (String name : path) {
6c204912
GB
129 field = field.getField(name);
130 if (field == null) {
131 return null;
132 }
133 }
134 return field;
135 }
136
4c564a2d
FC
137 // ------------------------------------------------------------------------
138 // Operations
139 // ------------------------------------------------------------------------
140
141 /**
142 * Create a root field from a list of labels.
306dc902 143 *
4c564a2d
FC
144 * @param labels the list of labels
145 * @return the (flat) root list
146 */
be0f521f 147 public static final ITmfEventField makeRoot(final String[] labels) {
085d898f 148 final ITmfEventField[] fields = new ITmfEventField[labels.length];
b9e37ffd 149 for (int i = 0; i < labels.length; i++) {
fafdd006
AM
150 String label = checkNotNull(labels[i]);
151 fields[i] = new TmfEventField(label, null, null);
b9e37ffd
FC
152 }
153 // Return a new root field;
214cc822 154 return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields);
4c564a2d
FC
155 }
156
cbd4ad82
FC
157 // ------------------------------------------------------------------------
158 // Object
159 // ------------------------------------------------------------------------
8c8bf09f 160
28b94d61 161 @Override
cbd4ad82 162 public int hashCode() {
b742c196 163 Object value = fValue;
cbbcc354 164 final int prime = 31;
165 int result = 1;
75d42a16 166 result = prime * result + fName.hashCode();
b742c196 167 result = prime * result + ((value == null) ? 0 : value.hashCode());
40d8c779 168 result = prime * result + fFields.hashCode();
2fb2eb37 169 return result;
cbd4ad82
FC
170 }
171
cbbcc354 172 @Override
085d898f 173 public boolean equals(final Object obj) {
b9e37ffd 174 if (this == obj) {
cbbcc354 175 return true;
b9e37ffd
FC
176 }
177 if (obj == null) {
cbbcc354 178 return false;
b9e37ffd
FC
179 }
180 if (!(obj instanceof TmfEventField)) {
cbbcc354 181 return false;
b9e37ffd 182 }
40d8c779 183
085d898f 184 final TmfEventField other = (TmfEventField) obj;
40d8c779
AM
185
186 /* Check that 'fName' is the same */
b9e37ffd 187 if (!fName.equals(other.fName)) {
cbbcc354 188 return false;
b9e37ffd 189 }
40d8c779
AM
190
191 /* Check that 'fValue' is the same */
b742c196
AM
192 Object value = this.fValue;
193 if (value == null) {
b9e37ffd 194 if (other.fValue != null) {
cbbcc354 195 return false;
b9e37ffd 196 }
b742c196 197 } else if (!value.equals(other.fValue)) {
cbbcc354 198 return false;
b9e37ffd 199 }
40d8c779
AM
200
201 /* Check that 'fFields' are the same */
202 if (!fFields.equals(other.fFields)) {
203 return false;
204 }
205
cbbcc354 206 return true;
28b94d61
FC
207 }
208
82b08e62 209 @Override
cbbcc354 210 public String toString() {
306dc902
AM
211 StringBuilder ret = new StringBuilder();
212 if (fName.equals(ITmfEventField.ROOT_FIELD_ID)) {
213 /*
214 * If this field is a top-level "field container", we will print its
215 * sub-fields directly.
216 */
217 appendSubFields(ret);
218
219 } else {
220 /* The field has its own values */
221 ret.append(fName);
222 ret.append('=');
223 ret.append(fValue);
224
b742c196 225 if (!fFields.isEmpty()) {
306dc902
AM
226 /*
227 * In addition to its own name/value, this field also has
228 * sub-fields.
229 */
230 ret.append(" ["); //$NON-NLS-1$
231 appendSubFields(ret);
232 ret.append(']');
233 }
234 }
235 return ret.toString();
236 }
237
238 private void appendSubFields(StringBuilder sb) {
b742c196
AM
239 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
240 sb.append(joiner.join(getFields()));
8c8bf09f 241 }
1f506a43 242
8f86c552
GB
243 @Override
244 public String getFormattedValue() {
245 return getValue().toString();
246 }
247
cbbcc354 248}
This page took 0.097824 seconds and 5 git commands to generate.