• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
23 
24 /**
25  * An iterator that supports a one-element lookahead while iterating.
26  *
27  * <p>See the Guava User Guide article on <a href=
28  * "http://code.google.com/p/guava-libraries/wiki/CollectionHelpersExplained#PeekingIterator">
29  * {@code PeekingIterator}</a>.
30  *
31  * @author Mick Killianey
32  * @since 2.0 (imported from Google Collections Library)
33  */
34 @GwtCompatible
35 public interface PeekingIterator<E> extends Iterator<E> {
36   /**
37    * Returns the next element in the iteration, without advancing the iteration.
38    *
39    * <p>Calls to {@code peek()} should not change the state of the iteration,
40    * except that it <i>may</i> prevent removal of the most recent element via
41    * {@link #remove()}.
42    *
43    * @throws NoSuchElementException if the iteration has no more elements
44    *     according to {@link #hasNext()}
45    */
peek()46   E peek();
47 
48   /**
49    * {@inheritDoc}
50    *
51    * <p>The objects returned by consecutive calls to {@link #peek()} then {@link
52    * #next()} are guaranteed to be equal to each other.
53    */
54   @Override
next()55   E next();
56 
57   /**
58    * {@inheritDoc}
59    *
60    * <p>Implementations may or may not support removal when a call to {@link
61    * #peek()} has occurred since the most recent call to {@link #next()}.
62    *
63    * @throws IllegalStateException if there has been a call to {@link #peek()}
64    *     since the most recent call to {@link #next()} and this implementation
65    *     does not support this sequence of calls (optional)
66    */
67   @Override
remove()68   void remove();
69 }
70