String comparison may return any integer (not only 0, 1 and -1)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / internal / ctf / core / trace / StreamInputPacketIndex.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
ce2388e0 13package org.eclipse.linuxtools.internal.ctf.core.trace;
866e5b51 14
866e5b51
FC
15import java.util.ListIterator;
16import java.util.Vector;
17
ce2388e0
FC
18import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19
866e5b51
FC
20/**
21 * <b><u>StreamInputPacketIndex</u></b>
22 * <p>
23 * TODO Implement me. Please.
24 */
25public class StreamInputPacketIndex {
26
27 // ------------------------------------------------------------------------
28 // Attributes
29 // ------------------------------------------------------------------------
30
31 /**
32 * Entries of the index. They are sorted by increasing begin timestamp.
33 * index builder.
34 */
35 private final Vector<StreamInputPacketIndexEntry> entries = new Vector<StreamInputPacketIndexEntry>();
36
37 // ------------------------------------------------------------------------
38 // Getters/Setters/Predicates
39 // ------------------------------------------------------------------------
40
ce2388e0 41 public Vector<StreamInputPacketIndexEntry> getEntries() {
866e5b51
FC
42 return this.entries;
43 }
44
45 public ListIterator<StreamInputPacketIndexEntry> listIterator() {
46 return this.entries.listIterator();
47 }
48
49 public ListIterator<StreamInputPacketIndexEntry> listIterator(int n) {
50 return this.entries.listIterator(n);
51 }
52
53 // ------------------------------------------------------------------------
54 // Operations
55 // ------------------------------------------------------------------------
56
57 /**
58 * Adds an entry to the index.
59 *
60 * @param entry
61 * The entry to add
62 * @throws CTFReaderException
63 */
64 public void addEntry(StreamInputPacketIndexEntry entry)
65 throws CTFReaderException {
ce2388e0
FC
66 assert (entry.getContentSizeBits() != 0);
67 assert (entry.getContentSizeBits() != 0);
866e5b51 68
ce2388e0 69 if (entry.getTimestampBegin() > entry.getTimestampEnd()) {
866e5b51
FC
70 throw new CTFReaderException(
71 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
72 }
73
74 if (!this.entries.isEmpty()) {
bfe038ff
MK
75 if (entry.getTimestampBegin() < this.entries.lastElement()
76 .getTimestampBegin()) {
866e5b51
FC
77 throw new CTFReaderException(
78 "Packets begin timestamp decreasing"); //$NON-NLS-1$
79 }
80 }
81
82 this.entries.add(entry);
83 }
84
85 /**
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.
89 *
90 * @param timestamp
91 * The timestamp to look for.
92 * @return The StreamInputPacketEntry that corresponds to the packet that
93 * includes the given timestamp.
94 */
95 public ListIterator<StreamInputPacketIndexEntry> search(final long timestamp) {
96 /*
97 * Start with min and max covering all the elements.
98 */
99 int max = this.entries.size() - 1;
100 int min = 0;
101
102 int guessI;
103 StreamInputPacketIndexEntry guessEntry = null;
104
4a110860
AM
105 /*
106 * If the index is empty, return the iterator at the very beginning.
107 */
108 if( this.getEntries().isEmpty()) {
109 return this.getEntries().listIterator();
110 }
111
866e5b51
FC
112 if (timestamp < 0) {
113 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
114 }
115
116 for (;;) {
117 /*
118 * Guess in the middle of min and max. The +1 is so that in case
119 * (min + 1 == max), we choose the packet at the subscript "max"
120 * instead of the one at "min". Otherwise, it would give an infinite
121 * loop.
122 */
123 guessI = (max + min + 1) / 2;
124 guessEntry = this.entries.get(guessI);
125
126 /*
127 * If we reached the point where we focus on a single packet, our
128 * search is done.
129 */
130 if (min == max) {
131 break;
132 }
133
ce2388e0 134 if (timestamp < guessEntry.getTimestampBegin()) {
866e5b51
FC
135 /*
136 * If the timestamp if before the begin timestamp, we know that
137 * the packet to return is before the guess.
138 */
139 max = guessI - 1;
ce2388e0 140 } else if (timestamp >= guessEntry.getTimestampBegin()) {
866e5b51
FC
141 /*
142 * If the timestamp is after the begin timestamp, we know that
143 * the packet to return is after the guess or is the guess.
144 */
145 min = guessI;
146 }
147 }
148
ce2388e0
FC
149 return this.entries.listIterator(guessI);
150 }
866e5b51 151}
This page took 0.035454 seconds and 5 git commands to generate.