org.jclouds.collect.PagedIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jclouds-core Show documentation
Show all versions of jclouds-core Show documentation
Core components to access jclouds services
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 result set. 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 iterate.
*
*
* FluentIterable extends Image> images = imageApi.listInDetail().concat();
*
* for (Image image: images) {
* System.out.println(image);
* }
*
*
* Another usage is to employ the {@link #concat} convenience function, and one
* of the methods from {@link FluentIterable}.
*
*
* Optional extends Image> image = imageApi.listInDetail().concat().firstMatch(isInterestingImage());
* System.out.println(image.orNull());
* ...
* private static Predicate isInterestingImage() {
* return new Predicate() {
* {@literal @}Override
* public boolean apply(Image image) {
* return image.getName().startsWith("Arch");
* }
* };
* }
*
*/
@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