software.amazon.awssdk.services.health.paginators.DescribeEventsIterable Maven / Gradle / Ivy
Show all versions of health Show documentation
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services.health.paginators;
import java.util.Collections;
import java.util.Iterator;
import java.util.function.Function;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.core.pagination.sync.PaginatedItemsIterable;
import software.amazon.awssdk.core.pagination.sync.PaginatedResponsesIterator;
import software.amazon.awssdk.core.pagination.sync.SdkIterable;
import software.amazon.awssdk.core.pagination.sync.SyncPageFetcher;
import software.amazon.awssdk.core.util.PaginatorUtils;
import software.amazon.awssdk.services.health.HealthClient;
import software.amazon.awssdk.services.health.internal.UserAgentUtils;
import software.amazon.awssdk.services.health.model.DescribeEventsRequest;
import software.amazon.awssdk.services.health.model.DescribeEventsResponse;
import software.amazon.awssdk.services.health.model.Event;
/**
*
* Represents the output for the
* {@link software.amazon.awssdk.services.health.HealthClient#describeEventsPaginator(software.amazon.awssdk.services.health.model.DescribeEventsRequest)}
* operation which is a paginated operation. This class is an iterable of
* {@link software.amazon.awssdk.services.health.model.DescribeEventsResponse} that can be used to iterate through all
* the response pages of the operation.
*
*
* When the operation is called, an instance of this class is returned. At this point, no service calls are made yet and
* so there is no guarantee that the request is valid. As you iterate through the iterable, SDK will start lazily
* loading response pages by making service calls until there are no pages left or your iteration stops. If there are
* errors in your request, you will see the failures only after you start iterating through the iterable.
*
*
*
* The following are few ways to iterate through the response pages:
*
* 1) Using a Stream
*
*
* {@code
* software.amazon.awssdk.services.health.paginators.DescribeEventsIterable responses = client.describeEventsPaginator(request);
* responses.stream().forEach(....);
* }
*
*
* 2) Using For loop
*
*
* {
* @code
* software.amazon.awssdk.services.health.paginators.DescribeEventsIterable responses = client.describeEventsPaginator(request);
* for (software.amazon.awssdk.services.health.model.DescribeEventsResponse response : responses) {
* // do something;
* }
* }
*
*
* 3) Use iterator directly
*
*
* {@code
* software.amazon.awssdk.services.health.paginators.DescribeEventsIterable responses = client.describeEventsPaginator(request);
* responses.iterator().forEachRemaining(....);
* }
*
*
* Please notice that the configuration of maxResults won't limit the number of results you get with the paginator.
* It only limits the number of results in each page.
*
*
* Note: If you prefer to have control on service calls, use the
* {@link #describeEvents(software.amazon.awssdk.services.health.model.DescribeEventsRequest)} operation.
*
*/
@Generated("software.amazon.awssdk:codegen")
public class DescribeEventsIterable implements SdkIterable {
private final HealthClient client;
private final DescribeEventsRequest firstRequest;
private final SyncPageFetcher nextPageFetcher;
public DescribeEventsIterable(HealthClient client, DescribeEventsRequest firstRequest) {
this.client = client;
this.firstRequest = UserAgentUtils.applyPaginatorUserAgent(firstRequest);
this.nextPageFetcher = new DescribeEventsResponseFetcher();
}
@Override
public Iterator iterator() {
return PaginatedResponsesIterator.builder().nextPageFetcher(nextPageFetcher).build();
}
/**
* Returns an iterable to iterate through the paginated {@link DescribeEventsResponse#events()} member. The returned
* iterable is used to iterate through the results across all response pages and not a single page.
*
* This method is useful if you are interested in iterating over the paginated member in the response pages instead
* of the top level pages. Similar to iteration over pages, this method internally makes service calls to get the
* next list of results until the iteration stops or there are no more results.
*/
public final SdkIterable events() {
Function> getIterator = response -> {
if (response != null && response.events() != null) {
return response.events().iterator();
}
return Collections.emptyIterator();
};
return PaginatedItemsIterable. builder().pagesIterable(this)
.itemIteratorFunction(getIterator).build();
}
private class DescribeEventsResponseFetcher implements SyncPageFetcher {
@Override
public boolean hasNextPage(DescribeEventsResponse previousPage) {
return PaginatorUtils.isOutputTokenAvailable(previousPage.nextToken());
}
@Override
public DescribeEventsResponse nextPage(DescribeEventsResponse previousPage) {
if (previousPage == null) {
return client.describeEvents(firstRequest);
}
return client.describeEvents(firstRequest.toBuilder().nextToken(previousPage.nextToken()).build());
}
}
}