All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sola.instagram.util.PaginationIterator Maven / Gradle / Ivy

The newest version!
package com.sola.instagram.util;

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sola.instagram.io.GetMethod;

public class PaginationIterator implements Iterator {
	protected String nextUri; 
	protected ArrayList list;
	protected int index = 0;
	
	public PaginationIterator(ArrayList list, String nextUri) throws Exception {
		this.list = list;
		this.nextUri = nextUri;
		if(list.size() == 0) fetch();
	}


	public boolean hasNext() {
		return index < list.size() || !paginationComplete(); 
	}

	public E next() {
		if(index >= list.size() - 1) {	
			try {
				fetch();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		return list.get(index++);
	}

	public void remove() {
		// not implenting this	
	}

	public void fetch() throws Exception {
		if(paginationComplete()) return;
		JSONObject object = (new GetMethod().setMethodURI(nextUri))
				.call().getJSON();
		JSONObject pagination = object.optJSONObject("pagination");
		nextUri = pagination == null ? null : pagination.optString("next_url");
		JSONArray data = object.optJSONArray("data");
		if(data != null) {
			handleLoad(data);
		}
	}

	public void handleLoad(JSONArray data) throws JSONException {
		
	}

	public boolean paginationComplete() {
		return this.nextUri == null || this.nextUri == "";
	}
	
	public PaginationIterator reset() {
		this.index = 0;
		return this;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy