com.testdroid.api.APIListResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testdroid-api Show documentation
Show all versions of testdroid-api Show documentation
The Testdroid API library for Java
package com.testdroid.api;
import com.testdroid.api.dto.Context;
/**
* @author Łukasz Kajda
* @author Slawomir Pawluk
*/
public class APIListResource {
private final APIClient client;
private final Context context;
private final String resourceURI;
public APIListResource(APIClient client, String resourceURI, Class type) {
this.client = client;
this.resourceURI = resourceURI;
this.context = new Context<>(type);
}
public APIListResource(APIClient client, String resourceURI, Context context) {
this.client = client;
this.resourceURI = resourceURI;
this.context = context;
}
public APIList getEntity() throws APIException {
APIList entity = client.get(resourceURI, context);
for (APIEntity item : entity.getData()) {
item.client = this.client;
item.selfURI = APIEntity.createUri(this.resourceURI, String.format("/%s", item.id));
}
return entity;
}
/**
* Returns total number of available items in this resource list.
*
* @throws APIException on any API errors.
*/
public Integer getTotal() throws APIException {
return getEntity().getTotal();
}
private boolean isNextAvailable() {
try {
APIList list = getEntity();
return list.getOffset() + list.getLimit() < list.getTotal();
} catch (APIException ex) {
return false;
}
}
/**
* Returns resource with next page of items in that list.
* Should check with isNextAvailable()
before calling this method.
* If no next page is available, returns null
.
*
* @throws APIException on any API errors.
*/
public APIListResource getNext() throws APIException {
if (!isNextAvailable()) {
return null;
}
APIList list = getEntity();
return new APIListResource<>(client, resourceURI, context.setOffset(list.getOffset() + list.getLimit()));
}
private boolean isPreviousAvailable() {
try {
return getEntity().getOffset() > 0;
} catch (APIException ex) {
return false;
}
}
/**
* Returns resource with previous page of items in that list.
* Should check with isPreviousAvailable()
before calling this method.
* If no previous page is available, returns null
.
*
* @throws APIException on any API errors.
*/
public APIListResource getPrevious() throws APIException {
if (!isPreviousAvailable()) {
return null;
}
APIList list = getEntity();
return new APIListResource<>(client, resourceURI, context.setOffset(list.getOffset() - list.getLimit()));
}
}