rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / context / CtfTmfContext.java
CommitLineData
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 14package org.eclipse.tracecompass.tmf.ctf.core.context;
788ddcbc 15
fe71057b 16import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
2bdf0193
AM
17import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
18import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
9722e5d7
AM
19import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
20import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
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
788ddcbc 27 */
81a2d02e 28public class CtfTmfContext implements ITmfContext {
788ddcbc 29
788ddcbc
MK
30 // -------------------------------------------
31 // Fields
32 // -------------------------------------------
81a2d02e 33
b94865c3
MK
34 private CtfLocation fCurLocation;
35 private long fCurRank;
788ddcbc 36
53b235e1 37 private final CtfTmfTrace fTrace;
788ddcbc
MK
38
39 // -------------------------------------------
40 // Constructor
41 // -------------------------------------------
53b235e1
MK
42
43 /**
81a2d02e 44 * Constructor
53b235e1
MK
45 *
46 * @param ctfTmfTrace
47 * the parent trace
53b235e1 48 */
81a2d02e 49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace) {
53b235e1 50 fTrace = ctfTmfTrace;
b94865c3 51 fCurLocation = new CtfLocation(new CtfLocationInfo(0, 0));
788ddcbc
MK
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
b94865c3 60 return fCurRank;
788ddcbc
MK
61 }
62
63 @Override
51a7e2f6 64 public synchronized ITmfLocation getLocation() {
b94865c3 65 return fCurLocation;
788ddcbc
MK
66 }
67
68 @Override
69 public boolean hasValidRank() {
b94865c3 70 return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp();
788ddcbc
MK
71 }
72
73 @Override
51a7e2f6 74 public synchronized void setLocation(ITmfLocation location) {
544c79ad
PT
75 if (location instanceof CtfLocation) {
76 CtfIterator iterator = getIterator();
77 iterator.seek(((CtfLocation) location).getLocationInfo());
78 fCurLocation = iterator.getLocation();
79 } else {
80 fCurLocation = null;
575beffc 81 }
788ddcbc
MK
82 }
83
84 @Override
85 public void setRank(long rank) {
b94865c3 86 fCurRank = rank;
788ddcbc
MK
87
88 }
89
90 @Override
91 public void increaseRank() {
92 if (hasValidRank()) {
b94865c3 93 fCurRank++;
788ddcbc
MK
94 }
95 }
96
97 // -------------------------------------------
98 // CtfTmfTrace Helpers
99 // -------------------------------------------
100
81a2d02e
AM
101 /**
102 * Gets the trace of this context.
103 *
104 * @return The trace of this context
105 */
106 public CtfTmfTrace getTrace() {
107 return fTrace;
108 }
109
788ddcbc
MK
110 /**
111 * Gets the current event. Wrapper to help CtfTmfTrace
53b235e1 112 *
788ddcbc
MK
113 * @return The event or null
114 */
115 public synchronized CtfTmfEvent getCurrentEvent() {
53b235e1 116 return getIterator().getCurrentEvent();
788ddcbc
MK
117 }
118
119 /**
120 * Advances to a the next event. Wrapper to help CtfTmfTrace
53b235e1 121 *
788ddcbc
MK
122 * @return success or not
123 */
124 public synchronized boolean advance() {
b94865c3 125 final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo();
962fb72f
PT
126 CtfIterator iterator = getIterator();
127 boolean retVal = iterator.advance();
128 CtfTmfEvent currentEvent = iterator.getCurrentEvent();
132a02b0 129
788ddcbc 130 if (currentEvent != null) {
962fb72f 131 final long timestampValue = iterator.getCurrentTimestamp();
132a02b0 132 if (curLocationData.getTimestamp() == timestampValue) {
b94865c3 133 fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
132a02b0 134 } else {
b94865c3 135 fCurLocation = new CtfLocation(timestampValue, 0L);
132a02b0 136 }
788ddcbc 137 } else {
b94865c3 138 fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
788ddcbc
MK
139 }
140
141 return retVal;
142 }
143
144 @Override
145 public void dispose() {
0451d4d7 146 fTrace.disposeContext(this);
788ddcbc
MK
147 }
148
149 /**
150 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
53b235e1
MK
151 *
152 * @param timestamp
153 * desired timestamp
788ddcbc
MK
154 * @return success or not
155 */
156 public synchronized boolean seek(final long timestamp) {
544c79ad
PT
157 CtfIterator iterator = getIterator();
158 boolean ret = iterator.seek(timestamp);
159 fCurLocation = iterator.getLocation();
160 return ret;
788ddcbc
MK
161 }
162
132a02b0
MK
163 /**
164 * Seeks to a given location. Wrapper to help CtfTmfTrace
165 * @param location
166 * unique location to find the event.
167 *
168 * @return success or not
132a02b0 169 */
f5df94f8 170 public synchronized boolean seek(final CtfLocationInfo location) {
b94865c3 171 fCurLocation = new CtfLocation(location);
132a02b0
MK
172 return getIterator().seek(location);
173 }
174
788ddcbc
MK
175 // -------------------------------------------
176 // Private helpers
177 // -------------------------------------------
81a2d02e 178
788ddcbc 179 /**
53b235e1
MK
180 * Get iterator, called every time to get an iterator, no local copy is
181 * stored so that there is no need to "update"
788ddcbc 182 *
53b235e1 183 * @return an iterator
788ddcbc
MK
184 */
185 private CtfIterator getIterator() {
0451d4d7 186 return (CtfIterator) fTrace.createIteratorFromContext(this);
788ddcbc 187 }
788ddcbc 188}
This page took 0.091152 seconds and 5 git commands to generate.