diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobListResult.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobListResult.java index b4f94bc4b0e7..9bc53fd94a9b 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobListResult.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobListResult.java @@ -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; /** @@ -27,13 +27,35 @@ public class BlobListResult implements ListResult { private final ListResult infoList; - private final transient Storage storage; - private transient List results; + private final Storage storage; + + private class BlobListIterator implements Iterator { + + private final Iterator 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 infoList) { - this.storage = storage; - this.infoList = infoList; - this.results = null; + this.storage = checkNotNull(storage); + this.infoList = checkNotNull(infoList); } @Override @@ -52,13 +74,7 @@ public ListResult nextPage() { @Override public Iterator iterator() { - if (results == null) { - this.results = new LinkedList<>(); - for (Iterator it = infoList.iterator(); it.hasNext();) { - results.add(new Blob(storage, it.next())); - } - } - return results.iterator(); + return new BlobListIterator(); } @Override diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/ListResult.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/ListResult.java index 008ad5164a0a..a07d6edff745 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/ListResult.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/ListResult.java @@ -16,28 +16,19 @@ package com.google.gcloud.storage; -import java.util.Iterator; - /** * Interface for Google Cloud storage list result. */ public interface ListResult extends Iterable { /** - * 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 nextPage(); - - @Override - public Iterator iterator(); + ListResult nextPage(); }