11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2010 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License.
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.NoSuchElementException;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport javax.annotation.Nullable;
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * This class provides a skeletal implementation of the {@code Iterator}
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * interface for sequences whose next element can always be derived from the
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * previous element. Null elements are not supported, nor is the
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link #remove()} method.
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Example: <pre>   {@code
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   Iterator<Integer> powersOfTwo = new AbstractLinkedIterator<Integer>(1) {
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     protected Integer computeNext(Integer previous) {
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *       return (previous == 1 << 30) ? null : previous * 2;
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *     }
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *   };}</pre>
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Chris Povirk
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 8.0
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@Beta
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic abstract class AbstractLinkedIterator<T>
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    extends UnmodifiableIterator<T> {
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private T nextOrNull;
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Creates a new iterator with the given first element, or, if {@code
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * firstOrNull} is null, creates a new empty iterator.
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected AbstractLinkedIterator(@Nullable T firstOrNull) {
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    this.nextOrNull = firstOrNull;
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns the element that follows {@code previous}, or returns {@code null}
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * if no elements remain. This method is invoked during each call to
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link #next()} in order to compute the result of a <i>future</i> call to
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code next()}.
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  protected abstract T computeNext(T previous);
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public final boolean hasNext() {
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return nextOrNull != null;
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public final T next() {
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    if (!hasNext()) {
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      throw new NoSuchElementException();
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    try {
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      return nextOrNull;
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    } finally {
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      nextOrNull = computeNext(nextOrNull);
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
82