| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made 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 | * Patrick Tasse - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom; |
| 14 | |
| 15 | import java.text.ParseException; |
| 16 | import java.text.SimpleDateFormat; |
| 17 | import java.util.Arrays; |
| 18 | import java.util.Date; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTraceDefinition.OutputColumn; |
| 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 25 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 26 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
| 27 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
| 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 29 | |
| 30 | public class CustomEvent extends TmfEvent { |
| 31 | |
| 32 | protected static final String TIMESTAMP_INPUT_FORMAT_KEY = "CE_TS_I_F"; //$NON-NLS-1$ |
| 33 | protected static final String NO_MESSAGE = ""; //$NON-NLS-1$ |
| 34 | public static final byte TIMESTAMP_SCALE = -3; |
| 35 | |
| 36 | protected CustomTraceDefinition fDefinition; |
| 37 | protected Map<String, String> fData; |
| 38 | private TmfEventField[] fColumnData; |
| 39 | |
| 40 | public CustomEvent(CustomTraceDefinition definition) { |
| 41 | fDefinition = definition; |
| 42 | fData = new HashMap<String, String>(); |
| 43 | } |
| 44 | |
| 45 | public CustomEvent(CustomTraceDefinition definition, TmfEvent other) { |
| 46 | super(other); |
| 47 | fDefinition = definition; |
| 48 | fData = new HashMap<String, String>(); |
| 49 | } |
| 50 | |
| 51 | public CustomEvent(CustomTraceDefinition definition, ITmfTrace parentTrace, ITmfTimestamp timestamp, String source, TmfEventType type, String reference) { |
| 52 | super(parentTrace, timestamp, source, type, null, reference); |
| 53 | fDefinition = definition; |
| 54 | fData = new HashMap<String, String>(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public ITmfTimestamp getTimestamp() { |
| 59 | if (fData != null) { |
| 60 | processData(); |
| 61 | } |
| 62 | return super.getTimestamp(); |
| 63 | } |
| 64 | |
| 65 | public TmfEventField[] extractItemFields() { |
| 66 | if (fData != null) { |
| 67 | processData(); |
| 68 | } |
| 69 | return Arrays.copyOf(fColumnData, fColumnData.length); |
| 70 | } |
| 71 | |
| 72 | private void processData() { |
| 73 | String timeStampString = fData.get(CustomTraceDefinition.TAG_TIMESTAMP); |
| 74 | String timeStampInputFormat = fData.get(TIMESTAMP_INPUT_FORMAT_KEY); |
| 75 | Date date = null; |
| 76 | if (timeStampInputFormat != null && timeStampString != null) { |
| 77 | SimpleDateFormat dateFormat = new SimpleDateFormat(timeStampInputFormat); |
| 78 | try { |
| 79 | date = dateFormat.parse(timeStampString); |
| 80 | setTimestamp(new TmfTimestamp(date.getTime(), TIMESTAMP_SCALE)); |
| 81 | } catch (ParseException e) { |
| 82 | setTimestamp(TmfTimestamp.ZERO); |
| 83 | } |
| 84 | } else { |
| 85 | setTimestamp(TmfTimestamp.ZERO); |
| 86 | } |
| 87 | |
| 88 | int i = 0; |
| 89 | fColumnData = new TmfEventField[fDefinition.outputs.size()]; |
| 90 | for (OutputColumn outputColumn : fDefinition.outputs) { |
| 91 | String value = fData.get(outputColumn.name); |
| 92 | if (outputColumn.name.equals(CustomTraceDefinition.TAG_TIMESTAMP) && date != null) { |
| 93 | SimpleDateFormat dateFormat = new SimpleDateFormat(fDefinition.timeStampOutputFormat); |
| 94 | fColumnData[i++] = new TmfEventField(outputColumn.name, dateFormat.format(date)); |
| 95 | } else { |
| 96 | fColumnData[i++] = new TmfEventField(outputColumn.name, (value != null ? value : "")); //$NON-NLS-1$ |
| 97 | } |
| 98 | } |
| 99 | CustomEventContent content = (CustomEventContent) getContent(); |
| 100 | content.setFields(fColumnData); |
| 101 | fData = null; |
| 102 | } |
| 103 | |
| 104 | /* (non-Javadoc) |
| 105 | * @see java.lang.Object#hashCode() |
| 106 | */ |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | final int prime = 31; |
| 110 | int result = super.hashCode(); |
| 111 | result = prime * result + ((fDefinition == null) ? 0 : fDefinition.hashCode()); |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | /* (non-Javadoc) |
| 116 | * @see java.lang.Object#equals(java.lang.Object) |
| 117 | */ |
| 118 | @Override |
| 119 | public boolean equals(Object obj) { |
| 120 | if (this == obj) { |
| 121 | return true; |
| 122 | } |
| 123 | if (!super.equals(obj)) { |
| 124 | return false; |
| 125 | } |
| 126 | if (!(obj instanceof CustomEvent)) { |
| 127 | return false; |
| 128 | } |
| 129 | CustomEvent other = (CustomEvent) obj; |
| 130 | if (fDefinition == null) { |
| 131 | if (other.fDefinition != null) { |
| 132 | return false; |
| 133 | } |
| 134 | } else if (!fDefinition.equals(other.fDefinition)) { |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | } |