598081cb16b3637c83cc9c691b837abcf3a575fb
[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 subfields the list of subfields
56 */
57 public TmfEventField(String name, 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(String name, 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 subfields the list of subfields
77 */
78 public TmfEventField(String name, Object value, ITmfEventField[] fields) {
79 if (name == null) {
80 throw new IllegalArgumentException();
81 }
82 fName = name;
83 fValue = value;
84 fFields = (fields != null) ? Arrays.copyOf(fields, fields.length) : null;
85 populateStructs();
86 }
87
88 /**
89 * Copy constructor
90 *
91 * @param field the other event field
92 */
93 public TmfEventField(TmfEventField field) {
94 if (field == null)
95 throw new IllegalArgumentException();
96 fName = field.fName;
97 fValue = field.fValue;
98 fFields = field.fFields;
99 fFieldNames = field.fFieldNames;
100 populateStructs();
101 }
102
103 // ------------------------------------------------------------------------
104 // ITmfEventField
105 // ------------------------------------------------------------------------
106
107 /* (non-Javadoc)
108 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
109 */
110 @Override
111 public String getName() {
112 return fName;
113 }
114
115 /* (non-Javadoc)
116 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
117 */
118 @Override
119 public Object getValue() {
120 return fValue;
121 }
122
123 /* (non-Javadoc)
124 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
125 */
126 @Override
127 public String[] getFieldNames() {
128 return Arrays.copyOf(fFieldNames, fFieldNames.length);
129 }
130
131 /* (non-Javadoc)
132 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
133 */
134 @Override
135 public String getFieldName(int index) {
136 ITmfEventField field = getField(index);
137 if (field != null) {
138 return field.getName();
139 }
140 return null;
141 }
142
143 /* (non-Javadoc)
144 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
145 */
146 @Override
147 public ITmfEventField[] getFields() {
148 return (fFields != null) ? Arrays.copyOf(fFields, fFields.length) : null;
149 }
150
151 /* (non-Javadoc)
152 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
153 */
154 @Override
155 public ITmfEventField getField(String name) {
156 return fNameMapping.get(name);
157 }
158
159 /* (non-Javadoc)
160 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
161 */
162 @Override
163 public ITmfEventField getField(int index) {
164 if (fFields != null && index >= 0 && index < fFields.length)
165 return fFields[index];
166 return null;
167 }
168
169 // ------------------------------------------------------------------------
170 // Convenience setters
171 // ------------------------------------------------------------------------
172
173 /**
174 * @param value new field raw value
175 * @param fields the corresponding fields
176 */
177 protected void setValue(Object value, ITmfEventField[] fields) {
178 fValue = value;
179 fFields = (fields != null) ? Arrays.copyOf(fields, fields.length) : null;
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_FIELD_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 }
214 }
215
216 // ------------------------------------------------------------------------
217 // Cloneable
218 // ------------------------------------------------------------------------
219
220 /* (non-Javadoc)
221 * @see java.lang.Object#clone()
222 */
223 @Override
224 public TmfEventField clone() {
225 TmfEventField clone = null;
226 try {
227 clone = (TmfEventField) super.clone();
228 clone.fName = fName;
229 clone.fValue = fValue;
230 clone.fFields = (fFields != null) ? fFields.clone() : null;
231 clone.populateStructs();
232 } catch (CloneNotSupportedException e) {
233 }
234 return clone;
235 }
236
237 // ------------------------------------------------------------------------
238 // Object
239 // ------------------------------------------------------------------------
240
241 /* (non-Javadoc)
242 * @see java.lang.Object#hashCode()
243 */
244 @Override
245 public int hashCode() {
246 final int prime = 31;
247 int result = 1;
248 result = prime * result + fName.hashCode();
249 result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
250 return result;
251 }
252
253 /* (non-Javadoc)
254 * @see java.lang.Object#equals(java.lang.Object)
255 */
256 @Override
257 public boolean equals(Object obj) {
258 if (this == obj)
259 return true;
260 if (obj == null)
261 return false;
262 if (!(obj instanceof TmfEventField))
263 return false;
264 TmfEventField other = (TmfEventField) obj;
265 if (!fName.equals(other.fName))
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;
273 }
274
275 /* (non-Javadoc)
276 * @see java.lang.Object#toString()
277 */
278 @Override
279 @SuppressWarnings("nls")
280 public String toString() {
281 return "TmfEventField [fFieldId=" + fName + ", fValue=" + fValue + "]";
282 }
283
284 }
This page took 0.210453 seconds and 4 git commands to generate.