os.linux: Move Attributes class to internal package
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / analysis / os / linux / ui / views / resources / CachingIterator.java
CommitLineData
19ed6598
MK
1/*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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
10package org.eclipse.tracecompass.analysis.os.linux.ui.views.resources;
11
12import java.util.Comparator;
13import java.util.Iterator;
14import java.util.NoSuchElementException;
15
16import org.eclipse.jdt.annotation.NonNull;
17import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
18
19/**
20 * Caching iterator of time events with a couple extras.
21 * <ul>
22 * <li>peek() allows reading the head without removing it</li>
23 * <li>trim() allows removing a bit of the first event</li>
24 * </ul>
25 *
26 * @author Matthew Khouzam
27 */
28class CachingIterator implements Iterator<@NonNull ITimeEvent>, Comparable<CachingIterator> {
29 private ITimeEvent fEvent;
30 private @NonNull Iterator<@NonNull ? extends ITimeEvent> fIterator;
31 private final Comparator<ITimeEvent> fComparator;
32
33 public CachingIterator(@NonNull Iterator<@NonNull ? extends ITimeEvent> iterator, Comparator<ITimeEvent> comparator) {
34 fIterator = iterator;
35 fComparator = comparator;
36 fEvent = iterator.hasNext() ? iterator.next() : null;
37 }
38
39 @Override
40 public ITimeEvent next() {
41 ITimeEvent retVal = fEvent;
42 fEvent = fIterator.hasNext() ? fIterator.next() : null;
43 if (retVal == null) {
44 throw new NoSuchElementException("Iterator is empty"); //$NON-NLS-1$
45 }
46 return retVal;
47 }
48
49 @Override
50 public boolean hasNext() {
51 return fEvent != null;
52 }
53
54 /**
55 * Retrieves, but does not remove, the next element of this iterator, or
56 * returns {@code null} if this iterator does not have a next.
57 *
58 * @return the next element of the iterator
59 */
60 public ITimeEvent peek() {
61 return fEvent;
62 }
63
64 @Override
65 public int compareTo(CachingIterator o) {
66 final ITimeEvent myEvent = peek();
67 final ITimeEvent otherEvent = o.peek();
68 return fComparator.compare(myEvent, otherEvent);
69 }
70
71 /**
72 * Trims the next element in the iterator to be after a cut-off time.
73 *
74 * @param time
75 * the cut-off time
76 * @return true if there was a trim
77 */
78 public boolean trim(long time) {
79 if (time <= fEvent.getTime()) {
80 return false;
81 }
82 if (time < fEvent.getTime() + fEvent.getDuration()) {
83 fEvent = fEvent.splitAfter(time);
84 return true;
85 }
86 fEvent = fIterator.hasNext() ? fIterator.next() : null;
87 return true;
88 }
89
90}
This page took 0.029288 seconds and 5 git commands to generate.