Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / AbstractIterator.java
blobd3ad38ef5edff9947bd8e337b33d622fbe89a946
1 // Copyright 2009 Google Inc. All Rights Reserved.
2 package com.google.appengine.api.datastore;
4 import java.util.Iterator;
5 import java.util.NoSuchElementException;
7 /**
8 * @see com.google.common.collect.AbstractIterator
9 */
10 abstract class AbstractIterator<T> implements Iterator<T> {
11 private State state = State.NOT_READY;
13 enum State {
14 READY,
15 NOT_READY,
16 DONE,
17 FAILED,
20 private T next;
22 protected abstract T computeNext();
24 protected final T endOfData() {
25 state = State.DONE;
26 return null;
29 @Override
30 public final boolean hasNext() {
31 if (state == State.FAILED) {
32 throw new IllegalStateException();
34 switch (state) {
35 case DONE:
36 return false;
37 case READY:
38 return true;
39 default:
41 return tryToComputeNext();
44 private boolean tryToComputeNext() {
45 state = State.FAILED;
46 next = computeNext();
47 if (state != State.DONE) {
48 state = State.READY;
49 return true;
51 return false;
54 @Override
55 public final T next() {
56 if (!hasNext()) {
57 throw new NoSuchElementException();
59 state = State.NOT_READY;
60 return next;
63 @Override
64 public void remove() {
65 throw new UnsupportedOperationException();