1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
;
15 import java
.util
.ListIterator
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
21 * <b><u>StreamInputPacketIndex</u></b>
23 * TODO Implement me. Please.
25 public class StreamInputPacketIndex
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
32 * Entries of the index. They are sorted by increasing begin timestamp.
35 private final Vector
<StreamInputPacketIndexEntry
> entries
= new Vector
<StreamInputPacketIndexEntry
>();
37 // ------------------------------------------------------------------------
38 // Getters/Setters/Predicates
39 // ------------------------------------------------------------------------
46 public Vector
<StreamInputPacketIndexEntry
> getEntries() {
51 * Gets an iterator to the entries
53 * @return an iterator to the entries
55 public ListIterator
<StreamInputPacketIndexEntry
> listIterator() {
56 return this.entries
.listIterator();
60 * Gets an iterator to the entries at a given position
64 * @return the iterator
66 public ListIterator
<StreamInputPacketIndexEntry
> listIterator(int n
) {
67 return this.entries
.listIterator(n
);
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Adds an entry to the index.
79 * @throws CTFReaderException
80 * If there was a problem reading the entry
82 public void addEntry(StreamInputPacketIndexEntry entry
)
83 throws CTFReaderException
{
84 assert (entry
.getContentSizeBits() != 0);
85 assert (entry
.getContentSizeBits() != 0);
87 if (entry
.getTimestampBegin() > entry
.getTimestampEnd()) {
88 throw new CTFReaderException("Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
91 if (!this.entries
.isEmpty()) {
92 if (entry
.getTimestampBegin() < this.entries
.lastElement()
93 .getTimestampBegin()) {
94 throw new CTFReaderException("Packets begin timestamp decreasing"); //$NON-NLS-1$
98 this.entries
.add(entry
);
102 * Given a timestamp, this methods returns the first PacketIndexEntry that
103 * could include the timestamp, that is the last packet with a begin
104 * timestamp smaller than the given timestamp.
107 * The timestamp to look for.
108 * @return The StreamInputPacketEntry that corresponds to the packet that
109 * includes the given timestamp.
111 public ListIterator
<StreamInputPacketIndexEntry
> search(final long timestamp
) {
113 * Start with min and max covering all the elements.
115 int max
= this.entries
.size() - 1;
119 StreamInputPacketIndexEntry guessEntry
= null;
122 * If the index is empty, return the iterator at the very beginning.
124 if (this.getEntries().isEmpty()) {
125 return this.getEntries().listIterator();
129 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
134 * Guess in the middle of min and max. The +1 is so that in case
135 * (min + 1 == max), we choose the packet at the subscript "max"
136 * instead of the one at "min". Otherwise, it would give an infinite
139 guessI
= (max
+ min
+ 1) / 2;
140 guessEntry
= this.entries
.get(guessI
);
143 * If we reached the point where we focus on a single packet, our
150 if (timestamp
< guessEntry
.getTimestampBegin()) {
152 * If the timestamp if before the begin timestamp, we know that
153 * the packet to return is before the guess.
156 } else if (timestamp
> guessEntry
.getTimestampBegin()) {
158 * If the timestamp is after the begin timestamp, we know that
159 * the packet to return is after the guess or is the guess.
162 } else if (timestamp
== guessEntry
.getTimestampBegin()) {
164 * If the timestamp is equal to the begin timestamp, we want to
165 * return the first packetIndexEntry that have this timestamp.
168 StreamInputPacketIndexEntry previousGuessEntry
= this.entries
.get(guessI
- 1);
169 while (guessI
> 0 && guessEntry
.getTimestampBegin() == previousGuessEntry
.getTimestampBegin()) {
170 guessEntry
= previousGuessEntry
;
172 if (guessI
- 1 >= 0) {
173 previousGuessEntry
= this.entries
.get(guessI
- 1);
183 return this.entries
.listIterator(guessI
);