Replace location by context in checkpoint indexer
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / trace / TmfExperimentContext.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.core.trace;
15
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;
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. This doesn't imply that a
29 * full parse of the event content was performed (read: the legacy LTTng works
30 * like this...).
31 * <p>
32 * The last trace refers to the trace from which the last event was "consumed"
33 * at the experiment level.
34 */
35 public class TmfExperimentContext extends TmfContext implements Cloneable {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
41 /**
42 * No last trace read indicator
43 */
44 public static final int NO_TRACE = -1;
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
50 private ITmfContext[] fContexts;
51 private ITmfEvent[] fEvents;
52 private int fLastTraceRead;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 /**
59 * @param contexts
60 */
61 public TmfExperimentContext(final ITmfContext[] contexts) {
62 super();
63 fContexts = contexts;
64 fEvents = new ITmfEvent[fContexts.length];
65 final ITmfLocation<?>[] locations = new ITmfLocation[fContexts.length];
66 final long[] ranks = new long[fContexts.length];
67 long rank = 0;
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();
73 }
74
75 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
76 setRank(rank);
77 fLastTraceRead = NO_TRACE;
78 }
79
80 /**
81 * @param other
82 */
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);
90 }
91
92 /* (non-Javadoc)
93 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
94 */
95 @Override
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;
102 return clone;
103 }
104
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;
109 return contexts;
110 }
111
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;
116 return events;
117 }
118
119 // ------------------------------------------------------------------------
120 // Accessors
121 // ------------------------------------------------------------------------
122
123 public ITmfContext[] getContexts() {
124 return fContexts;
125 }
126
127 public ITmfEvent[] getEvents() {
128 return fEvents;
129 }
130
131 public int getLastTrace() {
132 return fLastTraceRead;
133 }
134
135 public void setLastTrace(final int newIndex) {
136 fLastTraceRead = newIndex;
137 }
138
139 // ------------------------------------------------------------------------
140 // Object
141 // ------------------------------------------------------------------------
142
143 @Override
144 public int hashCode() {
145 int result = 17;
146 for (int i = 0; i < fContexts.length; i++) {
147 result = 37 * result + fContexts[i].hashCode();
148 }
149 return result;
150 }
151
152 @Override
153 public boolean equals(final Object other) {
154 if (this == other)
155 return true;
156 if (!super.equals(other))
157 return false;
158 if (!(other instanceof TmfExperimentContext))
159 return false;
160 final TmfExperimentContext o = (TmfExperimentContext) other;
161 boolean isEqual = true;
162 int i = 0;
163 while (isEqual && (i < fContexts.length)) {
164 isEqual &= fContexts[i].equals(o.fContexts[i]);
165 i++;
166 }
167 return isEqual;
168 }
169
170 }
This page took 0.036812 seconds and 5 git commands to generate.