Also posted on Stack Overflow.
I need a "peekable" ListIterator that lets me peek at the previous and next list elements without moving the cursor. This is akin to PeekingIterator, only bidirectional because PeekingIterator only has a next() method, not a previous().
Does this need to be implemented by my writing a new PeekingListIterator class or is there an Guava-intended way to combine the two concepts? If I write the class, I'll submit a change request, but I'm just surprised it's not already part of Guava.
Also, I'm not sure if this is a bug or by design, but in trying to figure out how this PeekableIterator works, I wrote the following test. It appears that peekingIterator doesn't copy the iterator passed in the parameter.
Instead it appears to keep a handle on the passed iterator. When peek() is called, it appears to be internally advancing to next() without going back to previous(), causing the following test to fail:
final ListIterator<String> listIterator = flavors.listIterator();
final PeekingIterator<String> peekingIterator = Iterators.peekingIterator(listIterator);
assertEquals("chocolate", peekingIterator.peek());
assertEquals("chocolate", listIterator.next()); // Fails, is vanilla.
iterator - the backing iterator. The PeekingIterator assumes ownership of this iterator, so users should cease making direct calls to it after calling this method.