1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Put in shape for 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
21 * The experiment context in TMF.
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.
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
28 * full parse of the event content was performed (read: the legacy LTTng works
31 * The last trace refers to the trace from which the last event was "consumed"
32 * at the experiment level.
34 public class TmfExperimentContext
extends TmfContext
implements Cloneable
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 * No last trace read indicator
43 public static final int NO_TRACE
= -1;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
49 private ITmfContext
[] fContexts
;
50 private ITmfEvent
[] fEvents
;
51 private int fLastTraceRead
;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
60 public TmfExperimentContext(final ITmfContext
[] contexts
) {
63 fEvents
= new ITmfEvent
[fContexts
.length
];
64 final TmfRankedLocation
[] locations
= new TmfRankedLocation
[fContexts
.length
];
67 for (int i
= 0; i
< fContexts
.length
; i
++) {
68 if (contexts
[i
] != null) {
69 locations
[i
] = new TmfRankedLocation(contexts
[i
]);
70 rank
+= contexts
[i
].getRank();
74 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
)));
76 fLastTraceRead
= NO_TRACE
;
82 public TmfExperimentContext(final TmfExperimentContext other
) {
83 this(other
.cloneContexts());
84 fEvents
= other
.fEvents
;
85 if (other
.getLocation() != null)
86 setLocation(other
.getLocation().clone());
87 setRank(other
.getRank());
88 setLastTrace(other
.fLastTraceRead
);
92 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
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
;
104 private ITmfContext
[] cloneContexts() {
105 final ITmfContext
[] contexts
= new ITmfContext
[fContexts
.length
];
106 for (int i
= 0; i
< fContexts
.length
; i
++)
107 contexts
[i
] = (fContexts
[i
] != null) ? fContexts
[i
].clone() : null;
111 private ITmfEvent
[] cloneEvents() {
112 final ITmfEvent
[] events
= new ITmfEvent
[fEvents
.length
];
113 for (int i
= 0; i
< fEvents
.length
; i
++)
114 events
[i
] = (fEvents
[i
] != null) ? fEvents
[i
].clone() : null;
118 // ------------------------------------------------------------------------
120 // ------------------------------------------------------------------------
122 public ITmfContext
[] getContexts() {
126 public ITmfEvent
[] getEvents() {
130 public int getLastTrace() {
131 return fLastTraceRead
;
134 public void setLastTrace(final int newIndex
) {
135 fLastTraceRead
= newIndex
;
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 public int hashCode() {
145 for (int i
= 0; i
< fContexts
.length
; i
++) {
146 result
= 37 * result
+ fContexts
[i
].hashCode();
152 public boolean equals(final Object other
) {
155 if (!super.equals(other
))
157 if (!(other
instanceof TmfExperimentContext
))
159 final TmfExperimentContext o
= (TmfExperimentContext
) other
;
160 boolean isEqual
= true;
162 while (isEqual
&& (i
< fContexts
.length
)) {
163 isEqual
&= fContexts
[i
].equals(o
.fContexts
[i
]);
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"));
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"));
183 sb
.append("\tfLastTraceRead=" + fLastTraceRead
+ "\n");
185 return sb
.toString();