Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
bbc1c411 | 2 | * Copyright (c) 2009, 2012 Ericsson |
8c8bf09f | 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 | |
8 | * | |
9 | * Contributors: | |
1f506a43 | 10 | * Francois Chouinard - Initial API and implementation |
bbc1c411 | 11 | * Francois Chouinard - Updated as per TMF Event Model 1.0 |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.event; |
8c8bf09f | 15 | |
4c564a2d FC |
16 | import java.util.HashMap; |
17 | import java.util.Map; | |
18 | ||
8c8bf09f ASL |
19 | /** |
20 | * <b><u>TmfEventField</u></b> | |
21 | * <p> | |
75d42a16 FC |
22 | * A basic implementation of ITmfEventField. Non-value fields are structural |
23 | * (i.e. used to represent the event structure including optional fields) while | |
24 | * the valued fields are actual event fields. | |
8c8bf09f | 25 | */ |
cbbcc354 | 26 | public class TmfEventField implements ITmfEventField { |
8c8bf09f | 27 | |
cbd4ad82 | 28 | // ------------------------------------------------------------------------ |
8c8bf09f | 29 | // Attributes |
cbd4ad82 | 30 | // ------------------------------------------------------------------------ |
8c8bf09f | 31 | |
4c564a2d FC |
32 | private String fName; |
33 | private Object fValue; | |
34 | private ITmfEventField[] fFields; | |
8c8bf09f | 35 | |
4c564a2d FC |
36 | private String[] fFieldNames; |
37 | private Map<String, ITmfEventField> fNameMapping; | |
38 | ||
cbd4ad82 | 39 | // ------------------------------------------------------------------------ |
8c8bf09f | 40 | // Constructors |
cbd4ad82 FC |
41 | // ------------------------------------------------------------------------ |
42 | ||
cbbcc354 | 43 | /** |
44 | * Default constructor | |
45 | */ | |
cbd4ad82 | 46 | @SuppressWarnings("unused") |
cbbcc354 | 47 | private TmfEventField() { |
cbd4ad82 | 48 | } |
8c8bf09f | 49 | |
4c564a2d | 50 | /** |
75d42a16 | 51 | * Constructor for a structural field |
4c564a2d FC |
52 | * |
53 | * @param name the event field id | |
75d42a16 | 54 | * @param subfields the list of subfields |
4c564a2d | 55 | */ |
75d42a16 FC |
56 | public TmfEventField(String name, ITmfEventField[] fields) { |
57 | this(name, null, fields); | |
4c564a2d FC |
58 | } |
59 | ||
60 | /** | |
75d42a16 | 61 | * Constructor for a terminal field (i.e. no subfields) |
4c564a2d FC |
62 | * |
63 | * @param name the event field id | |
75d42a16 | 64 | * @param value the event field value |
4c564a2d | 65 | */ |
75d42a16 FC |
66 | public TmfEventField(String name, Object value) { |
67 | this(name, value, null); | |
4c564a2d FC |
68 | } |
69 | ||
8c8bf09f | 70 | /** |
cbbcc354 | 71 | * Full constructor |
72 | * | |
4c564a2d | 73 | * @param name the event field id |
cbbcc354 | 74 | * @param value the event field value |
4c564a2d | 75 | * @param subfields the list of subfields |
8c8bf09f | 76 | */ |
4c564a2d FC |
77 | public TmfEventField(String name, Object value, ITmfEventField[] fields) { |
78 | if (name == null) { | |
cbbcc354 | 79 | throw new IllegalArgumentException(); |
80 | } | |
4c564a2d | 81 | fName = name; |
cbbcc354 | 82 | fValue = value; |
4c564a2d FC |
83 | fFields = fields; |
84 | populateStructs(); | |
28b94d61 FC |
85 | } |
86 | ||
87 | /** | |
cbbcc354 | 88 | * Copy constructor |
89 | * | |
90 | * @param field the other event field | |
28b94d61 | 91 | */ |
cbbcc354 | 92 | public TmfEventField(TmfEventField field) { |
93 | if (field == null) | |
cbd4ad82 | 94 | throw new IllegalArgumentException(); |
4c564a2d | 95 | fName = field.fName; |
cbbcc354 | 96 | fValue = field.fValue; |
4c564a2d FC |
97 | fFields = field.fFields; |
98 | fFieldNames = field.fFieldNames; | |
75d42a16 | 99 | populateStructs(); |
28b94d61 FC |
100 | } |
101 | ||
cbd4ad82 | 102 | // ------------------------------------------------------------------------ |
cbbcc354 | 103 | // ITmfEventField |
cbd4ad82 | 104 | // ------------------------------------------------------------------------ |
8c8bf09f | 105 | |
d7dbf09a FC |
106 | /* (non-Javadoc) |
107 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName() | |
108 | */ | |
109 | @Override | |
4c564a2d FC |
110 | public String getName() { |
111 | return fName; | |
28b94d61 FC |
112 | } |
113 | ||
d7dbf09a FC |
114 | /* (non-Javadoc) |
115 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() | |
116 | */ | |
117 | @Override | |
8c8bf09f ASL |
118 | public Object getValue() { |
119 | return fValue; | |
120 | } | |
121 | ||
d7dbf09a FC |
122 | /* (non-Javadoc) |
123 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames() | |
124 | */ | |
125 | @Override | |
4c564a2d FC |
126 | public String[] getFieldNames() { |
127 | return fFieldNames; | |
128 | } | |
129 | ||
d7dbf09a FC |
130 | /* (non-Javadoc) |
131 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int) | |
132 | */ | |
133 | @Override | |
4c564a2d FC |
134 | public String getFieldName(int index) { |
135 | ITmfEventField field = getField(index); | |
136 | if (field != null) { | |
137 | return field.getName(); | |
138 | } | |
139 | return null; | |
140 | } | |
141 | ||
d7dbf09a FC |
142 | /* (non-Javadoc) |
143 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields() | |
144 | */ | |
145 | @Override | |
4c564a2d FC |
146 | public ITmfEventField[] getFields() { |
147 | return fFields; | |
148 | } | |
149 | ||
d7dbf09a FC |
150 | /* (non-Javadoc) |
151 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String) | |
152 | */ | |
153 | @Override | |
4c564a2d | 154 | public ITmfEventField getField(String name) { |
d7dbf09a | 155 | // FIXME: Add treatment for 'special' fields |
4c564a2d FC |
156 | return fNameMapping.get(name); |
157 | } | |
158 | ||
d7dbf09a FC |
159 | /* (non-Javadoc) |
160 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int) | |
161 | */ | |
162 | @Override | |
4c564a2d | 163 | public ITmfEventField getField(int index) { |
75d42a16 | 164 | if (fFields != null && index >= 0 && index < fFields.length) |
4c564a2d FC |
165 | return fFields[index]; |
166 | return null; | |
cbbcc354 | 167 | } |
168 | ||
169 | // ------------------------------------------------------------------------ | |
170 | // Convenience setters | |
171 | // ------------------------------------------------------------------------ | |
172 | ||
28b94d61 | 173 | /** |
4c564a2d FC |
174 | * @param value new field raw value |
175 | * @param fields the corresponding fields | |
28b94d61 | 176 | */ |
4c564a2d | 177 | protected void setValue(Object value, ITmfEventField[] fields) { |
28b94d61 | 178 | fValue = value; |
4c564a2d FC |
179 | fFields = fields; |
180 | populateStructs(); | |
181 | } | |
182 | ||
183 | // ------------------------------------------------------------------------ | |
184 | // Operations | |
185 | // ------------------------------------------------------------------------ | |
186 | ||
187 | /** | |
188 | * Create a root field from a list of labels. | |
189 | * | |
190 | * @param labels the list of labels | |
191 | * @return the (flat) root list | |
192 | */ | |
193 | public final static ITmfEventField makeRoot(String[] labels) { | |
194 | ITmfEventField[] fields = new ITmfEventField[labels.length]; | |
195 | for (int i = 0; i < labels.length; i++) { | |
196 | fields[i] = new TmfEventField(labels[i], null); | |
197 | } | |
198 | ITmfEventField rootField = new TmfEventField(ITmfEventField.ROOT_ID, fields); | |
199 | return rootField; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Populate the subfield names and the name map | |
204 | */ | |
205 | private void populateStructs() { | |
206 | int nbFields = (fFields != null) ? fFields.length : 0; | |
207 | fFieldNames = new String[nbFields]; | |
208 | fNameMapping = new HashMap<String, ITmfEventField>(); | |
209 | for (int i = 0; i < nbFields; i++) { | |
210 | String name = fFields[i].getName(); | |
211 | fFieldNames[i] = name; | |
212 | fNameMapping.put(name, fFields[i]); | |
213 | } | |
28b94d61 FC |
214 | } |
215 | ||
cbbcc354 | 216 | // ------------------------------------------------------------------------ |
217 | // Cloneable | |
218 | // ------------------------------------------------------------------------ | |
219 | ||
d7dbf09a FC |
220 | /* (non-Javadoc) |
221 | * @see java.lang.Object#clone() | |
222 | */ | |
cbbcc354 | 223 | @Override |
224 | public ITmfEventField clone() { | |
225 | TmfEventField clone = null; | |
226 | try { | |
227 | clone = (TmfEventField) super.clone(); | |
4c564a2d | 228 | clone.fName = fName; |
cbbcc354 | 229 | clone.fValue = fValue; |
4c564a2d FC |
230 | clone.fFields = (fFields != null) ? fFields.clone() : null; |
231 | clone.populateStructs(); | |
cbbcc354 | 232 | } catch (CloneNotSupportedException e) { |
233 | } | |
234 | return clone; | |
235 | } | |
236 | ||
cbd4ad82 FC |
237 | // ------------------------------------------------------------------------ |
238 | // Object | |
239 | // ------------------------------------------------------------------------ | |
8c8bf09f | 240 | |
d7dbf09a FC |
241 | /* (non-Javadoc) |
242 | * @see java.lang.Object#hashCode() | |
243 | */ | |
28b94d61 | 244 | @Override |
cbd4ad82 | 245 | public int hashCode() { |
cbbcc354 | 246 | final int prime = 31; |
247 | int result = 1; | |
75d42a16 | 248 | result = prime * result + fName.hashCode(); |
cbbcc354 | 249 | result = prime * result + ((fValue == null) ? 0 : fValue.hashCode()); |
2fb2eb37 | 250 | return result; |
cbd4ad82 FC |
251 | } |
252 | ||
d7dbf09a FC |
253 | /* (non-Javadoc) |
254 | * @see java.lang.Object#equals(java.lang.Object) | |
255 | */ | |
cbbcc354 | 256 | @Override |
257 | public boolean equals(Object obj) { | |
258 | if (this == obj) | |
259 | return true; | |
260 | if (obj == null) | |
261 | return false; | |
262 | if (getClass() != obj.getClass()) | |
263 | return false; | |
264 | TmfEventField other = (TmfEventField) obj; | |
75d42a16 | 265 | if (!fName.equals(other.fName)) |
cbbcc354 | 266 | return false; |
267 | if (fValue == null) { | |
268 | if (other.fValue != null) | |
269 | return false; | |
270 | } else if (!fValue.equals(other.fValue)) | |
271 | return false; | |
272 | return true; | |
28b94d61 FC |
273 | } |
274 | ||
d7dbf09a FC |
275 | /* (non-Javadoc) |
276 | * @see java.lang.Object#toString() | |
277 | */ | |
82b08e62 | 278 | @Override |
3b38ea61 | 279 | @SuppressWarnings("nls") |
cbbcc354 | 280 | public String toString() { |
4c564a2d | 281 | return "TmfEventField [fFieldId=" + fName + ", fValue=" + fValue + "]"; |
8c8bf09f | 282 | } |
1f506a43 | 283 | |
cbbcc354 | 284 | } |