Permalink
Browse files

TFJ-549 nextPage return null to make iteration more easy

  • Loading branch information...
1 parent 704546e commit f3ee1ef9e56ad5f10baf5e8ad5aa7776a4ee825e @yusuke committed Jul 16, 2012
@@ -43,14 +43,16 @@
List<Tweet> getTweets();
/**
- * Returns a Query instance to fetch next page
+ * Returns a Query instance to fetch next page or null if there is no next page.
+ *
* @return Query instance to fetch next page
* @since Twitter4J 3.0.0
*/
Query nextQuery();
/**
* test if there is next page
+ *
* @return if there is next page
* @since Twitter4J 3.0.0
*/
@@ -50,23 +50,24 @@
private String query;
private List<Tweet> tweets;
private static final long serialVersionUID = -9059136565234613286L;
- private String nextQuery;
+ private String nextPage;
// private static factory method to instantiate Query class with "next_page"
// http://jira.twitter4j.org/browse/TFJ-549
static Method queryFactoryMethod;
+
static {
- Method[] methods = Query.class.getDeclaredMethods();
- for(Method method : methods){
- if(method.getName().equals("createWithNextPageQuery")){
- queryFactoryMethod = method;
- queryFactoryMethod.setAccessible(true);
- break;
- }
- }
- if(queryFactoryMethod == null){
- throw new ExceptionInInitializerError(new NoSuchMethodException("twitter4j.Query.createWithNextPageQuery(java.lang.String)"));
+ Method[] methods = Query.class.getDeclaredMethods();
+ for (Method method : methods) {
+ if (method.getName().equals("createWithNextPageQuery")) {
+ queryFactoryMethod = method;
+ queryFactoryMethod.setAccessible(true);
+ break;
}
+ }
+ if (queryFactoryMethod == null) {
+ throw new ExceptionInInitializerError(new NoSuchMethodException("twitter4j.Query.createWithNextPageQuery(java.lang.String)"));
+ }
}
/*package*/ QueryResultJSONImpl(HttpResponse res, Configuration conf) throws TwitterException {
@@ -90,7 +91,7 @@
JSONObject tweet = array.getJSONObject(i);
tweets.add(new TweetJSONImpl(tweet, conf));
}
- nextQuery = json.has("next_page") ? json.getString("next_page") : null;
+ nextPage = json.has("next_page") ? json.getString("next_page") : null;
} catch (JSONException jsone) {
throw new TwitterException(jsone.getMessage() + ":" + json.toString(), jsone);
}
@@ -176,20 +177,29 @@ public String getQuery() {
return tweets;
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Query nextQuery() {
+ if (nextPage == null) {
+ return null;
+ }
try {
- return (Query) queryFactoryMethod.invoke(null, new String[]{nextQuery});
+ return (Query) queryFactoryMethod.invoke(null, new String[]{nextPage});
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public boolean hasNext() {
- return nextQuery != null;
+ return nextPage != null;
}
@Override
@@ -133,6 +133,14 @@ public void testEasyPaging() throws Exception {
query = result.nextQuery();
result = twitter1.search(query);
assertTrue(result.getTweets().size() > 0);
+
+ do {
+ result = twitter1.search(query);
+ if (result.getTweets().size() == 0) {
+ break;
+ }
+ // do something
+ } while ((query = result.nextQuery()) != null);
}
public void testTrends() throws Exception {

0 comments on commit f3ee1ef

Please sign in to comment.