org.kohsuke.github.GitHubPageContentsIterable Maven / Gradle / Ivy
The newest version!
package org.kohsuke.github;
import java.io.IOException;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
// TODO: Auto-generated Javadoc
/**
* {@link PagedIterable} implementation that take a {@link Consumer} that initializes all the items on each page as they
* are retrieved.
*
* {@link GitHubPageContentsIterable} is immutable and thread-safe, but the iterator returned from {@link #iterator()}
* is not. Any one instance of iterator should only be called from a single thread.
*
* @author Liam Newman
* @param
* the type of items on each page
*/
class GitHubPageContentsIterable extends PagedIterable {
private final GitHubClient client;
private final GitHubRequest request;
private final Class receiverType;
private final Consumer itemInitializer;
/**
* Instantiates a new git hub page contents iterable.
*
* @param client
* the client
* @param request
* the request
* @param receiverType
* the receiver type
* @param itemInitializer
* the item initializer
*/
GitHubPageContentsIterable(GitHubClient client,
GitHubRequest request,
Class receiverType,
Consumer itemInitializer) {
this.client = client;
this.request = request;
this.receiverType = receiverType;
this.itemInitializer = itemInitializer;
}
/**
* {@inheritDoc}
*/
@Override
@Nonnull
public PagedIterator _iterator(int pageSize) {
final GitHubPageIterator iterator = GitHubPageIterator.create(client, receiverType, request, pageSize);
return new GitHubPageContentsIterator(iterator, itemInitializer);
}
/**
* Eagerly walk {@link Iterable} and return the result in a {@link GitHubResponse} containing an array of {@link T}
* items.
*
* @return the last response with an array containing all the results from all pages.
* @throws IOException
* if an I/O exception occurs.
*/
@Nonnull
GitHubResponse toResponse() throws IOException {
GitHubPageContentsIterator iterator = (GitHubPageContentsIterator) iterator();
T[] items = toArray(iterator);
GitHubResponse lastResponse = iterator.lastResponse();
return new GitHubResponse<>(lastResponse, items);
}
/**
* This class is not thread-safe. Any one instance should only be called from a single thread.
*/
private class GitHubPageContentsIterator extends PagedIterator {
public GitHubPageContentsIterator(GitHubPageIterator iterator, Consumer itemInitializer) {
super(iterator, itemInitializer);
}
/**
* Gets the {@link GitHubResponse} for the last page received.
*
* @return the {@link GitHubResponse} for the last page received.
*/
private GitHubResponse lastResponse() {
return ((GitHubPageIterator) base).finalResponse();
}
}
}