AbstractLinkedIterator.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 * Copyright (C) 2010 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
17package com.google.common.collect;
18
19import com.google.common.annotations.Beta;
20import com.google.common.annotations.GwtCompatible;
21
22import java.util.NoSuchElementException;
23
24import javax.annotation.Nullable;
25
26/**
27 * This class provides a skeletal implementation of the {@code Iterator}
28 * interface for sequences whose next element can always be derived from the
29 * previous element. Null elements are not supported, nor is the
30 * {@link #remove()} method.
31 *
32 * <p>Example: <pre>   {@code
33 *
34 *   Iterator<Integer> powersOfTwo = new AbstractLinkedIterator<Integer>(1) {
35 *     protected Integer computeNext(Integer previous) {
36 *       return (previous == 1 << 30) ? null : previous * 2;
37 *     }
38 *   };}</pre>
39 *
40 * @author Chris Povirk
41 * @since 8.0
42 */
43@Beta
44@GwtCompatible
45public abstract class AbstractLinkedIterator<T>
46    extends UnmodifiableIterator<T> {
47  private T nextOrNull;
48
49  /**
50   * Creates a new iterator with the given first element, or, if {@code
51   * firstOrNull} is null, creates a new empty iterator.
52   */
53  protected AbstractLinkedIterator(@Nullable T firstOrNull) {
54    this.nextOrNull = firstOrNull;
55  }
56
57  /**
58   * Returns the element that follows {@code previous}, or returns {@code null}
59   * if no elements remain. This method is invoked during each call to
60   * {@link #next()} in order to compute the result of a <i>future</i> call to
61   * {@code next()}.
62   */
63  protected abstract T computeNext(T previous);
64
65  @Override
66  public final boolean hasNext() {
67    return nextOrNull != null;
68  }
69
70  @Override
71  public final T next() {
72    if (!hasNext()) {
73      throw new NoSuchElementException();
74    }
75    try {
76      return nextOrNull;
77    } finally {
78      nextOrNull = computeNext(nextOrNull);
79    }
80  }
81}
82