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