| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012 Ericsson |
| 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 | * |
| 9 | * Contributors: Matthew Khouzam - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
| 13 | |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.ListIterator; |
| 16 | |
| 17 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
| 19 | |
| 20 | /** |
| 21 | * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to |
| 22 | * a boxed Long, a long and an int. |
| 23 | * |
| 24 | * @version 1.0 |
| 25 | * @author Matthew Khouzam |
| 26 | */ |
| 27 | public class CtfTmfLightweightContext implements ITmfContext { |
| 28 | |
| 29 | // ------------------------------------------- |
| 30 | // Fields |
| 31 | // ------------------------------------------- |
| 32 | private CtfLocation curLocation; |
| 33 | private long curRank; |
| 34 | |
| 35 | private final CtfTmfTrace fTrace; |
| 36 | |
| 37 | // ------------------------------------------- |
| 38 | // Constructor |
| 39 | // ------------------------------------------- |
| 40 | /** |
| 41 | * Deprecated, use CtfTmfLightweightContext( CtfTmfTrace please ) |
| 42 | * |
| 43 | * @param iters |
| 44 | * the shared iterator pool. |
| 45 | * @param pos |
| 46 | * the iterator position. |
| 47 | */ |
| 48 | @Deprecated |
| 49 | public CtfTmfLightweightContext(ArrayList<CtfIterator> iters, |
| 50 | ListIterator<CtfIterator> pos) { |
| 51 | fTrace = iters.get(0).getCtfTmfTrace(); |
| 52 | curLocation = new CtfLocation(new CtfLocationData(0, 0)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * |
| 57 | * @param ctfTmfTrace |
| 58 | * the parent trace |
| 59 | * @since 1.1 |
| 60 | */ |
| 61 | public CtfTmfLightweightContext(CtfTmfTrace ctfTmfTrace) { |
| 62 | fTrace = ctfTmfTrace; |
| 63 | curLocation = new CtfLocation(new CtfLocationData(0, 0)); |
| 64 | } |
| 65 | |
| 66 | // ------------------------------------------- |
| 67 | // TmfContext Overrides |
| 68 | // ------------------------------------------- |
| 69 | |
| 70 | @Override |
| 71 | public long getRank() { |
| 72 | return curRank; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public ITmfLocation<? extends Comparable<?>> getLocation() { |
| 77 | return curLocation; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean hasValidRank() { |
| 82 | return curRank != CtfLocation.INVALID_LOCATION.getTimestamp(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void setLocation(ITmfLocation<? extends Comparable<?>> location) { |
| 87 | curLocation = (CtfLocation) location; |
| 88 | getIterator().seek(curLocation.getLocation()); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void setRank(long rank) { |
| 93 | curRank = rank; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void increaseRank() { |
| 99 | if (hasValidRank()) { |
| 100 | curRank++; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // ------------------------------------------- |
| 105 | // CtfTmfTrace Helpers |
| 106 | // ------------------------------------------- |
| 107 | |
| 108 | /** |
| 109 | * Gets the current event. Wrapper to help CtfTmfTrace |
| 110 | * |
| 111 | * @return The event or null |
| 112 | */ |
| 113 | public synchronized CtfTmfEvent getCurrentEvent() { |
| 114 | return getIterator().getCurrentEvent(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Advances to a the next event. Wrapper to help CtfTmfTrace |
| 119 | * |
| 120 | * @return success or not |
| 121 | */ |
| 122 | public synchronized boolean advance() { |
| 123 | final CtfLocationData curLocationData = this.curLocation.getLocation(); |
| 124 | boolean retVal = getIterator().advance(); |
| 125 | CtfTmfEvent currentEvent = getIterator().getCurrentEvent(); |
| 126 | |
| 127 | if (currentEvent != null) { |
| 128 | final long timestampValue = currentEvent.getTimestampValue(); |
| 129 | if (curLocationData.getTimestamp() == timestampValue) { |
| 130 | curLocation.setLocation(timestampValue, curLocationData.getIndex() + 1); |
| 131 | } else { |
| 132 | curLocation.setLocation(timestampValue, 0L); |
| 133 | } |
| 134 | } else { |
| 135 | curLocation.setLocation(CtfLocation.INVALID_LOCATION); |
| 136 | } |
| 137 | |
| 138 | return retVal; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void dispose() { |
| 143 | // do nothing |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Seeks to a given timestamp. Wrapper to help CtfTmfTrace |
| 148 | * |
| 149 | * @param timestamp |
| 150 | * desired timestamp |
| 151 | * @return success or not |
| 152 | */ |
| 153 | public synchronized boolean seek(final long timestamp) { |
| 154 | curLocation.setLocation(timestamp, 0); |
| 155 | return getIterator().seek(timestamp); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Seeks to a given location. Wrapper to help CtfTmfTrace |
| 160 | * @param location |
| 161 | * unique location to find the event. |
| 162 | * |
| 163 | * @return success or not |
| 164 | * @since 2.0 |
| 165 | */ |
| 166 | public synchronized boolean seek(final CtfLocationData location) { |
| 167 | curLocation.setLocation(location); |
| 168 | return getIterator().seek(location); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * (non-Javadoc) |
| 173 | * |
| 174 | * @see java.lang.Object#clone() |
| 175 | */ |
| 176 | @Override |
| 177 | public CtfTmfLightweightContext clone() { |
| 178 | CtfTmfLightweightContext ret = new CtfTmfLightweightContext(fTrace); |
| 179 | ret.curLocation = curLocation.clone(); |
| 180 | ret.curRank = curRank; |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | // ------------------------------------------- |
| 185 | // Private helpers |
| 186 | // ------------------------------------------- |
| 187 | /** |
| 188 | * Get iterator, called every time to get an iterator, no local copy is |
| 189 | * stored so that there is no need to "update" |
| 190 | * |
| 191 | * @return an iterator |
| 192 | */ |
| 193 | private CtfIterator getIterator() { |
| 194 | return CtfIteratorManager.getIterator(fTrace, this); |
| 195 | } |
| 196 | } |