Skip to content

Commit

Permalink
Rename ListResult and move to core module
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Nov 3, 2015
1 parent 33bfb1e commit ea36a9e
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

package com.google.gcloud.storage;
package com.google.gcloud;

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

/**
* Base implementation for Google Cloud storage list result.
* Base implementation for Google Cloud paginated results.
*/
public class BaseListResult<T extends Serializable> implements ListResult<T>, Serializable {
public class BasePage<T extends Serializable> implements Page<T>, Serializable {

private static final long serialVersionUID = -6937287874908527950L;

Expand All @@ -33,10 +33,10 @@ public class BaseListResult<T extends Serializable> implements ListResult<T>, Se
private final NextPageFetcher<T> pageFetcher;

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

public BaseListResult(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) {
public BasePage(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) {
this.pageFetcher = pageFetcher;
this.cursor = cursor;
this.results = results;
Expand All @@ -48,7 +48,7 @@ public String nextPageCursor() {
}

@Override
public ListResult<T> nextPage() {
public Page<T> nextPage() {
if (cursor == null || pageFetcher == null) {
return null;
}
Expand All @@ -67,10 +67,10 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BaseListResult)) {
if (!(obj instanceof BasePage)) {
return false;
}
BaseListResult<?> other = (BaseListResult<?>) obj;
BasePage<?> other = (BasePage<?>) obj;
return Objects.equals(cursor, other.cursor)
&& Objects.equals(results, other.results);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
* limitations under the License.
*/

package com.google.gcloud.storage;
package com.google.gcloud;

/**
* Interface for Google Cloud storage list result.
* Interface for Google Cloud paginated results.
*/
public interface ListResult<T> extends Iterable<T> {
public interface Page<T> extends Iterable<T> {

/**
* Returns the cursor for the nextPage or {@code null} if no more results.
*/
String nextPageCursor();

/**
* Returns the results of the nextPage or {@code null} if no more result.
* Returns the next page of results or {@code null} if no more result.
*/
ListResult<T> nextPage();
Page<T> nextPage();

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,32 @@
* limitations under the License.
*/

package com.google.gcloud.storage;
package com.google.gcloud;

import static org.junit.Assert.assertEquals;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.BasePage;

import org.junit.Test;

import java.util.Collections;

public class BaseListResultTest {
public class BasePageTest {

@Test
public void testListResult() throws Exception {
public void testPage() throws Exception {
ImmutableList<String> values = ImmutableList.of("1", "2");
final BaseListResult<String> nextResult =
new BaseListResult<>(null, "c", Collections.<String>emptyList());
BaseListResult.NextPageFetcher<String> fetcher = new BaseListResult.NextPageFetcher<String>() {
final BasePage<String> nextResult =
new BasePage<>(null, "c", Collections.<String>emptyList());
BasePage.NextPageFetcher<String> fetcher = new BasePage.NextPageFetcher<String>() {

@Override
public BaseListResult<String> nextPage() {
public BasePage<String> nextPage() {
return nextResult;
}
};
BaseListResult<String> result = new BaseListResult<>(fetcher, "c", values);
BasePage<String> result = new BasePage<>(fetcher, "c", values);
assertEquals(nextResult, result.nextPage());
assertEquals("c", result.nextPageCursor());
assertEquals(values, ImmutableList.copyOf(result.iterator()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@

import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.google.gcloud.Page;

import java.util.Iterator;
import java.util.Objects;

/**
* Implementation of a paginated list of Google Cloud storage {@code Blob}.
*/
public class BlobListResult implements ListResult<Blob> {
public class BlobPage implements Page<Blob> {

private final ListResult<BlobInfo> infoList;
private final Page<BlobInfo> infoList;
private final Storage storage;

public BlobListResult(Storage storage, ListResult<BlobInfo> infoList) {
public BlobPage(Storage storage, Page<BlobInfo> infoList) {
this.storage = checkNotNull(storage);
this.infoList = checkNotNull(infoList);
}
Expand All @@ -43,12 +44,12 @@ public String nextPageCursor() {
}

@Override
public ListResult<Blob> nextPage() {
ListResult<BlobInfo> nextPageInfoList = infoList.nextPage();
public Page<Blob> nextPage() {
Page<BlobInfo> nextPageInfoList = infoList.nextPage();
if (nextPageInfoList == null) {
return null;
}
return new BlobListResult(storage, nextPageInfoList);
return new BlobPage(storage, nextPageInfoList);
}

@Override
Expand All @@ -68,10 +69,10 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BlobListResult)) {
if (!(obj instanceof BlobPage)) {
return false;
}
BlobListResult other = (BlobListResult) obj;
BlobPage other = (BlobPage) obj;
return Objects.equals(infoList, other.infoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.gcloud.Page;
import com.google.gcloud.storage.Storage.BlobSourceOption;
import com.google.gcloud.storage.Storage.BlobTargetOption;
import com.google.gcloud.storage.Storage.BlobWriteOption;
Expand Down Expand Up @@ -134,8 +135,8 @@ public boolean delete(BucketSourceOption... options) {
* @param options options for listing blobs
* @throws StorageException upon failure
*/
public ListResult<Blob> list(Storage.BlobListOption... options) {
return new BlobListResult(storage, storage.list(info.name(), options));
public Page<Blob> list(Storage.BlobListOption... options) {
return new BlobPage(storage, storage.list(info.name(), options));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.Lists;
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.gcloud.Service;
import com.google.gcloud.Page;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.Tuple;

Expand Down Expand Up @@ -827,14 +828,14 @@ private static void checkContentType(BlobInfo blobInfo) throws IllegalArgumentEx
*
* @throws StorageException upon failure
*/
ListResult<BucketInfo> list(BucketListOption... options);
Page<BucketInfo> list(BucketListOption... options);

/**
* List the bucket's blobs.
*
* @throws StorageException upon failure
*/
ListResult<BlobInfo> list(String bucket, BlobListOption... options);
Page<BlobInfo> list(String bucket, BlobListOption... options);

/**
* Update bucket information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Ints;
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.gcloud.BasePage;
import com.google.gcloud.BaseService;
import com.google.gcloud.ExceptionHandler;
import com.google.gcloud.ExceptionHandler.Interceptor;
import com.google.gcloud.RetryHelper.RetryHelperException;
import com.google.gcloud.Page;
import com.google.gcloud.spi.StorageRpc;
import com.google.gcloud.spi.StorageRpc.RewriteResponse;
import com.google.gcloud.spi.StorageRpc.Tuple;
Expand Down Expand Up @@ -224,7 +226,7 @@ public BlobInfo get(BlobId blob) {
}

private abstract static class BasePageFetcher<T extends Serializable>
implements BaseListResult.NextPageFetcher<T> {
implements BasePage.NextPageFetcher<T> {

private static final long serialVersionUID = 8236329004030295223L;
protected final Map<StorageRpc.Option, ?> requestOptions;
Expand Down Expand Up @@ -256,7 +258,7 @@ private static class BucketPageFetcher extends BasePageFetcher<BucketInfo> {
}

@Override
public ListResult<BucketInfo> nextPage() {
public Page<BucketInfo> nextPage() {
return listBuckets(serviceOptions, requestOptions);
}
}
Expand All @@ -273,17 +275,17 @@ private static class BlobPageFetcher extends BasePageFetcher<BlobInfo> {
}

@Override
public ListResult<BlobInfo> nextPage() {
public Page<BlobInfo> nextPage() {
return listBlobs(bucket, serviceOptions, requestOptions);
}
}

@Override
public ListResult<BucketInfo> list(BucketListOption... options) {
public Page<BucketInfo> list(BucketListOption... options) {
return listBuckets(options(), optionMap(options));
}

private static ListResult<BucketInfo> listBuckets(final StorageOptions serviceOptions,
private static Page<BucketInfo> listBuckets(final StorageOptions serviceOptions,
final Map<StorageRpc.Option, ?> optionsMap) {
try {
Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> result = runWithRetries(
Expand All @@ -302,19 +304,19 @@ public BucketInfo apply(com.google.api.services.storage.model.Bucket bucketPb) {
return BucketInfo.fromPb(bucketPb);
}
});
return new BaseListResult<>(new BucketPageFetcher(serviceOptions, cursor, optionsMap), cursor,
return new BasePage<>(new BucketPageFetcher(serviceOptions, cursor, optionsMap), cursor,
buckets);
} catch (RetryHelperException e) {
throw StorageException.translateAndThrow(e);
}
}

@Override
public ListResult<BlobInfo> list(final String bucket, BlobListOption... options) {
public Page<BlobInfo> list(final String bucket, BlobListOption... options) {
return listBlobs(bucket, options(), optionMap(options));
}

private static ListResult<BlobInfo> listBlobs(final String bucket,
private static Page<BlobInfo> listBlobs(final String bucket,
final StorageOptions serviceOptions, final Map<StorageRpc.Option, ?> optionsMap) {
try {
Tuple<String, Iterable<StorageObject>> result = runWithRetries(
Expand All @@ -333,7 +335,7 @@ public BlobInfo apply(StorageObject storageObject) {
return BlobInfo.fromPb(storageObject);
}
});
return new BaseListResult<>(new BlobPageFetcher(bucket, serviceOptions, cursor, optionsMap),
return new BasePage<>(new BlobPageFetcher(bucket, serviceOptions, cursor, optionsMap),
cursor,
blobs);
} catch (RetryHelperException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import static org.junit.Assert.assertFalse;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.BasePage;
import com.google.gcloud.Page;

import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;

public class BlobListResultTest {
public class BlobPageTest {

private static final Iterable<BlobInfo> FIRST_PAGE_RESULTS = ImmutableList.of(
BlobInfo.builder("b1", "n1").build(),
Expand All @@ -40,29 +42,29 @@ public class BlobListResultTest {
BlobInfo.builder("b1", "n1").build(),
BlobInfo.builder("b2", "n2").build());

private BaseListResult<BlobInfo> firstPage;
private BaseListResult<BlobInfo> secondPage;
private BasePage<BlobInfo> firstPage;
private BasePage<BlobInfo> secondPage;
private Storage storage;
private BlobListResult blobListResult;
private BlobPage blobPage;

@Before
public void setUp() throws Exception {
firstPage = createStrictMock(BaseListResult.class);
secondPage = createStrictMock(BaseListResult.class);
firstPage = createStrictMock(BasePage.class);
secondPage = createStrictMock(BasePage.class);
storage = createStrictMock(Storage.class);
blobListResult = new BlobListResult(storage, firstPage);
blobPage = new BlobPage(storage, firstPage);
}

@Test
public void testListResult() throws Exception {
public void testPage() throws Exception {
expect(firstPage.iterator()).andReturn(FIRST_PAGE_RESULTS.iterator());
replay(firstPage);
Iterator<BlobInfo> firstPageIterator = FIRST_PAGE_RESULTS.iterator();
Iterator<Blob> blobListIterator = blobListResult.iterator();
while (blobListIterator.hasNext() && firstPageIterator.hasNext()) {
assertEquals(firstPageIterator.next(), blobListIterator.next().info());
Iterator<Blob> pageIterator = blobPage.iterator();
while (pageIterator.hasNext() && firstPageIterator.hasNext()) {
assertEquals(firstPageIterator.next(), pageIterator.next().info());
}
assertFalse(blobListIterator.hasNext());
assertFalse(pageIterator.hasNext());
assertFalse(firstPageIterator.hasNext());
verify(firstPage);
}
Expand All @@ -71,7 +73,7 @@ public void testListResult() throws Exception {
public void testCursor() throws Exception {
expect(firstPage.nextPageCursor()).andReturn("c");
replay(firstPage);
assertEquals("c", blobListResult.nextPageCursor());
assertEquals("c", blobPage.nextPageCursor());
verify(firstPage);
}

Expand All @@ -81,7 +83,7 @@ public void testNextPage() throws Exception {
expect(secondPage.iterator()).andReturn(SECOND_PAGE_RESULTS.iterator());
replay(firstPage);
replay(secondPage);
ListResult<Blob> nextPageResult = blobListResult.nextPage();
Page<Blob> nextPageResult = blobPage.nextPage();
Iterator<BlobInfo> secondPageIterator = SECOND_PAGE_RESULTS.iterator();
Iterator<Blob> blobListIterator = nextPageResult.iterator();
while (blobListIterator.hasNext() && secondPageIterator.hasNext()) {
Expand Down
Loading

0 comments on commit ea36a9e

Please sign in to comment.