Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine Guava's ForwardingListIterator with a PeekingIterator?

I want to use Guava to implement a "peekable" ListIterator that lets me peek at the previous and next list elements without moving the cursor. This is akin to Guava's PeekingIterator, only bidirectional because Guava's PeekingIterator only has a next() method, not a previous().

Does this need to be implemented by writing a new PeekingListIterator class or is there an Guava-intended way to combine the two concepts?

like image 650
Jeff Axelrod Avatar asked Dec 06 '22 16:12

Jeff Axelrod


1 Answers

Where's the value in introducing a new "peeking" concept to an iterator that's already easily scrollable in both directions?

If you really want it, you could just implement two trivial static helpers:

public static <T> T peekNext(ListIterator<T> iterator) {
  T next = iterator.next();
  iterator.previous();
  return next;
}

public static <T> T peekPrevious(ListIterator<T> iterator) {
  T previous = iterator.previous();
  iterator.next();
  return previous;
}
like image 110
Kevin Bourrillion Avatar answered May 24 '23 19:05

Kevin Bourrillion