added concat to PagedIterable

This commit is contained in:
Adrian Cole 2012-07-16 10:57:41 -04:00
parent 315d5fb3ef
commit addeb0d031
10 changed files with 70 additions and 25 deletions

View File

@ -38,8 +38,6 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
* @author Jeremy Whitlock, Adrian Cole
*/
@ -118,7 +116,7 @@ public class MetricApiExpectTest extends BaseCloudWatchApiExpectTest {
CloudWatchApi apiWhenMetricsExist = requestsSendResponses(
listMetrics, listMetricsResponse, listMetrics2, listMetrics2Response);
assertEquals(Iterables.concat(apiWhenMetricsExist.getMetricApiForRegion(null).list()).toString(),
assertEquals(apiWhenMetricsExist.getMetricApiForRegion(null).list().concat().toString(),
"[Metric{namespace=AWS/EC2, metricName=CPUUtilization, dimension=[Dimension{name=InstanceId, value=i-689fcf0f}]}, Metric{namespace=AWS/EC2, metricName=CPUUtilization, dimension=[Dimension{name=InstanceId, value=i-689fcf0f}]}]");
}

View File

@ -18,10 +18,28 @@
*/
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;
/**
* Allows you to 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.
*
* <pre>
* FluentIterable<StorageMetadata> blobs = blobstore.list(...).concat();
* for (StorageMetadata blob : blobs) {
* process(blob);
* }
* </pre>
*
* Some may be interested in each page, for example to
*
* <pre>
* PagedIterator<StorageMetadata> blobs = blobstore.list(...).iterator();
* while (blobs.hasNext()) {
@ -36,9 +54,42 @@ import com.google.common.collect.FluentIterable;
* @author Adrian Cole
*/
@Beta
public abstract class PagedIterable<T> extends FluentIterable<IterableWithMarker<T>> {
public abstract class PagedIterable<E> extends FluentIterable<IterableWithMarker<E>> {
@Override
public abstract PagedIterator<T> iterator();
public abstract PagedIterator<E> iterator();
/**
* Combines all the pages into a single unmodifiable iterable. ex.
*
* <pre>
* FluentIterable<StorageMetadata> blobs = blobstore.list(...).concat();
* for (StorageMetadata blob : blobs) {
* process(blob);
* }
* </pre>
*
* @see Iterators#concat
*/
public FluentIterable<E> concat() {
final PagedIterator<E> iterator = iterator();
final UnmodifiableIterator<Iterator<E>> unmodifiable = new UnmodifiableIterator<Iterator<E>>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Iterator<E> next() {
return iterator.next().iterator();
}
};
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return Iterators.concat(unmodifiable);
}
};
}
}

View File

@ -9,7 +9,6 @@ import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
/**
* Tests behavior of {@code IterableWithMarkers}.
@ -51,7 +50,7 @@ public class PagedIterablesTest {
PagedIterable<String> iterable = PagedIterables.create(PagedIterators.advancing(initial, markerToNext));
Assert.assertEquals(ImmutableSet.copyOf(Iterables.concat(iterable)),
Assert.assertEquals(iterable.concat().toImmutableSet(),
ImmutableSet.of("foo", "bar", "boo", "baz", "ham", "cheeze"));
EasyMock.verify(markerToNext);

View File

@ -46,7 +46,6 @@ import org.jclouds.logging.Logger;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.ListenableFuture;
/**
@ -88,14 +87,13 @@ public class ELBListLoadBalancersStrategy implements ListLoadBalancersStrategy {
@Override
public Iterable<LoadBalancerInRegion> apply(PagedIterable<LoadBalancer> input) {
return Iterables.transform(Iterables.concat(input),
new Function<LoadBalancer, LoadBalancerInRegion>() {
@Override
public LoadBalancerInRegion apply(LoadBalancer lb) {
return new LoadBalancerInRegion(lb, from);
}
});
return input.concat()
.transform(new Function<LoadBalancer, LoadBalancerInRegion>() {
@Override
public LoadBalancerInRegion apply(LoadBalancer lb) {
return new LoadBalancerInRegion(lb, from);
}
});
}
}, executor);

View File

@ -145,7 +145,7 @@ public class LoadBalancerApiExpectTest extends BaseELBApiExpectTest {
LoadBalancer lb1 = new GetLoadBalancerResponseTest().expected().toBuilder().name("my-load-balancer-1").build();
LoadBalancer lb2 = new GetLoadBalancerResponseTest().expected();
assertEquals(ImmutableSet.copyOf(Iterables.concat(apiWhenExist.getLoadBalancerApi().list())), ImmutableSet.of(lb1, lb2));
assertEquals(apiWhenExist.getLoadBalancerApi().list().concat().toImmutableSet(), ImmutableSet.of(lb1, lb2));
}
public void testList2PagesWhenResponseIs2xxInEU() throws Exception {

View File

@ -20,7 +20,6 @@ package org.jclouds.elb.loadbalancer;
import static org.testng.Assert.assertEquals;
import org.jclouds.collect.PagedIterable;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.elb.ELBApi;
import org.jclouds.elb.ELBAsyncApi;
@ -30,9 +29,9 @@ import org.jclouds.rest.RestContext;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Iterables;
/**
*
@ -61,8 +60,8 @@ public class ELBLoadBalancerServiceLiveTest extends BaseLoadBalancerServiceLiveT
instanceIds.add(node.getProviderId());
}
PagedIterable<LoadBalancer> elbs = elbApi.getLoadBalancerApi().list();
for (LoadBalancer elb : Iterables.concat(elbs)) {
FluentIterable<LoadBalancer> elbs = elbApi.getLoadBalancerApi().list().concat();
for (LoadBalancer elb : elbs) {
if (elb.getName().equals(group))
assertEquals(elb.getInstanceIds(), instanceIds.build());
}

View File

@ -149,7 +149,7 @@ public class UserApiExpectTest extends BaseIAMApiExpectTest {
IAMApi apiWhenExist = requestsSendResponses(list, listResponse, list2, list2Response);
assertEquals(ImmutableList.copyOf(Iterables.concat(apiWhenExist.getUserApi().list())),
assertEquals(apiWhenExist.getUserApi().list().concat().toImmutableList(),
ImmutableList.copyOf(Iterables.concat(new ListUsersResponseTest().expected(), new ListUsersResponseTest().expected())));
}

View File

@ -140,7 +140,7 @@ public class InstanceApiExpectTest extends BaseRDSApiExpectTest {
RDSApi apiWhenExist = requestsSendResponses(
list, listResponse, list2, list2Response);
assertEquals(ImmutableList.copyOf(Iterables.concat(apiWhenExist.getInstanceApi().list())),
assertEquals(apiWhenExist.getInstanceApi().list().concat().toImmutableList(),
ImmutableList.copyOf(Iterables.concat(new DescribeDBInstancesResponseTest().expected(),
new DescribeDBInstancesResponseTest().expected())));
}

View File

@ -140,7 +140,7 @@ public class SecurityGroupApiExpectTest extends BaseRDSApiExpectTest {
RDSApi apiWhenExist = requestsSendResponses(
list, listResponse, list2, list2Response);
assertEquals(ImmutableList.copyOf(Iterables.concat(apiWhenExist.getSecurityGroupApi().list())),
assertEquals(apiWhenExist.getSecurityGroupApi().list().concat().toImmutableList(),
ImmutableList.copyOf(Iterables.concat(new DescribeDBSecurityGroupsResponseTest().expected(),
new DescribeDBSecurityGroupsResponseTest().expected())));
}

View File

@ -141,7 +141,7 @@ public class SubnetGroupApiExpectTest extends BaseRDSApiExpectTest {
RDSApi apiWhenExist = requestsSendResponses(
list, listResponse, list2, list2Response);
assertEquals(ImmutableList.copyOf(Iterables.concat(apiWhenExist.getSubnetGroupApi().list())),
assertEquals(apiWhenExist.getSubnetGroupApi().list().concat().toImmutableList(),
ImmutableList.copyOf(Iterables.concat(new DescribeDBSubnetGroupsResponseTest().expected(),
new DescribeDBSubnetGroupsResponseTest().expected())));
}