Default iterator for simple implementations of DataPoints.
| 16 | |
| 17 | /** Default iterator for simple implementations of {@link DataPoints}. */ |
| 18 | final class DataPointsIterator implements SeekableView, DataPoint { |
| 19 | |
| 20 | /** Instance to iterate on. */ |
| 21 | private final DataPoints dp; |
| 22 | |
| 23 | /** Where are we in the iteration. */ |
| 24 | private short index = -1; |
| 25 | |
| 26 | /** |
| 27 | * Ctor. |
| 28 | * @param dp The data points to iterate on. |
| 29 | */ |
| 30 | DataPointsIterator(final DataPoints dp) { |
| 31 | this.dp = dp; |
| 32 | } |
| 33 | |
| 34 | // ------------------ // |
| 35 | // Iterator interface // |
| 36 | // ------------------ // |
| 37 | |
| 38 | public boolean hasNext() { |
| 39 | return index < dp.size() - 1; |
| 40 | } |
| 41 | |
| 42 | public DataPoint next() { |
| 43 | if (hasNext()) { |
| 44 | index++; |
| 45 | return this; |
| 46 | } |
| 47 | throw new NoSuchElementException("no more elements in " + this); |
| 48 | } |
| 49 | |
| 50 | public void remove() { |
| 51 | throw new UnsupportedOperationException(); |
| 52 | } |
| 53 | |
| 54 | // ---------------------- // |
| 55 | // SeekableView interface // |
| 56 | // ---------------------- // |
| 57 | |
| 58 | public void seek(final long timestamp) { |
| 59 | if ((timestamp & 0xFFFFFFFF00000000L) != 0) { // negative or not 32 bits |
| 60 | throw new IllegalArgumentException("invalid timestamp: " + timestamp); |
| 61 | } |
| 62 | // Do a binary search to find the timestamp given or the one right before. |
| 63 | short lo = 0; |
| 64 | short hi = (short) dp.size(); |
| 65 | |
| 66 | while (lo <= hi) { |
| 67 | index = (short) ((lo + hi) >>> 1); |
| 68 | long cmp = dp.timestamp(index) - timestamp; |
| 69 | |
| 70 | if (cmp < 0) { |
| 71 | lo = (short) (index + 1); |
| 72 | } else if (cmp > 0) { |
| 73 | hi = (short) (index - 1); |
| 74 | } else { |
| 75 | index--; // 'index' is exactly on the timestamp wanted. |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…