/*
 * 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.
 *
 * @param <T> the value type that the page holds.
 */
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;

  /**
   * Interface for fetching the next page of results from the service.
   *
   * @param <T> the value type that the page holds.
   */
  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() {
      while (!currentPageIterator.hasNext()) {
        currentPage = currentPage.nextPage();
        if (currentPage == null) {
          return endOfData();
        }
        currentPageIterator = currentPage.values().iterator();
      }
      return currentPageIterator.next();
    }
  }

  /**
   * 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.<T>emptyList() : 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);
  }
}

@@ -690,16 +690,63 @@
 terator;
+%0Aimport com.google.common.collect.ImmutableMap;
 %0A%0Aimport
@@ -821,24 +821,46 @@
 l.Iterator;%0A
+import java.util.Map;%0A
 import java.
@@ -2628,17 +2628,16 @@
 terator%3C
-T
 %3E(this);
@@ -2633,24 +2633,24 @@
 or%3C%3E(this);%0A
+
   %7D%0A%0A  @Over
@@ -3159,16 +3159,16 @@
 cursor)%0A
-
         
@@ -3213,11 +3213,945 @@
 ts);%0A  %7D
+%0A%0A  /**%0A   * Utility method to construct the options map for the next page request.%0A   *%0A   * @param %3CT%3E the value type that the page holds. Instances of %7B@code T%7D should be%0A   *     %7B@code Serializable%7D%0A   * @param pageTokenOption the key for the next page cursor option in the options map%0A   * @param cursor the cursor for the next page%0A   * @param optionMap the previous options map%0A   * @return the options map for the next page request%0A   */%0A  public static %3CT%3E Map%3CT, Object%3E nextRequestOptions(%0A      T pageTokenOption, String cursor, Map%3CT, ?%3E optionMap) %7B%0A    ImmutableMap.Builder%3CT, Object%3E builder = ImmutableMap.builder();%0A    if (cursor != null) %7B%0A      builder.put(pageTokenOption, cursor);%0A    %7D%0A    for (Map.Entry%3CT, ?%3E option : optionMap.entrySet()) %7B%0A      if (!Objects.equals(option.getKey(), pageTokenOption)) %7B%0A        builder.put(option.getKey(), option.getValue());%0A      %7D%0A    %7D%0A    return builder.build();%0A  %7D
 %0A%7D%0A

Refactor page fetchers