com.aeontronix.enhancedmule.tools.util.PaginatedList Maven / Gradle / Ivy
/*
* Copyright (c) Aeontronix 2019
*/
package com.aeontronix.enhancedmule.tools.util;
import com.aeontronix.commons.URLBuilder;
import com.aeontronix.enhancedmule.tools.anypoint.AnypointObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public abstract class PaginatedList implements Iterable {
protected Z parent;
protected int limit = 100;
protected int offset = 0;
protected int total = -1;
protected List list;
public PaginatedList(Z parent) {
this.parent = parent;
}
public PaginatedList(Z parent, int limit) {
this(parent);
this.limit = limit;
}
@NotNull
protected abstract URLBuilder buildUrl();
public void download() throws HttpException {
String url = buildUrl().queryParam("limit", limit).queryParam("offset", offset).toString();
String json = parent.getClient().getHttpHelper().httpGet(url);
JsonHelper jsonHelper = parent.getClient().getJsonHelper();
parseJson(json, jsonHelper);
}
@SuppressWarnings("unchecked")
protected void parseJson(String json, JsonHelper jsonHelper) {
jsonHelper.readJson(this, json);
for (X obj : list) {
if (obj instanceof AnypointObject>) {
((AnypointObject) obj).setParent(parent);
}
}
}
@JsonProperty
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int size() {
return list != null ? list.size() : -1;
}
public List getAll() {
List l = new ArrayList<>();
this.forEach(l::add);
return l;
}
@NotNull
@Override
public Iterator iterator() {
return new PaginatingIterator();
}
public class PaginatingIterator implements Iterator {
private Iterator iterator;
public PaginatingIterator() {
iterator = list.iterator();
}
@Override
public boolean hasNext() {
boolean hasNext = iterator.hasNext();
if (!hasNext) {
offset = offset + limit;
try {
download();
if (list.isEmpty()) {
return false;
}
iterator = list.iterator();
} catch (HttpException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
return true;
}
@Override
public X next() {
return iterator.next();
}
@Override
public void remove() {
iterator.remove();
}
}
}