All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.topicus.jdbc.shaded.com.google.cloud.AsyncPageImpl Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright 2016 Google LLC
 *
 * 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 nl.topicus.jdbc.shaded.com.google.cloud;

import nl.topicus.jdbc.shaded.com.google.api.core.ApiFuture;
import nl.topicus.jdbc.shaded.com.google.api.core.ApiFutures;
import nl.topicus.jdbc.shaded.com.google.api.core.InternalApi;
import nl.topicus.jdbc.shaded.com.google.api.gax.paging.AsyncPage;
import nl.topicus.jdbc.shaded.com.google.api.gax.paging.Page;
import nl.topicus.jdbc.shaded.com.google.common.base.Throwables;
import nl.topicus.jdbc.shaded.com.google.common.util.concurrent.Uninterruptibles;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;

/**
 * Base implementation for asynchronously consuming Google Cloud paginated results.
 *
 * @param  the value type that the page holds
 */
@InternalApi
public class AsyncPageImpl extends PageImpl implements AsyncPage {

  private static final long serialVersionUID = -6009473188630364906L;

  private final NextPageFetcher asyncPageFetcher;

  /**
   * Interface for asynchronously fetching the next page of results from the service.
   *
   * @param  the value type that the page holds
   */
  public interface NextPageFetcher extends Serializable {

    ApiFuture> getNextPage();
  }

  private static class SyncNextPageFetcher implements PageImpl.NextPageFetcher {

    private static final long serialVersionUID = -4124568632363525351L;

    private final NextPageFetcher asyncPageFetcher;

    private SyncNextPageFetcher(NextPageFetcher asyncPageFetcher) {
      this.asyncPageFetcher = asyncPageFetcher;
    }

    @Override
    public Page getNextPage() {
      try {
        return asyncPageFetcher != null
            ? Uninterruptibles.getUninterruptibly(asyncPageFetcher.getNextPage()) : null;
      } catch (ExecutionException ex) {
        Throwables.throwIfUnchecked(ex.getCause());
        throw new RuntimeException(ex);
      }
    }
  }

  /**
   * Creates an {@code AsyncPageImpl} object.
   */
  public AsyncPageImpl(NextPageFetcher asyncPageFetcher, String cursor, Iterable results) {
    super(new SyncNextPageFetcher(asyncPageFetcher), cursor, results);
    this.asyncPageFetcher = asyncPageFetcher;
  }


  @Override
  public ApiFuture> getNextPageAsync() {
    if (getNextPageToken() == null || asyncPageFetcher == null) {
      return ApiFutures.immediateFuture(null);
    }
    return asyncPageFetcher.getNextPage();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy