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