/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gcloud;

import com.google.common.collect.AbstractIterator;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Objects;

/**
 * Base implementation for Google Cloud paginated results.
 */
public class PageImpl<T> implements Page<T>, Serializable {

  private static final long serialVersionUID = 3914827379823557934L;

  private final String cursor;
  private final Iterable<T> results;
  private final NextPageFetcher<T> pageFetcher;

  public interface NextPageFetcher<T> extends Serializable {
    Page<T> nextPage();
  }

  static class PageIterator<T> extends AbstractIterator<T> {

    private Iterator<T> currentPageIterator;
    private Page<T> currentPage;

    PageIterator(Page<T> currentPage) {
      this.currentPageIterator = currentPage.values().iterator();
      this.currentPage = currentPage;
    }

    @Override
    protected T computeNext() {
      if (currentPageIterator.hasNext()) {
        return currentPageIterator.next();
      }
      Page<T> nextPage = currentPage.nextPage();
      if (nextPage != null) {
        currentPage = nextPage;
        currentPageIterator = currentPage.values().iterator();
        if (currentPageIterator.hasNext()) {
          return currentPageIterator.next();
        }
      }
      return endOfData();
    }
  }

  /**
   * Creates a {@code PageImpl} object. In order for the object to be serializable the {@code
   * results} parameter must be serializable.
   */
  public PageImpl(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) {
    this.pageFetcher = pageFetcher;
    this.cursor = cursor;
    this.results = results;
  }

  @Override
  public Iterable<T> values() {
    return results == null ? Collections.EMPTY_LIST : results;
  }

  @Override
  public Iterator<T> iterateAll() {
    return new PageIterator<T>(this);
  }

  @Override
  public String nextPageCursor() {
    return cursor;
  }

  @Override
  public Page<T> nextPage() {
    if (cursor == null || pageFetcher == null) {
      return null;
    }
    return pageFetcher.nextPage();
  }

  @Override
  public int hashCode() {
    return Objects.hash(cursor, results);
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof PageImpl)) {
      return false;
    }
    PageImpl<?> other = (PageImpl<?>) obj;
    return Objects.equals(cursor, other.cursor)
        && Objects.equals(results, other.results);
  }
}

@@ -1550,28 +1550,32 @@
 t() %7B%0A      
-if
+while
  (
+!
 currentPageI
@@ -1595,39 +1595,32 @@
 xt()) %7B%0A        
-return 
 currentPageItera
@@ -1618,55 +1618,8 @@
 Page
-Iterator.next();%0A      %7D%0A      Page%3CT%3E nextPage
  = c
@@ -1637,24 +1637,26 @@
 nextPage();%0A
+  
       if (ne
@@ -1657,18 +1657,21 @@
 if (
-nex
+curren
 tPage 
-!
+=
 = nu
@@ -1688,31 +1688,39 @@
     
-currentPage = nextPage;
+  return endOfData();%0A        %7D
 %0A   
@@ -1789,132 +1789,47 @@
     
-  if (currentPageIterator.hasNext()) %7B%0A          return currentPageIterator.next();%0A        %7D%0A      %7D%0A      return endOfData
+%7D%0A      return currentPageIterator.next
 ();%0A

Add loop for empty pages in PageImpl.iterateAll