1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.ui
.views
.resources
;
12 import java
.util
.Comparator
;
13 import java
.util
.Iterator
;
14 import java
.util
.NoSuchElementException
;
16 import org
.eclipse
.jdt
.annotation
.NonNull
;
17 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
20 * Caching iterator of time events with a couple extras.
22 * <li>peek() allows reading the head without removing it</li>
23 * <li>trim() allows removing a bit of the first event</li>
26 * @author Matthew Khouzam
28 class 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
;
33 public CachingIterator(@NonNull Iterator
<@NonNull ?
extends ITimeEvent
> iterator
, Comparator
<ITimeEvent
> comparator
) {
35 fComparator
= comparator
;
36 fEvent
= iterator
.hasNext() ? iterator
.next() : null;
40 public ITimeEvent
next() {
41 ITimeEvent retVal
= fEvent
;
42 fEvent
= fIterator
.hasNext() ? fIterator
.next() : null;
44 throw new NoSuchElementException("Iterator is empty"); //$NON-NLS-1$
50 public boolean hasNext() {
51 return fEvent
!= null;
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.
58 * @return the next element of the iterator
60 public ITimeEvent
peek() {
65 public int compareTo(CachingIterator o
) {
66 final ITimeEvent myEvent
= peek();
67 final ITimeEvent otherEvent
= o
.peek();
68 return fComparator
.compare(myEvent
, otherEvent
);
72 * Trims the next element in the iterator to be after a cut-off time.
76 * @return true if there was a trim
78 public boolean trim(long time
) {
79 if (time
<= fEvent
.getTime()) {
82 if (time
< fEvent
.getTime() + fEvent
.getDuration()) {
83 fEvent
= fEvent
.splitAfter(time
);
86 fEvent
= fIterator
.hasNext() ? fIterator
.next() : null;
This page took 0.031735 seconds and 5 git commands to generate.