Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 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 | |
11 | *******************************************************************************/ | |
12 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.experiment; |
8c8bf09f | 14 | |
72f1e62a | 15 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
34ccf9a9 | 16 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b FC |
17 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
19 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
8c8bf09f ASL |
20 | |
21 | /** | |
22 | * <b><u>TmfExperimentContext</u></b> | |
23 | * <p> | |
cbdacf03 FC |
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. | |
8f50c396 | 26 | * <p> |
ce2388e0 FC |
27 | * This implies that the "next" event from each trace has already been |
28 | * read and that we at least know its timestamp. This doesn't imply that a | |
29 | * full parse of the event content was performed (read: LTTng works like | |
30 | * this). | |
8f50c396 | 31 | * <p> |
cbdacf03 FC |
32 | * The last trace refers to the trace from which the last event was "consumed" |
33 | * at the experiment level. | |
8c8bf09f | 34 | */ |
9f584e4c FC |
35 | public class TmfExperimentContext extends TmfContext { |
36 | ||
cbdacf03 FC |
37 | // ------------------------------------------------------------------------ |
38 | // Constants | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | public static final int NO_TRACE = -1; | |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Attributes | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
47 | private ITmfTrace<?>[] fTraces = new ITmfTrace[0]; | |
48 | private final ITmfContext[] fContexts; | |
ce2388e0 | 49 | private ITmfEvent[] fEvents; |
cbdacf03 FC |
50 | private int lastTraceRead; |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Constructors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | public TmfExperimentContext(final ITmfTrace<?>[] traces, final ITmfContext[] contexts) { | |
57 | super(); | |
58 | fTraces = traces; | |
59 | fContexts = contexts; | |
60 | fEvents = new ITmfEvent[fTraces.length]; | |
cbdacf03 FC |
61 | final ITmfLocation<?>[] locations = new ITmfLocation[fTraces.length]; |
62 | final long[] ranks = new long[fTraces.length]; | |
63 | long rank = 0; | |
64 | for (int i = 0; i < fTraces.length; i++) | |
65 | if (contexts[i] != null) { | |
66 | locations[i] = contexts[i].getLocation(); | |
67 | ranks[i] = contexts[i].getRank(); | |
68 | rank += contexts[i].getRank(); | |
69 | } | |
70 | ||
71 | setLocation(new TmfExperimentLocation(new TmfLocationArray(locations), ranks)); | |
72 | setRank(rank); | |
73 | lastTraceRead = NO_TRACE; | |
74 | } | |
75 | ||
ce2388e0 FC |
76 | public TmfExperimentContext(final TmfExperimentContext other) { |
77 | this(other.fTraces, other.cloneContexts()); | |
78 | fEvents = other.fEvents; | |
79 | if (other.getLocation() != null) | |
80 | setLocation(other.getLocation().clone()); | |
81 | setRank(other.getRank()); | |
82 | setLastTrace(other.lastTraceRead); | |
83 | } | |
84 | ||
cbdacf03 FC |
85 | public TmfExperimentContext(final ITmfTrace<?>[] traces) { |
86 | this(traces, new TmfContext[traces.length]); | |
87 | } | |
88 | ||
ce2388e0 FC |
89 | private ITmfContext[] cloneContexts() { |
90 | final ITmfContext[] contexts = new ITmfContext[fContexts.length]; | |
91 | for (int i = 0; i < fContexts.length; i++) | |
92 | contexts[i] = fContexts[i].clone(); | |
93 | return contexts; | |
94 | } | |
95 | ||
96 | ||
97 | // public TmfExperimentContext(TmfExperimentContext other) { | |
98 | // this(other.fTraces, other.cloneContexts()); | |
99 | // fEvents = other.fEvents; | |
100 | // if (other.getLocation() != null) | |
101 | // setLocation(other.getLocation().clone()); | |
102 | // setRank(other.getRank()); | |
103 | // setLastTrace(other.lastTraceRead); | |
104 | // } | |
105 | ||
106 | // private ITmfContext[] cloneContexts() { | |
107 | // ITmfContext[] contexts = new TmfContext[fContexts.length]; | |
108 | // for (int i = 0; i < fContexts.length; i++) | |
109 | // contexts[i] = fContexts[i].clone(); | |
110 | // return contexts; | |
111 | // } | |
cbdacf03 FC |
112 | |
113 | // ------------------------------------------------------------------------ | |
114 | // Accessors | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | public ITmfTrace<?>[] getTraces() { | |
118 | return fTraces; | |
119 | } | |
120 | ||
121 | public ITmfContext[] getContexts() { | |
122 | return fContexts; | |
123 | } | |
124 | ||
125 | public ITmfEvent[] getEvents() { | |
126 | return fEvents; | |
127 | } | |
128 | ||
129 | public int getLastTrace() { | |
130 | return lastTraceRead; | |
131 | } | |
132 | ||
133 | public void setLastTrace(final int newIndex) { | |
134 | lastTraceRead = newIndex; | |
135 | } | |
136 | ||
137 | // ------------------------------------------------------------------------ | |
138 | // Object | |
139 | // ------------------------------------------------------------------------ | |
550d787e FC |
140 | |
141 | @Override | |
142 | public int hashCode() { | |
cbdacf03 FC |
143 | int result = 17; |
144 | for (int i = 0; i < fTraces.length; i++) { | |
145 | result = 37 * result + fTraces[i].hashCode(); | |
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; | |
bcbea6a6 | 162 | while (isEqual && (i < fTraces.length)) { |
cbdacf03 FC |
163 | isEqual &= fTraces[i].equals(o.fTraces[i]); |
164 | isEqual &= fContexts[i].equals(o.fContexts[i]); | |
165 | i++; | |
166 | } | |
167 | return isEqual; | |
550d787e | 168 | } |
cbdacf03 | 169 | |
8c8bf09f | 170 | } |