Last active
December 25, 2015 15:39
-
-
Save gaul/6999997 to your computer and use it in GitHub Desktop.
Demonstrate how to write a single iterator which calls jclouds BlobStore.list until it lists all blobs.
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
public class PageSetIterableTest { | |
private class PageSetIterable implements Iterator<StorageMetadata> { | |
private final String container; | |
private ListContainerOptions options; | |
private PageSet<? extends StorageMetadata> set; | |
private Iterator<? extends StorageMetadata> iterator; | |
private PageSetIterable(String container, ListContainerOptions options) { | |
this.container = checkNotNull(container); | |
advanceList(options); | |
} | |
@Override | |
public boolean hasNext() { | |
if (iterator.hasNext()) { | |
return true; | |
} | |
String marker = set.getNextMarker(); | |
if (marker == null) { | |
return false; | |
} | |
advanceList(options.afterMarker(marker)); | |
return iterator.hasNext(); | |
} | |
@Override | |
public StorageMetadata next() { | |
if (hasNext()) { | |
return iterator.next(); | |
} else { | |
throw new NoSuchElementException(); | |
} | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
private void advanceList(ListContainerOptions options) { | |
this.options = options; | |
this.set = list(container, options); | |
this.iterator = set.iterator(); | |
} | |
} | |
public static Iterator<? extends StorageMetadata> listAll(String container, ListContainerOptions options) { | |
return new PageSetIterable(container, options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment