Skip to content

Commit

Permalink
Avoid storing Iterable<Blob> in BlobListResult
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Sep 23, 2015
1 parent 44cefb0 commit c1f018c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.google.gcloud.storage;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -27,13 +27,35 @@
public class BlobListResult implements ListResult<Blob> {

private final ListResult<BlobInfo> infoList;
private final transient Storage storage;
private transient List<Blob> results;
private final Storage storage;

private class BlobListIterator implements Iterator<Blob> {

private final Iterator<BlobInfo> blobInfoIterator;

public BlobListIterator() {
this.blobInfoIterator = infoList.iterator();
}

@Override
public boolean hasNext() {
return blobInfoIterator.hasNext();
}

@Override
public Blob next() {
return new Blob(storage, blobInfoIterator.next());
}

@Override
public void remove() {
blobInfoIterator.remove();
}
}

public BlobListResult(Storage storage, ListResult<BlobInfo> infoList) {
this.storage = storage;
this.infoList = infoList;
this.results = null;
this.storage = checkNotNull(storage);
this.infoList = checkNotNull(infoList);
}

@Override
Expand All @@ -52,13 +74,7 @@ public ListResult<Blob> nextPage() {

@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();
return new BlobListIterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,19 @@

package com.google.gcloud.storage;

import java.util.Iterator;

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

/**
* Returns the cursor for the nextPage or {@code null} if no more results.
*
* @return the string cursor for next page
* Return the cursor for the nextPage or {@code null} if no more results.
*/
public String nextPageCursor();
String nextPageCursor();

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

@Override
public Iterator<T> iterator();
ListResult<T> nextPage();

}

0 comments on commit c1f018c

Please sign in to comment.