Commit | Line | Data |
---|---|---|
788ddcbc | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2014 Ericsson |
788ddcbc MK |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * 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 | * | |
f0414df8 SD |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | * Simon Delisle - Remove the iterator in dispose() | |
788ddcbc MK |
12 | *******************************************************************************/ |
13 | ||
9722e5d7 | 14 | package org.eclipse.tracecompass.tmf.ctf.core.context; |
788ddcbc | 15 | |
2bdf0193 AM |
16 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
17 | import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation; | |
9722e5d7 AM |
18 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; |
19 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; | |
20 | import org.eclipse.tracecompass.tmf.ctf.core.trace.iterator.CtfIterator; | |
788ddcbc MK |
21 | |
22 | /** | |
23 | * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to | |
24 | * a boxed Long, a long and an int. | |
25 | * | |
26 | * @author Matthew Khouzam | |
81a2d02e AM |
27 | * @version 1.0 |
28 | * @since 2.0 | |
788ddcbc | 29 | */ |
81a2d02e | 30 | public class CtfTmfContext implements ITmfContext { |
788ddcbc | 31 | |
788ddcbc MK |
32 | // ------------------------------------------- |
33 | // Fields | |
34 | // ------------------------------------------- | |
81a2d02e | 35 | |
b94865c3 MK |
36 | private CtfLocation fCurLocation; |
37 | private long fCurRank; | |
788ddcbc | 38 | |
53b235e1 | 39 | private final CtfTmfTrace fTrace; |
788ddcbc MK |
40 | |
41 | // ------------------------------------------- | |
42 | // Constructor | |
43 | // ------------------------------------------- | |
53b235e1 MK |
44 | |
45 | /** | |
81a2d02e | 46 | * Constructor |
53b235e1 MK |
47 | * |
48 | * @param ctfTmfTrace | |
49 | * the parent trace | |
50 | * @since 1.1 | |
51 | */ | |
81a2d02e | 52 | public CtfTmfContext(CtfTmfTrace ctfTmfTrace) { |
53b235e1 | 53 | fTrace = ctfTmfTrace; |
b94865c3 | 54 | fCurLocation = new CtfLocation(new CtfLocationInfo(0, 0)); |
788ddcbc MK |
55 | } |
56 | ||
57 | // ------------------------------------------- | |
58 | // TmfContext Overrides | |
59 | // ------------------------------------------- | |
60 | ||
61 | @Override | |
62 | public long getRank() { | |
b94865c3 | 63 | return fCurRank; |
788ddcbc MK |
64 | } |
65 | ||
a3db8436 AM |
66 | /** |
67 | * @since 3.0 | |
68 | */ | |
788ddcbc | 69 | @Override |
51a7e2f6 | 70 | public synchronized ITmfLocation getLocation() { |
b94865c3 | 71 | return fCurLocation; |
788ddcbc MK |
72 | } |
73 | ||
74 | @Override | |
75 | public boolean hasValidRank() { | |
b94865c3 | 76 | return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp(); |
788ddcbc MK |
77 | } |
78 | ||
a3db8436 AM |
79 | /** |
80 | * @since 3.0 | |
81 | */ | |
788ddcbc | 82 | @Override |
51a7e2f6 | 83 | public synchronized void setLocation(ITmfLocation location) { |
b94865c3 MK |
84 | fCurLocation = (CtfLocation) location; |
85 | if (fCurLocation != null) { | |
86 | getIterator().seek(fCurLocation.getLocationInfo()); | |
575beffc | 87 | } |
788ddcbc MK |
88 | } |
89 | ||
90 | @Override | |
91 | public void setRank(long rank) { | |
b94865c3 | 92 | fCurRank = rank; |
788ddcbc MK |
93 | |
94 | } | |
95 | ||
96 | @Override | |
97 | public void increaseRank() { | |
98 | if (hasValidRank()) { | |
b94865c3 | 99 | fCurRank++; |
788ddcbc MK |
100 | } |
101 | } | |
102 | ||
103 | // ------------------------------------------- | |
104 | // CtfTmfTrace Helpers | |
105 | // ------------------------------------------- | |
106 | ||
81a2d02e AM |
107 | /** |
108 | * Gets the trace of this context. | |
109 | * | |
110 | * @return The trace of this context | |
111 | */ | |
112 | public CtfTmfTrace getTrace() { | |
113 | return fTrace; | |
114 | } | |
115 | ||
788ddcbc MK |
116 | /** |
117 | * Gets the current event. Wrapper to help CtfTmfTrace | |
53b235e1 | 118 | * |
788ddcbc MK |
119 | * @return The event or null |
120 | */ | |
121 | public synchronized CtfTmfEvent getCurrentEvent() { | |
53b235e1 | 122 | return getIterator().getCurrentEvent(); |
788ddcbc MK |
123 | } |
124 | ||
125 | /** | |
126 | * Advances to a the next event. Wrapper to help CtfTmfTrace | |
53b235e1 | 127 | * |
788ddcbc MK |
128 | * @return success or not |
129 | */ | |
130 | public synchronized boolean advance() { | |
b94865c3 | 131 | final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo(); |
962fb72f PT |
132 | CtfIterator iterator = getIterator(); |
133 | boolean retVal = iterator.advance(); | |
134 | CtfTmfEvent currentEvent = iterator.getCurrentEvent(); | |
132a02b0 | 135 | |
788ddcbc | 136 | if (currentEvent != null) { |
962fb72f | 137 | final long timestampValue = iterator.getCurrentTimestamp(); |
132a02b0 | 138 | if (curLocationData.getTimestamp() == timestampValue) { |
b94865c3 | 139 | fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1); |
132a02b0 | 140 | } else { |
b94865c3 | 141 | fCurLocation = new CtfLocation(timestampValue, 0L); |
132a02b0 | 142 | } |
788ddcbc | 143 | } else { |
b94865c3 | 144 | fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION); |
788ddcbc MK |
145 | } |
146 | ||
147 | return retVal; | |
148 | } | |
149 | ||
150 | @Override | |
151 | public void dispose() { | |
6a0ec7bc | 152 | fTrace.getIteratorManager().removeIterator(this); |
788ddcbc MK |
153 | } |
154 | ||
155 | /** | |
156 | * Seeks to a given timestamp. Wrapper to help CtfTmfTrace | |
53b235e1 MK |
157 | * |
158 | * @param timestamp | |
159 | * desired timestamp | |
788ddcbc MK |
160 | * @return success or not |
161 | */ | |
162 | public synchronized boolean seek(final long timestamp) { | |
b94865c3 | 163 | fCurLocation = new CtfLocation(timestamp, 0); |
53b235e1 | 164 | return getIterator().seek(timestamp); |
788ddcbc MK |
165 | } |
166 | ||
132a02b0 MK |
167 | /** |
168 | * Seeks to a given location. Wrapper to help CtfTmfTrace | |
169 | * @param location | |
170 | * unique location to find the event. | |
171 | * | |
172 | * @return success or not | |
173 | * @since 2.0 | |
174 | */ | |
f5df94f8 | 175 | public synchronized boolean seek(final CtfLocationInfo location) { |
b94865c3 | 176 | fCurLocation = new CtfLocation(location); |
132a02b0 MK |
177 | return getIterator().seek(location); |
178 | } | |
179 | ||
788ddcbc MK |
180 | // ------------------------------------------- |
181 | // Private helpers | |
182 | // ------------------------------------------- | |
81a2d02e | 183 | |
788ddcbc | 184 | /** |
53b235e1 MK |
185 | * Get iterator, called every time to get an iterator, no local copy is |
186 | * stored so that there is no need to "update" | |
788ddcbc | 187 | * |
53b235e1 | 188 | * @return an iterator |
788ddcbc MK |
189 | */ |
190 | private CtfIterator getIterator() { | |
6a0ec7bc | 191 | return fTrace.getIteratorManager().getIterator(this); |
788ddcbc | 192 | } |
788ddcbc | 193 | } |