Fix NLS-related Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventField.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Alexandre Montplaisir - Removed Cloneable, made immutable
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.event;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 /**
21 * A basic implementation of ITmfEventField.
22 * <p>
23 * Non-value fields are structural (i.e. used to represent the event structure
24 * including optional fields) while the valued fields are actual event fields.
25 *
26 * @version 1.0
27 * @author Francois Chouinard
28 *
29 * @see ITmfEvent
30 * @see ITmfEventType
31 */
32 public class TmfEventField implements ITmfEventField {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 private final String fName;
39 private final Object fValue;
40 private final ITmfEventField[] fFields;
41
42 private final String[] fFieldNames;
43 private final Map<String, ITmfEventField> fNameMapping;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Full constructor
51 *
52 * @param name the event field id
53 * @param value the event field value
54 * @param fields the list of subfields
55 */
56 public TmfEventField(final String name, final Object value, final ITmfEventField[] fields) {
57 if (name == null) {
58 throw new IllegalArgumentException();
59 }
60 fName = name;
61 fValue = value;
62 fFields = fields;
63
64 /* Fill the fFieldNames and fNameMapping structures */
65 final int nbFields = (fFields != null) ? fFields.length : 0;
66 fFieldNames = new String[nbFields];
67 fNameMapping = new HashMap<String, ITmfEventField>();
68
69 for (int i = 0; i < nbFields; i++) {
70 final String curName = fFields[i].getName();
71 fFieldNames[i] = curName;
72 fNameMapping.put(curName, fFields[i]);
73 }
74 }
75
76 /**
77 * Copy constructor
78 *
79 * @param field the other event field
80 */
81 public TmfEventField(final TmfEventField field) {
82 if (field == null) {
83 throw new IllegalArgumentException();
84 }
85 fName = field.fName;
86 fValue = field.fValue;
87 fFields = field.fFields;
88 fFieldNames = field.fFieldNames;
89 fNameMapping = field.fNameMapping;
90 }
91
92 // ------------------------------------------------------------------------
93 // ITmfEventField
94 // ------------------------------------------------------------------------
95
96 /* (non-Javadoc)
97 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
98 */
99 @Override
100 public String getName() {
101 return fName;
102 }
103
104 /* (non-Javadoc)
105 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
106 */
107 @Override
108 public Object getValue() {
109 return fValue;
110 }
111
112 /* (non-Javadoc)
113 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
114 */
115 @Override
116 public String[] getFieldNames() {
117 return fFieldNames;
118 }
119
120 /* (non-Javadoc)
121 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
122 */
123 @Override
124 public String getFieldName(final int index) {
125 final ITmfEventField field = getField(index);
126 if (field != null) {
127 return field.getName();
128 }
129 return null;
130 }
131
132 /* (non-Javadoc)
133 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
134 */
135 @Override
136 public ITmfEventField[] getFields() {
137 return (fFields != null) ? fFields : new ITmfEventField[0];
138 }
139
140 /* (non-Javadoc)
141 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(java.lang.String)
142 */
143 @Override
144 public ITmfEventField getField(final String name) {
145 return fNameMapping.get(name);
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
150 */
151 @Override
152 public ITmfEventField getField(final int index) {
153 if (fFields != null && index >= 0 && index < fFields.length) {
154 return fFields[index];
155 }
156 return null;
157 }
158
159 // ------------------------------------------------------------------------
160 // Operations
161 // ------------------------------------------------------------------------
162
163 /**
164 * Create a root field from a list of labels.
165 *
166 * @param labels the list of labels
167 * @return the (flat) root list
168 */
169 public final static ITmfEventField makeRoot(final String[] labels) {
170 final ITmfEventField[] fields = new ITmfEventField[labels.length];
171 for (int i = 0; i < labels.length; i++) {
172 fields[i] = new TmfEventField(labels[i], null, null);
173 }
174 // Return a new root field;
175 return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields);
176 }
177
178 // ------------------------------------------------------------------------
179 // Object
180 // ------------------------------------------------------------------------
181
182 /* (non-Javadoc)
183 * @see java.lang.Object#hashCode()
184 */
185 @Override
186 public int hashCode() {
187 final int prime = 31;
188 int result = 1;
189 result = prime * result + fName.hashCode();
190 result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
191 return result;
192 }
193
194 /* (non-Javadoc)
195 * @see java.lang.Object#equals(java.lang.Object)
196 */
197 @Override
198 public boolean equals(final Object obj) {
199 if (this == obj) {
200 return true;
201 }
202 if (obj == null) {
203 return false;
204 }
205 if (!(obj instanceof TmfEventField)) {
206 return false;
207 }
208 final TmfEventField other = (TmfEventField) obj;
209 if (!fName.equals(other.fName)) {
210 return false;
211 }
212 if (fValue == null) {
213 if (other.fValue != null) {
214 return false;
215 }
216 } else if (!fValue.equals(other.fValue)) {
217 return false;
218 }
219 return true;
220 }
221
222 /* (non-Javadoc)
223 * @see java.lang.Object#toString()
224 */
225 @Override
226 public String toString() {
227 StringBuilder ret = new StringBuilder();
228 if (fName.equals(ITmfEventField.ROOT_FIELD_ID)) {
229 /*
230 * If this field is a top-level "field container", we will print its
231 * sub-fields directly.
232 */
233 appendSubFields(ret);
234
235 } else {
236 /* The field has its own values */
237 ret.append(fName);
238 ret.append('=');
239 ret.append(fValue);
240
241 if (fFields != null && fFields.length > 0) {
242 /*
243 * In addition to its own name/value, this field also has
244 * sub-fields.
245 */
246 ret.append(" ["); //$NON-NLS-1$
247 appendSubFields(ret);
248 ret.append(']');
249 }
250 }
251 return ret.toString();
252 }
253
254 private void appendSubFields(StringBuilder sb) {
255 ITmfEventField field;
256 for (int i = 0; i < getFields().length; i++) {
257 field = getFields()[i];
258 if (i != 0) {
259 sb.append(", ");//$NON-NLS-1$
260 }
261 sb.append(field.toString());
262 }
263 }
264
265 /**
266 * @since 2.0
267 */
268 @Override
269 public String getFormattedValue() {
270 return getValue().toString();
271 }
272
273 }
This page took 0.036293 seconds and 5 git commands to generate.