Remove unused sourceTemplateFeature folder from projects.
[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
9ac2eb62
MK
41 /**
42 * Gets the entries
43 * @return the entries
44 */
ce2388e0 45 public Vector<StreamInputPacketIndexEntry> getEntries() {
866e5b51
FC
46 return this.entries;
47 }
48
9ac2eb62
MK
49 /**
50 * Gets an iterator to the entries
51 * @return an iterator to the entries
52 */
866e5b51
FC
53 public ListIterator<StreamInputPacketIndexEntry> listIterator() {
54 return this.entries.listIterator();
55 }
56
9ac2eb62
MK
57 /**
58 * Gets an iterator to the entries at a given position
59 * @param n the position to get
60 * @return the iterator
61 */
866e5b51
FC
62 public ListIterator<StreamInputPacketIndexEntry> listIterator(int n) {
63 return this.entries.listIterator(n);
64 }
65
66 // ------------------------------------------------------------------------
67 // Operations
68 // ------------------------------------------------------------------------
69
70 /**
71 * Adds an entry to the index.
72 *
73 * @param entry
74 * The entry to add
75 * @throws CTFReaderException
be6df2d8 76 * If there was a problem reading the entry
866e5b51
FC
77 */
78 public void addEntry(StreamInputPacketIndexEntry entry)
79 throws CTFReaderException {
ce2388e0
FC
80 assert (entry.getContentSizeBits() != 0);
81 assert (entry.getContentSizeBits() != 0);
866e5b51 82
ce2388e0 83 if (entry.getTimestampBegin() > entry.getTimestampEnd()) {
866e5b51
FC
84 throw new CTFReaderException(
85 "Packet begin timestamp is after end timestamp"); //$NON-NLS-1$
86 }
87
88 if (!this.entries.isEmpty()) {
bfe038ff
MK
89 if (entry.getTimestampBegin() < this.entries.lastElement()
90 .getTimestampBegin()) {
866e5b51
FC
91 throw new CTFReaderException(
92 "Packets begin timestamp decreasing"); //$NON-NLS-1$
93 }
94 }
95
96 this.entries.add(entry);
97 }
98
99 /**
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.
103 *
104 * @param timestamp
105 * The timestamp to look for.
106 * @return The StreamInputPacketEntry that corresponds to the packet that
107 * includes the given timestamp.
108 */
109 public ListIterator<StreamInputPacketIndexEntry> search(final long timestamp) {
110 /*
111 * Start with min and max covering all the elements.
112 */
113 int max = this.entries.size() - 1;
114 int min = 0;
115
116 int guessI;
117 StreamInputPacketIndexEntry guessEntry = null;
118
4a110860
AM
119 /*
120 * If the index is empty, return the iterator at the very beginning.
121 */
122 if( this.getEntries().isEmpty()) {
123 return this.getEntries().listIterator();
124 }
125
866e5b51
FC
126 if (timestamp < 0) {
127 throw new IllegalArgumentException("timestamp is negative"); //$NON-NLS-1$
128 }
129
130 for (;;) {
131 /*
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
135 * loop.
136 */
137 guessI = (max + min + 1) / 2;
138 guessEntry = this.entries.get(guessI);
139
140 /*
141 * If we reached the point where we focus on a single packet, our
142 * search is done.
143 */
144 if (min == max) {
145 break;
146 }
147
ce2388e0 148 if (timestamp < guessEntry.getTimestampBegin()) {
866e5b51
FC
149 /*
150 * If the timestamp if before the begin timestamp, we know that
151 * the packet to return is before the guess.
152 */
153 max = guessI - 1;
ce2388e0 154 } else if (timestamp >= guessEntry.getTimestampBegin()) {
866e5b51
FC
155 /*
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.
158 */
159 min = guessI;
160 }
161 }
162
ce2388e0
FC
163 return this.entries.listIterator(guessI);
164 }
866e5b51 165}
This page took 0.066886 seconds and 5 git commands to generate.