diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java index a65490998c17..9bc89206889b 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java @@ -38,6 +38,7 @@ import com.google.gcloud.ExceptionHandler.Interceptor; import com.google.gcloud.Page; import com.google.gcloud.PageImpl; +import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.RetryHelper; import com.google.gcloud.bigquery.InsertAllRequest.RowToInsert; import com.google.gcloud.spi.BigQueryRpc; @@ -69,36 +70,17 @@ public RetryResult beforeEval(Exception exception) { static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder() .abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build(); - private abstract static class BasePageFetcher implements PageImpl.NextPageFetcher { + private static class DatasetPageFetcher implements NextPageFetcher { - private static final long serialVersionUID = -338124488600215401L; - - protected final Map requestOptions; - protected final BigQueryOptions serviceOptions; - - BasePageFetcher(BigQueryOptions serviceOptions, String cursor, - Map optionMap) { - this.serviceOptions = serviceOptions; - ImmutableMap.Builder builder = ImmutableMap.builder(); - if (cursor != null) { - builder.put(BigQueryRpc.Option.PAGE_TOKEN, cursor); - } - for (Map.Entry option : optionMap.entrySet()) { - if (option.getKey() != BigQueryRpc.Option.PAGE_TOKEN) { - builder.put(option.getKey(), option.getValue()); - } - } - this.requestOptions = builder.build(); - } - } - - private static class DatasetPageFetcher extends BasePageFetcher { - - private static final long serialVersionUID = 3030824397616608646L; + private static final long serialVersionUID = -3057564042439021278L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; DatasetPageFetcher(BigQueryOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; } @Override @@ -107,14 +89,18 @@ public Page nextPage() { } } - private static class TablePageFetcher extends BasePageFetcher { + private static class TablePageFetcher implements NextPageFetcher { - private static final long serialVersionUID = 5908129355985236115L; + private static final long serialVersionUID = 8611248840504201187L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; private final String dataset; TablePageFetcher(String dataset, BigQueryOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; this.dataset = dataset; } @@ -124,13 +110,17 @@ public Page nextPage() { } } - private static class JobPageFetcher extends BasePageFetcher { + private static class JobPageFetcher implements NextPageFetcher { - private static final long serialVersionUID = -4984845360519279880L; + private static final long serialVersionUID = 8536533282558245472L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; JobPageFetcher(BigQueryOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; } @Override @@ -139,14 +129,18 @@ public Page nextPage() { } } - private static class TableDataPageFetcher extends BasePageFetcher> { + private static class TableDataPageFetcher implements NextPageFetcher> { - private static final long serialVersionUID = 1281938239570262432L; + private static final long serialVersionUID = -8501991114794410114L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; private final TableId table; TableDataPageFetcher(TableId table, BigQueryOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; this.table = table; } @@ -156,15 +150,19 @@ public Page> nextPage() { } } - private static class QueryResultsPageFetcherImpl extends BasePageFetcher> - implements QueryResult.QueryResultsPageFetcher { + private static class QueryResultsPageFetcherImpl + implements NextPageFetcher>, QueryResult.QueryResultsPageFetcher { - private static final long serialVersionUID = 6713948754731557486L; + private static final long serialVersionUID = -9198905840550459803L; + private final Map requestOptions; + private final BigQueryOptions serviceOptions; private final JobId job; QueryResultsPageFetcherImpl(JobId job, BigQueryOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(BigQueryRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; this.job = job; } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java b/gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java index 9ebf7f0f24e8..2dc031ab9bd4 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/PageImpl.java @@ -17,10 +17,12 @@ package com.google.gcloud; import com.google.common.collect.AbstractIterator; +import com.google.common.collect.ImmutableMap; import java.io.Serializable; import java.util.Collections; import java.util.Iterator; +import java.util.Map; import java.util.Objects; /** @@ -85,7 +87,7 @@ public Iterable values() { @Override public Iterator iterateAll() { - return new PageIterator(this); + return new PageIterator<>(this); } @Override @@ -115,4 +117,28 @@ public boolean equals(Object obj) { return Objects.equals(cursor, other.cursor) && Objects.equals(results, other.results); } + + /** + * Utility method to construct the options map for the next page request. + * + * @param the value type that the page holds. Instances of {@code T} should be + * {@code Serializable} + * @param pageTokenOption the key for the next page cursor option in the options map + * @param cursor the cursor for the next page + * @param optionMap the previous options map + * @return the options map for the next page request + */ + public static Map nextRequestOptions( + T pageTokenOption, String cursor, Map optionMap) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + if (cursor != null) { + builder.put(pageTokenOption, cursor); + } + for (Map.Entry option : optionMap.entrySet()) { + if (!Objects.equals(option.getKey(), pageTokenOption)) { + builder.put(option.getKey(), option.getValue()); + } + } + return builder.build(); + } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java index 9ae8c976631b..2a0e09d9fb31 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerImpl.java @@ -29,11 +29,11 @@ import com.google.gcloud.ExceptionHandler.Interceptor; import com.google.gcloud.Page; import com.google.gcloud.PageImpl; +import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.RetryHelper.RetryHelperException; import com.google.gcloud.spi.ResourceManagerRpc; import com.google.gcloud.spi.ResourceManagerRpc.Tuple; -import java.io.Serializable; import java.util.Map; import java.util.concurrent.Callable; @@ -117,37 +117,17 @@ public com.google.api.services.cloudresourcemanager.model.Project call() { } } - private abstract static class BasePageFetcher - implements PageImpl.NextPageFetcher { + private static class ProjectPageFetcher implements NextPageFetcher { - private static final long serialVersionUID = -5560906434575940205L; - - protected final Map requestOptions; - protected final ResourceManagerOptions serviceOptions; - - BasePageFetcher(ResourceManagerOptions serviceOptions, String cursor, - Map optionMap) { - this.serviceOptions = serviceOptions; - ImmutableMap.Builder builder = ImmutableMap.builder(); - if (cursor != null) { - builder.put(ResourceManagerRpc.Option.PAGE_TOKEN, cursor); - } - for (Map.Entry option : optionMap.entrySet()) { - if (option.getKey() != ResourceManagerRpc.Option.PAGE_TOKEN) { - builder.put(option.getKey(), option.getValue()); - } - } - this.requestOptions = builder.build(); - } - } - - private static class ProjectPageFetcher extends BasePageFetcher { - - private static final long serialVersionUID = -533306655445189098L; + private static final long serialVersionUID = 2158209410430566961L; + private final Map requestOptions; + private final ResourceManagerOptions serviceOptions; ProjectPageFetcher(ResourceManagerOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(ResourceManagerRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; } @Override diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index 85e0b02025af..93fc202febef 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -48,6 +48,7 @@ import com.google.gcloud.ExceptionHandler.Interceptor; import com.google.gcloud.Page; import com.google.gcloud.PageImpl; +import com.google.gcloud.PageImpl.NextPageFetcher; import com.google.gcloud.RetryHelper.RetryHelperException; import com.google.gcloud.spi.StorageRpc; import com.google.gcloud.spi.StorageRpc.RewriteResponse; @@ -209,36 +210,18 @@ public BlobInfo get(BlobId blob) { return get(blob, new BlobGetOption[0]); } - private abstract static class BasePageFetcher - implements PageImpl.NextPageFetcher { + private static class BucketPageFetcher implements NextPageFetcher { - private static final long serialVersionUID = 8236329004030295223L; - protected final Map requestOptions; - protected final StorageOptions serviceOptions; + private static final long serialVersionUID = 5850406828803613729L; + private final Map requestOptions; + private final StorageOptions serviceOptions; - BasePageFetcher(StorageOptions serviceOptions, String cursor, + BucketPageFetcher( + StorageOptions serviceOptions, String cursor, Map optionMap) { + this.requestOptions = + PageImpl.nextRequestOptions(StorageRpc.Option.PAGE_TOKEN, cursor, optionMap); this.serviceOptions = serviceOptions; - ImmutableMap.Builder builder = ImmutableMap.builder(); - if (cursor != null) { - builder.put(StorageRpc.Option.PAGE_TOKEN, cursor); - } - for (Map.Entry option : optionMap.entrySet()) { - if (option.getKey() != StorageRpc.Option.PAGE_TOKEN) { - builder.put(option.getKey(), option.getValue()); - } - } - this.requestOptions = builder.build(); - } - } - - private static class BucketPageFetcher extends BasePageFetcher { - - private static final long serialVersionUID = -5490616010200159174L; - - BucketPageFetcher(StorageOptions serviceOptions, String cursor, - Map optionMap) { - super(serviceOptions, cursor, optionMap); } @Override @@ -247,14 +230,18 @@ public Page nextPage() { } } - private static class BlobPageFetcher extends BasePageFetcher { + private static class BlobPageFetcher implements NextPageFetcher { - private static final long serialVersionUID = -5490616010200159174L; + private static final long serialVersionUID = 81807334445874098L; + private final Map requestOptions; + private final StorageOptions serviceOptions; private final String bucket; BlobPageFetcher(String bucket, StorageOptions serviceOptions, String cursor, Map optionMap) { - super(serviceOptions, cursor, optionMap); + this.requestOptions = + PageImpl.nextRequestOptions(StorageRpc.Option.PAGE_TOKEN, cursor, optionMap); + this.serviceOptions = serviceOptions; this.bucket = bucket; }