001    /*
002     * Copyright (C) 2008 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.collect;
018    
019    import com.google.common.annotations.GwtCompatible;
020    
021    import java.util.Iterator;
022    import java.util.NoSuchElementException;
023    
024    /**
025     * An iterator that supports a one-element lookahead while iterating.
026     *
027     * @author Mick Killianey
028     * @since 2.0 (imported from Google Collections Library)
029     */
030    @GwtCompatible
031    public interface PeekingIterator<E> extends Iterator<E> {
032      /**
033       * Returns the next element in the iteration, without advancing the iteration.
034       *
035       * <p>Calls to {@code peek()} should not change the state of the iteration,
036       * except that it <i>may</i> prevent removal of the most recent element via
037       * {@link #remove()}.
038       *
039       * @throws NoSuchElementException if the iteration has no more elements
040       *     according to {@link #hasNext()}
041       */
042      E peek();
043    
044      /**
045       * {@inheritDoc}
046       *
047       * <p>The objects returned by consecutive calls to {@link #peek()} then {@link
048       * #next()} are guaranteed to be equal to each other.
049       */
050      @Override
051      E next();
052    
053      /**
054       * {@inheritDoc}
055       *
056       * <p>Implementations may or may not support removal when a call to {@link
057       * #peek()} has occurred since the most recent call to {@link #next()}.
058       *
059       * @throws IllegalStateException if there has been a call to {@link #peek()}
060       *     since the most recent call to {@link #next()} and this implementation
061       *     does not support this sequence of calls (optional)
062       */
063      @Override
064      void remove();
065    }