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