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 // ------------------------------------------------------------------------
45 public Vector
<StreamInputPacketIndexEntry
> getEntries() {
50 * Gets an iterator to the entries
51 * @return an iterator to the entries
53 public ListIterator
<StreamInputPacketIndexEntry
> listIterator() {
54 return this.entries
.listIterator();
58 * Gets an iterator to the entries at a given position
59 * @param n the position to get
60 * @return the iterator
62 public ListIterator
<StreamInputPacketIndexEntry
> listIterator(int n
) {
63 return this.entries
.listIterator(n
);
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * Adds an entry to the index.
75 * @throws CTFReaderException
76 * If there was a problem reading the entry
78 public void addEntry(StreamInputPacketIndexEntry entry
)
79 throws CTFReaderException
{
80 assert (entry
.getContentSizeBits() != 0);
81 assert (entry
.getContentSizeBits() != 0);
83 if (entry
.getTimestampBegin() > entry
.getTimestampEnd()) {
84 throw new CTFReaderException(
85 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
88 if (!this.entries
.isEmpty()) {
89 if (entry
.getTimestampBegin() < this.entries
.lastElement()
90 .getTimestampBegin()) {
91 throw new CTFReaderException(
92 "Packets begin timestamp decreasing"); //$NON-NLS-1$
96 this.entries
.add(entry
);
100 * Given a timestamp, this methods returns the first PacketIndexEntry that
101 * could include the timestamp, that is the last packet with a begin
102 * timestamp smaller than the given timestamp.
105 * The timestamp to look for.
106 * @return The StreamInputPacketEntry that corresponds to the packet that
107 * includes the given timestamp.
109 public ListIterator
<StreamInputPacketIndexEntry
> search(final long timestamp
) {
111 * Start with min and max covering all the elements.
113 int max
= this.entries
.size() - 1;
117 StreamInputPacketIndexEntry guessEntry
= null;
120 * If the index is empty, return the iterator at the very beginning.
122 if( this.getEntries().isEmpty()) {
123 return this.getEntries().listIterator();
127 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
132 * Guess in the middle of min and max. The +1 is so that in case
133 * (min + 1 == max), we choose the packet at the subscript "max"
134 * instead of the one at "min". Otherwise, it would give an infinite
137 guessI
= (max
+ min
+ 1) / 2;
138 guessEntry
= this.entries
.get(guessI
);
141 * If we reached the point where we focus on a single packet, our
148 if (timestamp
< guessEntry
.getTimestampBegin()) {
150 * If the timestamp if before the begin timestamp, we know that
151 * the packet to return is before the guess.
154 } else if (timestamp
>= guessEntry
.getTimestampBegin()) {
156 * If the timestamp is after the begin timestamp, we know that
157 * the packet to return is after the guess or is the guess.
163 return this.entries
.listIterator(guessI
);