More warning fixes
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventField.java
CommitLineData
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 14package org.eclipse.linuxtools.tmf.core.event;
8c8bf09f 15
ea2b103b 16import java.util.Arrays;
4c564a2d
FC
17import java.util.HashMap;
18import java.util.Map;
19
8c8bf09f
ASL
20/**
21 * <b><u>TmfEventField</u></b>
22 * <p>
75d42a16
FC
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.
8c8bf09f 26 */
cbbcc354 27public class TmfEventField implements ITmfEventField {
8c8bf09f 28
cbd4ad82 29 // ------------------------------------------------------------------------
8c8bf09f 30 // Attributes
cbd4ad82 31 // ------------------------------------------------------------------------
8c8bf09f 32
4c564a2d
FC
33 private String fName;
34 private Object fValue;
35 private ITmfEventField[] fFields;
8c8bf09f 36
4c564a2d
FC
37 private String[] fFieldNames;
38 private Map<String, ITmfEventField> fNameMapping;
39
cbd4ad82 40 // ------------------------------------------------------------------------
8c8bf09f 41 // Constructors
cbd4ad82
FC
42 // ------------------------------------------------------------------------
43
cbbcc354 44 /**
45 * Default constructor
46 */
cbd4ad82 47 @SuppressWarnings("unused")
cbbcc354 48 private TmfEventField() {
cbd4ad82 49 }
8c8bf09f 50
4c564a2d 51 /**
75d42a16 52 * Constructor for a structural field
4c564a2d
FC
53 *
54 * @param name the event field id
75d42a16 55 * @param subfields the list of subfields
4c564a2d 56 */
75d42a16
FC
57 public TmfEventField(String name, ITmfEventField[] fields) {
58 this(name, null, fields);
4c564a2d
FC
59 }
60
61 /**
75d42a16 62 * Constructor for a terminal field (i.e. no subfields)
4c564a2d
FC
63 *
64 * @param name the event field id
75d42a16 65 * @param value the event field value
4c564a2d 66 */
75d42a16
FC
67 public TmfEventField(String name, Object value) {
68 this(name, value, null);
4c564a2d
FC
69 }
70
8c8bf09f 71 /**
cbbcc354 72 * Full constructor
73 *
4c564a2d 74 * @param name the event field id
cbbcc354 75 * @param value the event field value
4c564a2d 76 * @param subfields the list of subfields
8c8bf09f 77 */
4c564a2d
FC
78 public TmfEventField(String name, Object value, ITmfEventField[] fields) {
79 if (name == null) {
cbbcc354 80 throw new IllegalArgumentException();
81 }
4c564a2d 82 fName = name;
cbbcc354 83 fValue = value;
ea2b103b 84 fFields = (fields != null) ? Arrays.copyOf(fields, fields.length) : null;
4c564a2d 85 populateStructs();
28b94d61
FC
86 }
87
88 /**
cbbcc354 89 * Copy constructor
90 *
91 * @param field the other event field
28b94d61 92 */
cbbcc354 93 public TmfEventField(TmfEventField field) {
94 if (field == null)
cbd4ad82 95 throw new IllegalArgumentException();
4c564a2d 96 fName = field.fName;
cbbcc354 97 fValue = field.fValue;
4c564a2d
FC
98 fFields = field.fFields;
99 fFieldNames = field.fFieldNames;
75d42a16 100 populateStructs();
28b94d61
FC
101 }
102
cbd4ad82 103 // ------------------------------------------------------------------------
cbbcc354 104 // ITmfEventField
cbd4ad82 105 // ------------------------------------------------------------------------
8c8bf09f 106
d7dbf09a
FC
107 /* (non-Javadoc)
108 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
109 */
110 @Override
4c564a2d
FC
111 public String getName() {
112 return fName;
28b94d61
FC
113 }
114
d7dbf09a
FC
115 /* (non-Javadoc)
116 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
117 */
118 @Override
8c8bf09f
ASL
119 public Object getValue() {
120 return fValue;
121 }
122
d7dbf09a
FC
123 /* (non-Javadoc)
124 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
125 */
126 @Override
4c564a2d 127 public String[] getFieldNames() {
ea2b103b 128 return Arrays.copyOf(fFieldNames, fFieldNames.length);
4c564a2d
FC
129 }
130
d7dbf09a
FC
131 /* (non-Javadoc)
132 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
133 */
134 @Override
4c564a2d
FC
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
d7dbf09a
FC
143 /* (non-Javadoc)
144 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
145 */
146 @Override
4c564a2d 147 public ITmfEventField[] getFields() {
ea2b103b 148 return (fFields != null) ? Arrays.copyOf(fFields, fFields.length) : null;
4c564a2d
FC
149 }
150
d7dbf09a
FC
151 /* (non-Javadoc)
152 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
153 */
154 @Override
4c564a2d
FC
155 public ITmfEventField getField(String name) {
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;
bd78efc6 262 if (!(obj instanceof TmfEventField))
cbbcc354 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}
This page took 0.046704 seconds and 5 git commands to generate.