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
.ctf
.core
.trace
;
15 import java
.util
.Collection
;
16 import java
.util
.ListIterator
;
17 import java
.util
.Vector
;
20 * <b><u>StreamInputPacketIndex</u></b>
22 * TODO Implement me. Please.
24 public class StreamInputPacketIndex
{
26 // ------------------------------------------------------------------------
28 // ------------------------------------------------------------------------
31 * Entries of the index. They are sorted by increasing begin timestamp.
34 private final Vector
<StreamInputPacketIndexEntry
> entries
= new Vector
<StreamInputPacketIndexEntry
>();
36 // ------------------------------------------------------------------------
37 // Getters/Setters/Predicates
38 // ------------------------------------------------------------------------
40 public Collection
<StreamInputPacketIndexEntry
> getEntries() {
44 public ListIterator
<StreamInputPacketIndexEntry
> listIterator() {
45 return this.entries
.listIterator();
48 public ListIterator
<StreamInputPacketIndexEntry
> listIterator(int n
) {
49 return this.entries
.listIterator(n
);
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * Adds an entry to the index.
61 * @throws CTFReaderException
63 public void addEntry(StreamInputPacketIndexEntry entry
)
64 throws CTFReaderException
{
65 assert (entry
.packetSizeBits
!= 0);
66 assert (entry
.contentSizeBits
!= 0);
68 if (entry
.timestampBegin
> entry
.timestampEnd
) {
69 throw new CTFReaderException(
70 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
73 if (!this.entries
.isEmpty()) {
74 if (entry
.timestampBegin
< this.entries
.lastElement().timestampBegin
) {
75 throw new CTFReaderException(
76 "Packets begin timestamp decreasing"); //$NON-NLS-1$
80 this.entries
.add(entry
);
84 * Given a timestamp, this methods returns the first PacketIndexEntry that
85 * could include the timestamp, that is the last packet with a begin
86 * timestamp smaller than the given timestamp.
89 * The timestamp to look for.
90 * @return The StreamInputPacketEntry that corresponds to the packet that
91 * includes the given timestamp.
93 public ListIterator
<StreamInputPacketIndexEntry
> search(final long timestamp
) {
95 * Start with min and max covering all the elements.
97 int max
= this.entries
.size() - 1;
101 StreamInputPacketIndexEntry guessEntry
= null;
104 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
109 * Guess in the middle of min and max. The +1 is so that in case
110 * (min + 1 == max), we choose the packet at the subscript "max"
111 * instead of the one at "min". Otherwise, it would give an infinite
114 guessI
= (max
+ min
+ 1) / 2;
115 guessEntry
= this.entries
.get(guessI
);
118 * If we reached the point where we focus on a single packet, our
125 if (timestamp
< guessEntry
.timestampBegin
) {
127 * If the timestamp if before the begin timestamp, we know that
128 * the packet to return is before the guess.
131 } else if (timestamp
>= guessEntry
.timestampBegin
) {
133 * If the timestamp is after the begin timestamp, we know that
134 * the packet to return is after the guess or is the guess.
140 return this.entries
.listIterator(guessI
);