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
.ITmfLocation
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
22 * The experiment context in TMF.
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.
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: the legacy LTTng works
32 * The last trace refers to the trace from which the last event was "consumed"
33 * at the experiment level.
35 public class TmfExperimentContext
extends TmfContext
implements Cloneable
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * No last trace read indicator
44 public static final int NO_TRACE
= -1;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private ITmfContext
[] fContexts
;
51 private ITmfEvent
[] fEvents
;
52 private int fLastTraceRead
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
61 public TmfExperimentContext(final ITmfContext
[] contexts
) {
64 fEvents
= new ITmfEvent
[fContexts
.length
];
65 final ITmfLocation
<?
>[] locations
= new ITmfLocation
[fContexts
.length
];
66 final long[] ranks
= new long[fContexts
.length
];
68 for (int i
= 0; i
< fContexts
.length
; i
++)
69 if (contexts
[i
] != null) {
70 locations
[i
] = contexts
[i
].getLocation();
71 ranks
[i
] = contexts
[i
].getRank();
72 rank
+= contexts
[i
].getRank();
75 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
)));
77 fLastTraceRead
= NO_TRACE
;
83 public TmfExperimentContext(final TmfExperimentContext other
) {
84 this(other
.cloneContexts());
85 fEvents
= other
.fEvents
;
86 if (other
.getLocation() != null)
87 setLocation(other
.getLocation().clone());
88 setRank(other
.getRank());
89 setLastTrace(other
.fLastTraceRead
);
93 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
96 public TmfExperimentContext
clone() {
97 TmfExperimentContext clone
= null;
98 clone
= (TmfExperimentContext
) super.clone();
99 clone
.fContexts
= cloneContexts();
100 clone
.fEvents
= cloneEvents();
101 clone
.fLastTraceRead
= fLastTraceRead
;
105 private ITmfContext
[] cloneContexts() {
106 final ITmfContext
[] contexts
= new ITmfContext
[fContexts
.length
];
107 for (int i
= 0; i
< fContexts
.length
; i
++)
108 contexts
[i
] = (fContexts
[i
] != null) ? fContexts
[i
].clone() : null;
112 private ITmfEvent
[] cloneEvents() {
113 final ITmfEvent
[] events
= new ITmfEvent
[fEvents
.length
];
114 for (int i
= 0; i
< fEvents
.length
; i
++)
115 events
[i
] = (fEvents
[i
] != null) ? fEvents
[i
].clone() : null;
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
123 public ITmfContext
[] getContexts() {
127 public ITmfEvent
[] getEvents() {
131 public int getLastTrace() {
132 return fLastTraceRead
;
135 public void setLastTrace(final int newIndex
) {
136 fLastTraceRead
= newIndex
;
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
144 public int hashCode() {
146 for (int i
= 0; i
< fContexts
.length
; i
++) {
147 result
= 37 * result
+ fContexts
[i
].hashCode();
153 public boolean equals(final Object other
) {
156 if (!super.equals(other
))
158 if (!(other
instanceof TmfExperimentContext
))
160 final TmfExperimentContext o
= (TmfExperimentContext
) other
;
161 boolean isEqual
= true;
163 while (isEqual
&& (i
< fContexts
.length
)) {
164 isEqual
&= fContexts
[i
].equals(o
.fContexts
[i
]);