com.ning.api.client.access.impl.ItemIteratorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ning-api-java Show documentation
Show all versions of ning-api-java Show documentation
Java client library for accessing Ning external API
package com.ning.api.client.access.impl;
import java.util.*;
import com.ning.api.client.action.PagedList;
import com.ning.api.client.item.ContentItem;
public class ItemIteratorImpl>
implements Iterator
{
public final static int DEFAULT_CHUNK_SIZE = 20;
protected final PagedList pageLister;
/**
* Number of items we will try to fetch with each call
*/
protected final int itemsPerCall;
protected Iterator currentItems;
/**
* Marker that is set when underlying iterator finishes
*/
protected boolean closed;
public ItemIteratorImpl(PagedList pageLister) {
this(pageLister, DEFAULT_CHUNK_SIZE);
}
public ItemIteratorImpl(PagedList pageLister, int itemsPerCall)
{
this.pageLister = pageLister;
this.itemsPerCall = itemsPerCall;
}
@Override
public boolean hasNext()
{
if (closed) return false;
if (currentItems == null || !currentItems.hasNext()) {
if (!fetchMore()) {
return false;
}
}
return true;
}
@Override
public C next()
{
if (currentItems == null || !currentItems.hasNext()) {
if (closed || !fetchMore()) {
throw new NoSuchElementException("No more entries to iterate");
}
}
return currentItems.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Item lists are read-only");
}
protected boolean fetchMore() {
List items = pageLister.next(itemsPerCall);
if (items.isEmpty()) {
closed = true;
return false;
}
currentItems = items.iterator();
return true;
}
}