| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 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 | * Francois Chouinard - Initial API and implementation |
| 11 | * Francois Chouinard - Put in shape for 1.0 |
| 12 | * Patrick Tasse - Updated for removal of context clone |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.internal.tmf.core.trace; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 19 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
| 20 | |
| 21 | /** |
| 22 | * The experiment context in TMF. |
| 23 | * <p> |
| 24 | * The experiment keeps track of the next event from each of its traces so it |
| 25 | * can pick the next one in chronological order. |
| 26 | * <p> |
| 27 | * This implies that the "next" event from each trace has already been |
| 28 | * read and that we at least know its timestamp. |
| 29 | * <p> |
| 30 | * The last trace refers to the trace from which the last event was "consumed" |
| 31 | * at the experiment level. |
| 32 | */ |
| 33 | public class TmfExperimentContext extends TmfContext { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Constants |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | /** |
| 40 | * No last trace read indicator |
| 41 | */ |
| 42 | public static final int NO_TRACE = -1; |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Attributes |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | private ITmfContext[] fContexts; |
| 49 | private ITmfEvent[] fEvents; |
| 50 | private int fLastTraceRead; |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Constructors |
| 54 | // ------------------------------------------------------------------------ |
| 55 | |
| 56 | /** |
| 57 | * Standard constructor |
| 58 | * |
| 59 | * @param nbTraces |
| 60 | * The number of traces in the experiment |
| 61 | */ |
| 62 | public TmfExperimentContext(final int nbTraces) { |
| 63 | super(); |
| 64 | fContexts = new ITmfContext[nbTraces]; |
| 65 | fEvents = new ITmfEvent[nbTraces]; |
| 66 | fLastTraceRead = NO_TRACE; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void dispose() { |
| 71 | for (ITmfContext context : fContexts) { |
| 72 | context.dispose(); |
| 73 | } |
| 74 | super.dispose(); |
| 75 | } |
| 76 | |
| 77 | // ------------------------------------------------------------------------ |
| 78 | // Accessors |
| 79 | // ------------------------------------------------------------------------ |
| 80 | |
| 81 | /** |
| 82 | * Get the trace contexts composing this experiment context. |
| 83 | * |
| 84 | * @return The array of trace contexts |
| 85 | */ |
| 86 | public ITmfContext[] getContexts() { |
| 87 | return fContexts; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the trace events located at this experiment context's location. |
| 92 | * |
| 93 | * @return The array of trace events |
| 94 | */ |
| 95 | public ITmfEvent[] getEvents() { |
| 96 | return fEvents; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the index of the trace that was last read (so the trace whose |
| 101 | * current context will match this experiment's). |
| 102 | * |
| 103 | * @return The index of the trace |
| 104 | */ |
| 105 | public int getLastTrace() { |
| 106 | return fLastTraceRead; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set the last trace read index |
| 111 | * |
| 112 | * @param newIndex |
| 113 | * The new value to assign |
| 114 | */ |
| 115 | public void setLastTrace(final int newIndex) { |
| 116 | fLastTraceRead = newIndex; |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | // Object |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
| 123 | @Override |
| 124 | public int hashCode() { |
| 125 | int result = 17; |
| 126 | for (int i = 0; i < fContexts.length; i++) { |
| 127 | result = 37 * result + fContexts[i].hashCode(); |
| 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(final Object other) { |
| 134 | if (this == other) { |
| 135 | return true; |
| 136 | } |
| 137 | if (!super.equals(other)) { |
| 138 | return false; |
| 139 | } |
| 140 | if (!(other instanceof TmfExperimentContext)) { |
| 141 | return false; |
| 142 | } |
| 143 | final TmfExperimentContext o = (TmfExperimentContext) other; |
| 144 | boolean isEqual = true; |
| 145 | int i = 0; |
| 146 | while (isEqual && (i < fContexts.length)) { |
| 147 | isEqual &= fContexts[i].equals(o.fContexts[i]); |
| 148 | i++; |
| 149 | } |
| 150 | return isEqual; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | @SuppressWarnings("nls") |
| 155 | public String toString() { |
| 156 | StringBuilder sb = new StringBuilder("TmfExperimentContext [\n"); |
| 157 | sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n"); |
| 158 | sb.append("\tfContexts=["); |
| 159 | for (int i = 0; i < fContexts.length; i++) { |
| 160 | sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n")); |
| 161 | } |
| 162 | sb.append("\tfEvents=["); |
| 163 | for (int i = 0; i < fEvents.length; i++) { |
| 164 | ITmfEvent event = fEvents[i]; |
| 165 | sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n")); |
| 166 | } |
| 167 | sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n"); |
| 168 | sb.append("]"); |
| 169 | return sb.toString(); |
| 170 | } |
| 171 | |
| 172 | } |