|
| 1 | +/* |
| 2 | + * Copyright (C) 2007 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 javax.annotation.CheckForNull; |
| 20 | + |
| 21 | +import java.util.NoSuchElementException; |
| 22 | + |
| 23 | +import static com.google.common.base.Preconditions.checkState; |
| 24 | +import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT; |
| 25 | + |
| 26 | +/** |
| 27 | + * This class provides a skeletal implementation of the {@code Iterator} interface, to make this |
| 28 | + * interface easier to implement for certain types of data sources. |
| 29 | + * |
| 30 | + * <p>{@code Iterator} requires its implementations to support querying the end-of-data status |
| 31 | + * without changing the iterator's state, using the {@link #hasNext} method. But many data sources, |
| 32 | + * such as {@link java.io.Reader#read()}, do not expose this information; the only way to discover |
| 33 | + * whether there is any data left is by trying to retrieve it. These types of data sources are |
| 34 | + * ordinarily difficult to write iterators for. But using this class, one must implement only the |
| 35 | + * {@link #computeNext} method, and invoke the {@link #endOfData} method when appropriate. |
| 36 | + * |
| 37 | + * <p>Another example is an iterator that skips over null elements in a backing iterator. This could |
| 38 | + * be implemented as: |
| 39 | + * |
| 40 | + * <pre>{@code |
| 41 | + * public static Iterator<String> skipNulls(final Iterator<String> in) { |
| 42 | + * return new AbstractIterator<String>() { |
| 43 | + * protected String computeNext() { |
| 44 | + * while (in.hasNext()) { |
| 45 | + * String s = in.next(); |
| 46 | + * if (s != null) { |
| 47 | + * return s; |
| 48 | + * } |
| 49 | + * } |
| 50 | + * return endOfData(); |
| 51 | + * } |
| 52 | + * }; |
| 53 | + * } |
| 54 | + * }</pre> |
| 55 | + * |
| 56 | + * <p>This class supports iterators that include null elements. |
| 57 | + * |
| 58 | + * @author Kevin Bourrillion |
| 59 | + * @since 2.0 |
| 60 | + */ |
| 61 | +// When making changes to this class, please also update the copy at |
| 62 | +// com.google.common.base.AbstractIterator |
| 63 | +public abstract class AbstractIterator<T extends Object> extends UnmodifiableIterator<T> { |
| 64 | + private State state = State.NOT_READY; |
| 65 | + |
| 66 | + /** Constructor for use by subclasses. */ |
| 67 | + protected AbstractIterator() {} |
| 68 | + |
| 69 | + private enum State { |
| 70 | + /** We have computed the next element and haven't returned it yet. */ |
| 71 | + READY, |
| 72 | + |
| 73 | + /** We haven't yet computed or have already returned the element. */ |
| 74 | + NOT_READY, |
| 75 | + |
| 76 | + /** We have reached the end of the data and are finished. */ |
| 77 | + DONE, |
| 78 | + |
| 79 | + /** We've suffered an exception and are kaput. */ |
| 80 | + FAILED, |
| 81 | + } |
| 82 | + |
| 83 | + @CheckForNull private T next; |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the next element. <b>Note:</b> the implementation must call {@link #endOfData()} when |
| 87 | + * there are no elements left in the iteration. Failure to do so could result in an infinite loop. |
| 88 | + * |
| 89 | + * <p>The initial invocation of {@link #hasNext()} or {@link #next()} calls this method, as does |
| 90 | + * the first invocation of {@code hasNext} or {@code next} following each successful call to |
| 91 | + * {@code next}. Once the implementation either invokes {@code endOfData} or throws an exception, |
| 92 | + * {@code computeNext} is guaranteed to never be called again. |
| 93 | + * |
| 94 | + * <p>If this method throws an exception, it will propagate outward to the {@code hasNext} or |
| 95 | + * {@code next} invocation that invoked this method. Any further attempts to use the iterator will |
| 96 | + * result in an {@link IllegalStateException}. |
| 97 | + * |
| 98 | + * <p>The implementation of this method may not invoke the {@code hasNext}, {@code next}, or |
| 99 | + * {@link #peek()} methods on this instance; if it does, an {@code IllegalStateException} will |
| 100 | + * result. |
| 101 | + * |
| 102 | + * @return the next element if there was one. If {@code endOfData} was called during execution, |
| 103 | + * the return value will be ignored. |
| 104 | + * @throws RuntimeException if any unrecoverable error happens. This exception will propagate |
| 105 | + * outward to the {@code hasNext()}, {@code next()}, or {@code peek()} invocation that invoked |
| 106 | + * this method. Any further attempts to use the iterator will result in an {@link |
| 107 | + * IllegalStateException}. |
| 108 | + */ |
| 109 | + @CheckForNull |
| 110 | + protected abstract T computeNext(); |
| 111 | + |
| 112 | + /** |
| 113 | + * Implementations of {@link #computeNext} <b>must</b> invoke this method when there are no |
| 114 | + * elements left in the iteration. |
| 115 | + * |
| 116 | + * @return {@code null}; a convenience so your {@code computeNext} implementation can use the |
| 117 | + * simple statement {@code return endOfData();} |
| 118 | + */ |
| 119 | + @CheckForNull |
| 120 | + protected final T endOfData() { |
| 121 | + state = State.DONE; |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public final boolean hasNext() { |
| 127 | + checkState(state != State.FAILED); |
| 128 | + switch (state) { |
| 129 | + case DONE: |
| 130 | + return false; |
| 131 | + case READY: |
| 132 | + return true; |
| 133 | + default: |
| 134 | + } |
| 135 | + return tryToComputeNext(); |
| 136 | + } |
| 137 | + |
| 138 | + private boolean tryToComputeNext() { |
| 139 | + state = State.FAILED; // temporary pessimism |
| 140 | + next = computeNext(); |
| 141 | + if (state != State.DONE) { |
| 142 | + state = State.READY; |
| 143 | + return true; |
| 144 | + } |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public final T next() { |
| 150 | + if (!hasNext()) { |
| 151 | + throw new NoSuchElementException(); |
| 152 | + } |
| 153 | + state = State.NOT_READY; |
| 154 | + // Safe because hasNext() ensures that tryToComputeNext() has put a T into `next`. |
| 155 | + T result = uncheckedCastNullableTToT(next); |
| 156 | + next = null; |
| 157 | + return result; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the next element in the iteration without advancing the iteration, according to the |
| 162 | + * contract of {@link PeekingIterator#peek()}. |
| 163 | + * |
| 164 | + * <p>Implementations of {@code AbstractIterator} that wish to expose this functionality should |
| 165 | + * implement {@code PeekingIterator}. |
| 166 | + */ |
| 167 | + public final T peek() { |
| 168 | + if (!hasNext()) { |
| 169 | + throw new NoSuchElementException(); |
| 170 | + } |
| 171 | + // Safe because hasNext() ensures that tryToComputeNext() has put a T into `next`. |
| 172 | + return uncheckedCastNullableTToT(next); |
| 173 | + } |
| 174 | +} |
0 commit comments