Commit | Line | Data |
---|---|---|
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 | 16 | package org.eclipse.tracecompass.tmf.core.event; |
8c8bf09f | 17 | |
5db5a3a4 AM |
18 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
19 | ||
b742c196 | 20 | import java.util.Collection; |
5db5a3a4 | 21 | import java.util.Map; |
b742c196 AM |
22 | |
23 | import org.eclipse.jdt.annotation.NonNull; | |
24 | import org.eclipse.jdt.annotation.Nullable; | |
25 | ||
26 | import com.google.common.base.Joiner; | |
27 | import 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 | 41 | public 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 | */ |
b742c196 | 67 | public TmfEventField(String name, @Nullable Object value, @Nullable ITmfEventField[] fields) { |
b9e37ffd | 68 | if (name == null) { |
cbbcc354 | 69 | throw new IllegalArgumentException(); |
b9e37ffd | 70 | } |
4c564a2d | 71 | fName = name; |
cbbcc354 | 72 | fValue = value; |
80349bf7 | 73 | |
b742c196 | 74 | if (fields == null) { |
5db5a3a4 | 75 | fFields = checkNotNull(ImmutableMap.<String, ITmfEventField> of()); |
b742c196 AM |
76 | } else { |
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); | |
82 | } | |
5db5a3a4 | 83 | fFields = checkNotNull(mapBuilder.build()); |
80349bf7 | 84 | } |
28b94d61 FC |
85 | } |
86 | ||
87 | /** | |
cbbcc354 | 88 | * Copy constructor |
306dc902 | 89 | * |
cbbcc354 | 90 | * @param field the other event field |
28b94d61 | 91 | */ |
085d898f | 92 | public TmfEventField(final TmfEventField field) { |
b9e37ffd | 93 | if (field == null) { |
085d898f | 94 | throw new IllegalArgumentException(); |
b9e37ffd | 95 | } |
085d898f FC |
96 | fName = field.fName; |
97 | fValue = field.fValue; | |
98 | fFields = field.fFields; | |
28b94d61 FC |
99 | } |
100 | ||
cbd4ad82 | 101 | // ------------------------------------------------------------------------ |
cbbcc354 | 102 | // ITmfEventField |
cbd4ad82 | 103 | // ------------------------------------------------------------------------ |
8c8bf09f | 104 | |
d7dbf09a | 105 | @Override |
4c564a2d FC |
106 | public String getName() { |
107 | return fName; | |
28b94d61 FC |
108 | } |
109 | ||
d7dbf09a | 110 | @Override |
8c8bf09f ASL |
111 | public Object getValue() { |
112 | return fValue; | |
113 | } | |
114 | ||
d7dbf09a | 115 | @Override |
b742c196 AM |
116 | public Collection<String> getFieldNames() { |
117 | return fFields.keySet(); | |
4c564a2d FC |
118 | } |
119 | ||
d7dbf09a | 120 | @Override |
b742c196 AM |
121 | public Collection<ITmfEventField> getFields() { |
122 | return fFields.values(); | |
4c564a2d FC |
123 | } |
124 | ||
d7dbf09a | 125 | @Override |
97de0bca PT |
126 | public ITmfEventField getField(final String... path) { |
127 | if (path.length == 1) { | |
128 | return fFields.get(path[0]); | |
129 | } | |
6c204912 | 130 | ITmfEventField field = this; |
97de0bca | 131 | for (String name : path) { |
6c204912 GB |
132 | field = field.getField(name); |
133 | if (field == null) { | |
134 | return null; | |
135 | } | |
136 | } | |
137 | return field; | |
138 | } | |
139 | ||
4c564a2d FC |
140 | // ------------------------------------------------------------------------ |
141 | // Operations | |
142 | // ------------------------------------------------------------------------ | |
143 | ||
144 | /** | |
145 | * Create a root field from a list of labels. | |
306dc902 | 146 | * |
4c564a2d FC |
147 | * @param labels the list of labels |
148 | * @return the (flat) root list | |
149 | */ | |
085d898f FC |
150 | public final static ITmfEventField makeRoot(final String[] labels) { |
151 | final ITmfEventField[] fields = new ITmfEventField[labels.length]; | |
b9e37ffd | 152 | for (int i = 0; i < labels.length; i++) { |
214cc822 | 153 | fields[i] = new TmfEventField(labels[i], null, null); |
b9e37ffd FC |
154 | } |
155 | // Return a new root field; | |
214cc822 | 156 | return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields); |
4c564a2d FC |
157 | } |
158 | ||
cbd4ad82 FC |
159 | // ------------------------------------------------------------------------ |
160 | // Object | |
161 | // ------------------------------------------------------------------------ | |
8c8bf09f | 162 | |
28b94d61 | 163 | @Override |
cbd4ad82 | 164 | public int hashCode() { |
b742c196 | 165 | Object value = fValue; |
cbbcc354 | 166 | final int prime = 31; |
167 | int result = 1; | |
75d42a16 | 168 | result = prime * result + fName.hashCode(); |
b742c196 | 169 | result = prime * result + ((value == null) ? 0 : value.hashCode()); |
40d8c779 | 170 | result = prime * result + fFields.hashCode(); |
2fb2eb37 | 171 | return result; |
cbd4ad82 FC |
172 | } |
173 | ||
cbbcc354 | 174 | @Override |
085d898f | 175 | public boolean equals(final Object obj) { |
b9e37ffd | 176 | if (this == obj) { |
cbbcc354 | 177 | return true; |
b9e37ffd FC |
178 | } |
179 | if (obj == null) { | |
cbbcc354 | 180 | return false; |
b9e37ffd FC |
181 | } |
182 | if (!(obj instanceof TmfEventField)) { | |
cbbcc354 | 183 | return false; |
b9e37ffd | 184 | } |
40d8c779 | 185 | |
085d898f | 186 | final TmfEventField other = (TmfEventField) obj; |
40d8c779 AM |
187 | |
188 | /* Check that 'fName' is the same */ | |
b9e37ffd | 189 | if (!fName.equals(other.fName)) { |
cbbcc354 | 190 | return false; |
b9e37ffd | 191 | } |
40d8c779 AM |
192 | |
193 | /* Check that 'fValue' is the same */ | |
b742c196 AM |
194 | Object value = this.fValue; |
195 | if (value == null) { | |
b9e37ffd | 196 | if (other.fValue != null) { |
cbbcc354 | 197 | return false; |
b9e37ffd | 198 | } |
b742c196 | 199 | } else if (!value.equals(other.fValue)) { |
cbbcc354 | 200 | return false; |
b9e37ffd | 201 | } |
40d8c779 AM |
202 | |
203 | /* Check that 'fFields' are the same */ | |
204 | if (!fFields.equals(other.fFields)) { | |
205 | return false; | |
206 | } | |
207 | ||
cbbcc354 | 208 | return true; |
28b94d61 FC |
209 | } |
210 | ||
82b08e62 | 211 | @Override |
cbbcc354 | 212 | public String toString() { |
306dc902 AM |
213 | StringBuilder ret = new StringBuilder(); |
214 | if (fName.equals(ITmfEventField.ROOT_FIELD_ID)) { | |
215 | /* | |
216 | * If this field is a top-level "field container", we will print its | |
217 | * sub-fields directly. | |
218 | */ | |
219 | appendSubFields(ret); | |
220 | ||
221 | } else { | |
222 | /* The field has its own values */ | |
223 | ret.append(fName); | |
224 | ret.append('='); | |
225 | ret.append(fValue); | |
226 | ||
b742c196 | 227 | if (!fFields.isEmpty()) { |
306dc902 AM |
228 | /* |
229 | * In addition to its own name/value, this field also has | |
230 | * sub-fields. | |
231 | */ | |
232 | ret.append(" ["); //$NON-NLS-1$ | |
233 | appendSubFields(ret); | |
234 | ret.append(']'); | |
235 | } | |
236 | } | |
237 | return ret.toString(); | |
238 | } | |
239 | ||
240 | private void appendSubFields(StringBuilder sb) { | |
b742c196 AM |
241 | Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ |
242 | sb.append(joiner.join(getFields())); | |
8c8bf09f | 243 | } |
1f506a43 | 244 | |
8f86c552 GB |
245 | @Override |
246 | public String getFormattedValue() { | |
247 | return getValue().toString(); | |
248 | } | |
249 | ||
cbbcc354 | 250 | } |