
com.google.cloud.AsyncPageImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gcloud-java-core Show documentation
Show all versions of gcloud-java-core Show documentation
Core module for the gcloud-java.
/*
* Copyright 2016 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.cloud;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.Uninterruptibles;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Base implementation for asynchronously consuming Google Cloud paginated results.
*
* @param the value type that the page holds
*/
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 {
Future> nextPage();
}
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 nextPage() {
try {
return asyncPageFetcher != null
? Uninterruptibles.getUninterruptibly(asyncPageFetcher.nextPage()) : null;
} catch (ExecutionException ex) {
throw Throwables.propagate(ex.getCause());
}
}
}
/**
* Creates an {@code AsyncPageImpl} object.
*/
public AsyncPageImpl(NextPageFetcher asyncPageFetcher, String cursor, Iterable results) {
super(new SyncNextPageFetcher(asyncPageFetcher), cursor, results);
this.asyncPageFetcher = asyncPageFetcher;
}
@Override
public Future> nextPageAsync() {
if (nextPageCursor() == null || asyncPageFetcher == null) {
return Futures.immediateCheckedFuture(null);
}
return asyncPageFetcher.nextPage();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy