Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
0316808c | 2 | * Copyright (c) 2009, 2010, 2012 Ericsson |
ce2388e0 | 3 | * |
8c8bf09f ASL |
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 | |
ce2388e0 | 8 | * |
8c8bf09f ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
3bd44ac8 | 11 | * Francois Chouinard - Put in shape for 1.0 |
8c8bf09f ASL |
12 | *******************************************************************************/ |
13 | ||
9e0640dc | 14 | package org.eclipse.linuxtools.internal.tmf.core.trace; |
8c8bf09f | 15 | |
72f1e62a | 16 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
34ccf9a9 | 17 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b | 18 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
8c8bf09f ASL |
19 | |
20 | /** | |
0316808c | 21 | * The experiment context in TMF. |
8c8bf09f | 22 | * <p> |
cbdacf03 FC |
23 | * The experiment keeps track of the next event from each of its traces so it |
24 | * can pick the next one in chronological order. | |
8f50c396 | 25 | * <p> |
ce2388e0 FC |
26 | * This implies that the "next" event from each trace has already been |
27 | * read and that we at least know its timestamp. This doesn't imply that a | |
0316808c FC |
28 | * full parse of the event content was performed (read: the legacy LTTng works |
29 | * like this...). | |
8f50c396 | 30 | * <p> |
cbdacf03 FC |
31 | * The last trace refers to the trace from which the last event was "consumed" |
32 | * at the experiment level. | |
8c8bf09f | 33 | */ |
03648eab | 34 | public class TmfExperimentContext extends TmfContext implements Cloneable { |
9f584e4c | 35 | |
cbdacf03 FC |
36 | // ------------------------------------------------------------------------ |
37 | // Constants | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
0316808c FC |
40 | /** |
41 | * No last trace read indicator | |
42 | */ | |
9e0640dc | 43 | public static final int NO_TRACE = -1; |
cbdacf03 FC |
44 | |
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
03648eab | 49 | private ITmfContext[] fContexts; |
ce2388e0 | 50 | private ITmfEvent[] fEvents; |
03648eab | 51 | private int fLastTraceRead; |
cbdacf03 FC |
52 | |
53 | // ------------------------------------------------------------------------ | |
54 | // Constructors | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
408e65d2 FC |
57 | /** |
58 | * @param contexts | |
59 | */ | |
0316808c | 60 | public TmfExperimentContext(final ITmfContext[] contexts) { |
cbdacf03 | 61 | super(); |
cbdacf03 | 62 | fContexts = contexts; |
0316808c | 63 | fEvents = new ITmfEvent[fContexts.length]; |
9a7f542f | 64 | final TmfRankedLocation[] locations = new TmfRankedLocation[fContexts.length]; |
3bd44ac8 | 65 | |
cbdacf03 | 66 | long rank = 0; |
9a7f542f | 67 | for (int i = 0; i < fContexts.length; i++) { |
cbdacf03 | 68 | if (contexts[i] != null) { |
9a7f542f | 69 | locations[i] = new TmfRankedLocation(contexts[i]); |
cbdacf03 FC |
70 | rank += contexts[i].getRank(); |
71 | } | |
9a7f542f | 72 | } |
cbdacf03 | 73 | |
9a7f542f | 74 | setLocation(new TmfExperimentLocation(new TmfLocationArray(locations))); |
cbdacf03 | 75 | setRank(rank); |
03648eab | 76 | fLastTraceRead = NO_TRACE; |
cbdacf03 FC |
77 | } |
78 | ||
408e65d2 FC |
79 | /** |
80 | * @param other | |
81 | */ | |
ce2388e0 | 82 | public TmfExperimentContext(final TmfExperimentContext other) { |
0316808c | 83 | this(other.cloneContexts()); |
ce2388e0 FC |
84 | fEvents = other.fEvents; |
85 | if (other.getLocation() != null) | |
86 | setLocation(other.getLocation().clone()); | |
87 | setRank(other.getRank()); | |
03648eab | 88 | setLastTrace(other.fLastTraceRead); |
ce2388e0 FC |
89 | } |
90 | ||
408e65d2 FC |
91 | /* (non-Javadoc) |
92 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() | |
93 | */ | |
94 | @Override | |
95 | public TmfExperimentContext clone() { | |
96 | TmfExperimentContext clone = null; | |
97 | clone = (TmfExperimentContext) super.clone(); | |
98 | clone.fContexts = cloneContexts(); | |
99 | clone.fEvents = cloneEvents(); | |
100 | clone.fLastTraceRead = fLastTraceRead; | |
101 | return clone; | |
102 | } | |
103 | ||
ce2388e0 FC |
104 | private ITmfContext[] cloneContexts() { |
105 | final ITmfContext[] contexts = new ITmfContext[fContexts.length]; | |
106 | for (int i = 0; i < fContexts.length; i++) | |
408e65d2 | 107 | contexts[i] = (fContexts[i] != null) ? fContexts[i].clone() : null; |
ce2388e0 FC |
108 | return contexts; |
109 | } | |
110 | ||
03648eab FC |
111 | private ITmfEvent[] cloneEvents() { |
112 | final ITmfEvent[] events = new ITmfEvent[fEvents.length]; | |
113 | for (int i = 0; i < fEvents.length; i++) | |
408e65d2 | 114 | events[i] = (fEvents[i] != null) ? fEvents[i].clone() : null; |
03648eab FC |
115 | return events; |
116 | } | |
ce2388e0 | 117 | |
cbdacf03 FC |
118 | // ------------------------------------------------------------------------ |
119 | // Accessors | |
120 | // ------------------------------------------------------------------------ | |
121 | ||
cbdacf03 FC |
122 | public ITmfContext[] getContexts() { |
123 | return fContexts; | |
124 | } | |
125 | ||
126 | public ITmfEvent[] getEvents() { | |
127 | return fEvents; | |
128 | } | |
129 | ||
130 | public int getLastTrace() { | |
03648eab | 131 | return fLastTraceRead; |
cbdacf03 FC |
132 | } |
133 | ||
134 | public void setLastTrace(final int newIndex) { | |
03648eab | 135 | fLastTraceRead = newIndex; |
cbdacf03 FC |
136 | } |
137 | ||
138 | // ------------------------------------------------------------------------ | |
139 | // Object | |
140 | // ------------------------------------------------------------------------ | |
550d787e FC |
141 | |
142 | @Override | |
143 | public int hashCode() { | |
cbdacf03 | 144 | int result = 17; |
0316808c | 145 | for (int i = 0; i < fContexts.length; i++) { |
cbdacf03 FC |
146 | result = 37 * result + fContexts[i].hashCode(); |
147 | } | |
148 | return result; | |
550d787e | 149 | } |
cbdacf03 | 150 | |
550d787e | 151 | @Override |
cbdacf03 | 152 | public boolean equals(final Object other) { |
6e85c58d FC |
153 | if (this == other) |
154 | return true; | |
155 | if (!super.equals(other)) | |
156 | return false; | |
cbdacf03 FC |
157 | if (!(other instanceof TmfExperimentContext)) |
158 | return false; | |
159 | final TmfExperimentContext o = (TmfExperimentContext) other; | |
160 | boolean isEqual = true; | |
161 | int i = 0; | |
0316808c | 162 | while (isEqual && (i < fContexts.length)) { |
cbdacf03 FC |
163 | isEqual &= fContexts[i].equals(o.fContexts[i]); |
164 | i++; | |
165 | } | |
166 | return isEqual; | |
550d787e | 167 | } |
cbdacf03 | 168 | |
3bd44ac8 FC |
169 | @Override |
170 | @SuppressWarnings("nls") | |
171 | public String toString() { | |
172 | StringBuilder sb = new StringBuilder("TmfExperimentContext [\n"); | |
173 | sb.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n"); | |
174 | sb.append("\tfContexts=["); | |
175 | for (int i = 0; i < fContexts.length; i++) { | |
176 | sb.append("(" + fContexts[i].getLocation() + "," + fContexts[i].getRank() + ((i < fContexts.length - 1) ? ")," : ")]\n")); | |
177 | } | |
178 | sb.append("\tfEvents=["); | |
179 | for (int i = 0; i < fEvents.length; i++) { | |
180 | ITmfEvent event = fEvents[i]; | |
181 | sb.append(((event != null) ? fEvents[i].getTimestamp() : "(null)") + ((i < fEvents.length - 1) ? "," : "]\n")); | |
182 | } | |
183 | sb.append("\tfLastTraceRead=" + fLastTraceRead + "\n"); | |
184 | sb.append("]"); | |
185 | return sb.toString(); | |
186 | } | |
187 | ||
8c8bf09f | 188 | } |