org.jclouds.collect.PagedIterable Maven / Gradle / Ivy
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you 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 org.jclouds.collect;
import java.util.Iterator;
import com.google.common.annotations.Beta;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
/**
* Extends {@link FluentIterable} allowing you to lazily advance through
* sequence of pages in a resultset. Typically used in apis that return only a
* certain number of records at a time.
*
* Simplest usage is to employ the {@link #concat} convenience function, and one
* of the methods from {@link FluentIterable}.
*
*
* // pull in new pages until it we see something interesting.
* Optional firstInterestingBlob = blobstore
* .list(// options //)
* .concat()
* .firstMatch(isInterestingBlob());
*
*
* For those seeking manual control of page advances, don't use concat, and
* instead look at the value of {@link IterableWithMarker#nextToken}.
*
*
* PagedIterator blobs = blobstore.list(...).iterator();
* while (blobs.hasNext()) {
* IterableWithMarker page = blobs.next();
* ProcessedResults results = process(page);
* if (results.shouldBeBookmarked() && page.nextMarker().isPresent()) {
* saveBookmark(page.nextMarker().get());
* }
* }
*
*
* @author Adrian Cole
*/
@Beta
public abstract class PagedIterable extends FluentIterable> {
/**
* Combines all the pages into a single unmodifiable iterable. ex.
*
*
* FluentIterable blobs = blobstore.list(...).concat();
* for (StorageMetadata blob : blobs) {
* process(blob);
* }
*
*
* @see Iterators#concat
*/
public FluentIterable concat() {
final Iterator> iterator = iterator();
final UnmodifiableIterator> unmodifiable = new UnmodifiableIterator>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Iterator next() {
return iterator.next().iterator();
}
};
return new FluentIterable() {
@Override
public Iterator iterator() {
return Iterators.concat(unmodifiable);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy