Skip to content

Commit

Permalink
Ability to get N number of pages from Paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbdean committed Oct 31, 2014
1 parent 33beeff commit 1987ae5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
38 changes: 36 additions & 2 deletions src/main/java/net/dean/jraw/paginators/Paginator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import net.dean.jraw.models.Listing;
import net.dean.jraw.models.Thing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -68,8 +70,8 @@ public Paginator(RedditClient creator, Class<T> thingType) {
* @param forwards If true, this method will return the next listing. If false, it will return the first listing.
* @return A new listing
* @throws NetworkException If the request was not successful
* @throws IllegalStateException If a setter method (such as {@link #setLimit(int)} was called after the first listing was
* requested.
* @throws IllegalStateException If a setter method (such as {@link #setLimit(int)} was called after the first
* listing was requested and {@link #reset()} was not called.
*/
protected Listing<T> getListing(boolean forwards) throws NetworkException, IllegalStateException {
if (started && changed) {
Expand Down Expand Up @@ -118,6 +120,38 @@ protected Listing<T> parseListing(RedditResponse response) {
return response.asListing(thingType);
}

/**
* Creates a list of listings whose size is less than or equal to the given number of pages. The amount of time this
* method takes to return will grow linearly based on the maximum number of pages, and is therefore doubly important
* this this method be executed on another thread.
*
* @param maxPages The maximum amount of pages to retrieve
* @return A list of listings
* @throws NetworkException
*/
public final List<Listing<T>> accumulate(int maxPages) throws NetworkException {
List<Listing<T>> listings = new ArrayList<>();
if (maxPages <= 0) {
throw new IllegalArgumentException("Pages must be greater than 0");
}

try {
while (hasNext() && getPageIndex() < maxPages) {
listings.add(next());
}
} catch (IllegalStateException e) {
// Most likely cause will be a NetworkException because next() throws a NetworkException as a cause of an
// IllegalStateException
if (e.getCause().getClass().equals(NetworkException.class)) {
throw (NetworkException) e.getCause();
} else {
throw e;
}
}

return listings;
}

@Override
public boolean hasNext() {
return (current != null && current.getAfter() != null) || !started;
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/net/dean/jraw/paginators/UserRecordPaginator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ protected Listing<UserRecord> parseListing(RedditResponse response) {
}

JsonNode data = response.getJson().get("data");
// A moderator listing only has a 'children' key
String before = where == Where.MODERATORS ? null : data.get("before").asText();
String after = where == Where.MODERATORS ? null : data.get("after").asText();
String modhash = where == Where.MODERATORS ? null : data.get("modhash").asText();
return new FauxListing<>(list.build(), before, after, modhash);
return new FauxListing<>(list.build(), getJsonValue(data, "before"),
getJsonValue(data, "after"), getJsonValue(data, "after"));
}

private String getJsonValue(JsonNode data, String key) {
if (where == Where.MODERATORS) {
// A moderator listing only has a 'children' key
return null;
}

JsonNode node = data.get(key);
if (node.isNull()) {
return null;
}
return node.asText();
}

@Override
Expand Down
14 changes: 3 additions & 11 deletions src/test/java/net/dean/jraw/test/auth/PaginationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -200,10 +199,7 @@ protected <T extends Thing> void commonTest(Paginator<T> p) {
try {
int numPages = 2;
// Test that the paginator can retrieve the data
List<Listing<T>> pages = new ArrayList<>();
while (p.hasNext() && p.getPageIndex() < numPages) {
pages.add(p.next());
}
List<Listing<T>> pages = p.accumulate(numPages);

for (Listing<T> listing : pages) {
// Validate the Listing
Expand All @@ -216,12 +212,8 @@ protected <T extends Thing> void commonTest(Paginator<T> p) {
JrawUtils.logger().warn("Listing was empty");
}
}
} catch (IllegalStateException e) {
if (e.getCause().getClass().equals(NetworkException.class)) {
handle(e.getCause());
} else {
handle(e);
}
} catch (NetworkException e) {
handle(e);
}
}
}

0 comments on commit 1987ae5

Please sign in to comment.