1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Contributors: Etienne Bergeron <etienne.bergeron@gmail.com>
12 * Contributors: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
;
17 import java
.util
.ListIterator
;
18 import java
.util
.Vector
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
23 * <b><u>StreamInputPacketIndex</u></b>
25 * TODO Implement me. Please.
27 public class StreamInputPacketIndex
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
34 * Entries of the index. They are sorted by increasing begin timestamp.
37 private final Vector
<StreamInputPacketIndexEntry
> entries
= new Vector
<>();
39 // ------------------------------------------------------------------------
40 // Getters/Setters/Predicates
41 // ------------------------------------------------------------------------
48 public Vector
<StreamInputPacketIndexEntry
> getEntries() {
53 * Gets an iterator to the entries
55 * @return an iterator to the entries
57 public ListIterator
<StreamInputPacketIndexEntry
> listIterator() {
58 return this.entries
.listIterator();
62 * Gets an iterator to the entries at a given position
66 * @return the iterator
68 public ListIterator
<StreamInputPacketIndexEntry
> listIterator(int n
) {
69 return this.entries
.listIterator(n
);
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * Adds an entry to the index.
81 * @throws CTFReaderException
82 * If there was a problem reading the entry
84 public void addEntry(StreamInputPacketIndexEntry entry
)
85 throws CTFReaderException
{
86 assert (entry
.getContentSizeBits() != 0);
88 /* Validate consistent entry. */
89 if (entry
.getTimestampBegin() > entry
.getTimestampEnd()) {
90 throw new CTFReaderException("Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
93 /* Validate entries are inserted in monotonic increasing timestamp order. */
94 if (!this.entries
.isEmpty()) {
95 if (entry
.getTimestampBegin() < this.entries
.lastElement()
96 .getTimestampBegin()) {
97 throw new CTFReaderException("Packets begin timestamp decreasing"); //$NON-NLS-1$
100 this.entries
.add(entry
);
104 * Returns the first PacketIndexEntry that could include the timestamp,
105 * that is the last packet with a begin timestamp smaller than the given timestamp.
108 * The timestamp to look for.
109 * @return The StreamInputPacketEntry that corresponds to the packet that
110 * includes the given timestamp.
112 public ListIterator
<StreamInputPacketIndexEntry
> search(final long timestamp
) {
114 * Start with min and max covering all the elements.
116 int max
= this.entries
.size() - 1;
120 StreamInputPacketIndexEntry guessEntry
= null;
123 * If the index is empty, return the iterator at the very beginning.
125 if (this.getEntries().isEmpty()) {
126 return this.getEntries().listIterator();
130 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
136 * Guess in the middle of min and max.
138 guessI
= min
+ ((max
- min
) / 2);
139 guessEntry
= this.entries
.get(guessI
);
142 * If we reached the point where we focus on a single packet, our
149 if (timestamp
<= guessEntry
.getTimestampEnd()) {
151 * If the timestamp is lower or equal to the end of the guess packet,
152 * then the guess packet becomes the new inclusive max.
157 * If the timestamp is greater than the end of the guess packet, then
158 * the new inclusive min is the packet after the guess packet.
164 return this.entries
.listIterator(guessI
);