| 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<>(); |
| 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 | @Override |
| 97 | public String getName() { |
| 98 | return fName; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Object getValue() { |
| 103 | return fValue; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String[] getFieldNames() { |
| 108 | return fFieldNames; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String getFieldName(final int index) { |
| 113 | final ITmfEventField field = getField(index); |
| 114 | if (field != null) { |
| 115 | return field.getName(); |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public ITmfEventField[] getFields() { |
| 122 | return (fFields != null) ? fFields : new ITmfEventField[0]; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public ITmfEventField getField(final String name) { |
| 127 | return fNameMapping.get(name); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ITmfEventField getField(final int index) { |
| 132 | if (fFields != null && index >= 0 && index < fFields.length) { |
| 133 | return fFields[index]; |
| 134 | } |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @since 3.0 |
| 140 | */ |
| 141 | @Override |
| 142 | public ITmfEventField getSubField(final String[] names) { |
| 143 | ITmfEventField field = this; |
| 144 | for (String name : names) { |
| 145 | field = field.getField(name); |
| 146 | if (field == null) { |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | return field; |
| 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | // Operations |
| 155 | // ------------------------------------------------------------------------ |
| 156 | |
| 157 | /** |
| 158 | * Create a root field from a list of labels. |
| 159 | * |
| 160 | * @param labels the list of labels |
| 161 | * @return the (flat) root list |
| 162 | */ |
| 163 | public final static ITmfEventField makeRoot(final String[] labels) { |
| 164 | final ITmfEventField[] fields = new ITmfEventField[labels.length]; |
| 165 | for (int i = 0; i < labels.length; i++) { |
| 166 | fields[i] = new TmfEventField(labels[i], null, null); |
| 167 | } |
| 168 | // Return a new root field; |
| 169 | return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields); |
| 170 | } |
| 171 | |
| 172 | // ------------------------------------------------------------------------ |
| 173 | // Object |
| 174 | // ------------------------------------------------------------------------ |
| 175 | |
| 176 | @Override |
| 177 | public int hashCode() { |
| 178 | final int prime = 31; |
| 179 | int result = 1; |
| 180 | result = prime * result + fName.hashCode(); |
| 181 | result = prime * result + ((fValue == null) ? 0 : fValue.hashCode()); |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean equals(final Object obj) { |
| 187 | if (this == obj) { |
| 188 | return true; |
| 189 | } |
| 190 | if (obj == null) { |
| 191 | return false; |
| 192 | } |
| 193 | if (!(obj instanceof TmfEventField)) { |
| 194 | return false; |
| 195 | } |
| 196 | final TmfEventField other = (TmfEventField) obj; |
| 197 | if (!fName.equals(other.fName)) { |
| 198 | return false; |
| 199 | } |
| 200 | if (fValue == null) { |
| 201 | if (other.fValue != null) { |
| 202 | return false; |
| 203 | } |
| 204 | } else if (!fValue.equals(other.fValue)) { |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String toString() { |
| 212 | StringBuilder ret = new StringBuilder(); |
| 213 | if (fName.equals(ITmfEventField.ROOT_FIELD_ID)) { |
| 214 | /* |
| 215 | * If this field is a top-level "field container", we will print its |
| 216 | * sub-fields directly. |
| 217 | */ |
| 218 | appendSubFields(ret); |
| 219 | |
| 220 | } else { |
| 221 | /* The field has its own values */ |
| 222 | ret.append(fName); |
| 223 | ret.append('='); |
| 224 | ret.append(fValue); |
| 225 | |
| 226 | if (fFields != null && fFields.length > 0) { |
| 227 | /* |
| 228 | * In addition to its own name/value, this field also has |
| 229 | * sub-fields. |
| 230 | */ |
| 231 | ret.append(" ["); //$NON-NLS-1$ |
| 232 | appendSubFields(ret); |
| 233 | ret.append(']'); |
| 234 | } |
| 235 | } |
| 236 | return ret.toString(); |
| 237 | } |
| 238 | |
| 239 | private void appendSubFields(StringBuilder sb) { |
| 240 | ITmfEventField field; |
| 241 | for (int i = 0; i < getFields().length; i++) { |
| 242 | field = getFields()[i]; |
| 243 | if (i != 0) { |
| 244 | sb.append(", ");//$NON-NLS-1$ |
| 245 | } |
| 246 | sb.append(field.toString()); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @since 2.0 |
| 252 | */ |
| 253 | @Override |
| 254 | public String getFormattedValue() { |
| 255 | return getValue().toString(); |
| 256 | } |
| 257 | |
| 258 | } |