-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ListResult into interface and implementation. Add BlobListResul…
…t for paginated list of Blobs. Add tests.
- Loading branch information
Showing
7 changed files
with
268 additions
and
59 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
gcloud-java-storage/src/main/java/com/google/gcloud/storage/BaseListResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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.storage; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Base implementation for Google Cloud storage list result. | ||
*/ | ||
public class BaseListResult<T extends Serializable> implements ListResult<T>, Serializable { | ||
|
||
private static final long serialVersionUID = -6937287874908527950L; | ||
|
||
private final String cursor; | ||
private final Iterable<T> results; | ||
private final NextPageFetcher<T> pageFetcher; | ||
|
||
public interface NextPageFetcher<T extends Serializable> extends Serializable { | ||
ListResult<T> nextPage(); | ||
} | ||
|
||
public BaseListResult(NextPageFetcher<T> pageFetcher, String cursor, Iterable<T> results) { | ||
this.pageFetcher = pageFetcher; | ||
this.cursor = cursor; | ||
this.results = results; | ||
} | ||
|
||
@Override | ||
public String nextPageCursor() { | ||
return cursor; | ||
} | ||
|
||
@Override | ||
public ListResult<T> nextPage() { | ||
if (cursor == null || pageFetcher == null) { | ||
return null; | ||
} | ||
return pageFetcher.nextPage(); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return results == null ? Collections.<T>emptyIterator() : results.iterator(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(cursor, results); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof BaseListResult)) { | ||
return false; | ||
} | ||
BaseListResult<?> other = (BaseListResult<?>) obj; | ||
return Objects.equals(cursor, other.cursor) | ||
&& Objects.equals(results, other.results); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobListResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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.storage; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Implementation of a paginated list of Google Cloud storage {@code Blob}. | ||
*/ | ||
public class BlobListResult implements ListResult<Blob> { | ||
|
||
private final ListResult<BlobInfo> infoList; | ||
private final transient Storage storage; | ||
private transient List<Blob> results; | ||
|
||
public BlobListResult(Storage storage, ListResult<BlobInfo> infoList) { | ||
this.storage = storage; | ||
this.infoList = infoList; | ||
this.results = null; | ||
} | ||
|
||
@Override | ||
public String nextPageCursor() { | ||
return infoList.nextPageCursor(); | ||
} | ||
|
||
@Override | ||
public ListResult<Blob> nextPage() { | ||
ListResult<BlobInfo> nextPageInfoList = infoList.nextPage(); | ||
if (nextPageInfoList == null) { | ||
return null; | ||
} | ||
return new BlobListResult(storage, nextPageInfoList); | ||
} | ||
|
||
@Override | ||
public Iterator<Blob> iterator() { | ||
if (results == null) { | ||
this.results = new LinkedList<>(); | ||
for (Iterator<BlobInfo> it = infoList.iterator(); it.hasNext();) { | ||
results.add(new Blob(storage, it.next())); | ||
} | ||
} | ||
return results.iterator(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(infoList); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof BlobListResult)) { | ||
return false; | ||
} | ||
BlobListResult other = (BlobListResult) obj; | ||
return Objects.equals(infoList, other.infoList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobListResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.storage; | ||
|
||
import static org.easymock.EasyMock.createStrictMock; | ||
import static org.easymock.EasyMock.expect; | ||
import static org.easymock.EasyMock.replay; | ||
import static org.easymock.EasyMock.verify; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.Iterator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BlobListResultTest { | ||
|
||
private static final Iterable<BlobInfo> FIRST_PAGE_RESULTS = ImmutableList.of( | ||
BlobInfo.of("b1", "n1"), | ||
BlobInfo.of("b2", "n2")); | ||
|
||
private static final Iterable<BlobInfo> SECOND_PAGE_RESULTS = ImmutableList.of( | ||
BlobInfo.of("b1", "n1"), | ||
BlobInfo.of("b2", "n2")); | ||
|
||
private BaseListResult<BlobInfo> firstPage; | ||
private BaseListResult<BlobInfo> secondPage; | ||
private Storage storage; | ||
private BlobListResult blobListResult; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
firstPage = createStrictMock(BaseListResult.class); | ||
secondPage = createStrictMock(BaseListResult.class); | ||
storage = createStrictMock(Storage.class); | ||
blobListResult = new BlobListResult(storage, firstPage); | ||
} | ||
|
||
@Test | ||
public void testListResult() 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()); | ||
} | ||
assertFalse(blobListIterator.hasNext()); | ||
assertFalse(firstPageIterator.hasNext()); | ||
verify(firstPage); | ||
} | ||
|
||
@Test | ||
public void testCursor() throws Exception { | ||
expect(firstPage.nextPageCursor()).andReturn("c"); | ||
replay(firstPage); | ||
assertEquals("c", blobListResult.nextPageCursor()); | ||
verify(firstPage); | ||
} | ||
|
||
@Test | ||
public void testNextPage() throws Exception { | ||
expect(firstPage.nextPage()).andReturn(secondPage); | ||
expect(secondPage.iterator()).andReturn(SECOND_PAGE_RESULTS.iterator()); | ||
replay(firstPage); | ||
replay(secondPage); | ||
ListResult<Blob> nextPageResult = blobListResult.nextPage(); | ||
Iterator<BlobInfo> secondPageIterator = SECOND_PAGE_RESULTS.iterator(); | ||
Iterator<Blob> blobListIterator = nextPageResult.iterator(); | ||
while (blobListIterator.hasNext() && secondPageIterator.hasNext()) { | ||
assertEquals(secondPageIterator.next(), blobListIterator.next().info()); | ||
} | ||
assertFalse(blobListIterator.hasNext()); | ||
assertFalse(secondPageIterator.hasNext()); | ||
verify(firstPage); | ||
verify(secondPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters