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 // ------------------------------------------------------------------------
41 public Vector
<StreamInputPacketIndexEntry
> getEntries() {
45 public ListIterator
<StreamInputPacketIndexEntry
> listIterator() {
46 return this.entries
.listIterator();
49 public ListIterator
<StreamInputPacketIndexEntry
> listIterator(int n
) {
50 return this.entries
.listIterator(n
);
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * Adds an entry to the index.
62 * @throws CTFReaderException
64 public void addEntry(StreamInputPacketIndexEntry entry
)
65 throws CTFReaderException
{
66 assert (entry
.getContentSizeBits() != 0);
67 assert (entry
.getContentSizeBits() != 0);
69 if (entry
.getTimestampBegin() > entry
.getTimestampEnd()) {
70 throw new CTFReaderException(
71 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
74 if (!this.entries
.isEmpty()) {
75 if (entry
.getTimestampBegin() < this.entries
.lastElement()
76 .getTimestampBegin()) {
77 throw new CTFReaderException(
78 "Packets begin timestamp decreasing"); //$NON-NLS-1$
82 this.entries
.add(entry
);
86 * Given a timestamp, this methods returns the first PacketIndexEntry that
87 * could include the timestamp, that is the last packet with a begin
88 * timestamp smaller than the given timestamp.
91 * The timestamp to look for.
92 * @return The StreamInputPacketEntry that corresponds to the packet that
93 * includes the given timestamp.
95 public ListIterator
<StreamInputPacketIndexEntry
> search(final long timestamp
) {
97 * Start with min and max covering all the elements.
99 int max
= this.entries
.size() - 1;
103 StreamInputPacketIndexEntry guessEntry
= null;
106 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
111 * Guess in the middle of min and max. The +1 is so that in case
112 * (min + 1 == max), we choose the packet at the subscript "max"
113 * instead of the one at "min". Otherwise, it would give an infinite
116 guessI
= (max
+ min
+ 1) / 2;
117 guessEntry
= this.entries
.get(guessI
);
120 * If we reached the point where we focus on a single packet, our
127 if (timestamp
< guessEntry
.getTimestampBegin()) {
129 * If the timestamp if before the begin timestamp, we know that
130 * the packet to return is before the guess.
133 } else if (timestamp
>= guessEntry
.getTimestampBegin()) {
135 * If the timestamp is after the begin timestamp, we know that
136 * the packet to return is after the guess or is the guess.
142 return this.entries
.listIterator(guessI
);