se.michaelthelin.spotify.model_objects.specification.PagingCursorbased Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spotify-web-api-java Show documentation
Show all versions of spotify-web-api-java Show documentation
A Java client for Spotify's Web API
package se.michaelthelin.spotify.model_objects.specification;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.gson.JsonObject;
import se.michaelthelin.spotify.model_objects.AbstractModelObject;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
/**
* Retrieve information about
*
* cursor-based Paging objects by building instances from this class.
* This cursor-based paging object is a container for a set of objects.
*
* @param The type of the objects contained in a paging object.
*/
@JsonDeserialize(builder = PagingCursorbased.Builder.class)
public class PagingCursorbased extends AbstractModelObject {
private final String href;
private final T[] items;
private final Integer limit;
private final String next;
private final Cursor[] cursors;
private final Integer total;
private PagingCursorbased(final PagingCursorbased.Builder builder) {
super(builder);
this.href = builder.href;
this.items = builder.items;
this.limit = builder.limit;
this.next = builder.next;
this.cursors = builder.cursors;
this.total = builder.total;
}
/**
* Get a link to the Web API endpoint returning the full result of the request.
*
* @return A link to the Web API endpoint returning the full result of the request.
*/
public String getHref() {
return href;
}
/**
* Get the items contained in the paging object.
*
* @return The items contained in the paging object.
*/
public T[] getItems() {
return items;
}
/**
* Get the maximum number of items in the response (as set in the query or by default).
*
* @return The maximum number of items in the response (as set in the query or by default).
*/
public Integer getLimit() {
return limit;
}
/**
* Get the URL to the next page of items. ({@code null} if none)
*
* @return URL to the next page of items. ({@code null} if none)
*/
public String getNext() {
return next;
}
/**
* Get the cursors used to find the next set of items.
*
* @return The cursors used to find the next set of items.
*/
public Cursor[] getCursors() {
return cursors;
}
/**
* Get the total number of items available to return.
*
* @return The total number of items available to return.
*/
public Integer getTotal() {
return total;
}
@Override
public String toString() {
return "PagingCursorbased(href=" + href + ", items=" + Arrays.toString(items) + ", limit=" + limit + ", next="
+ next + ", cursors=" + Arrays.toString(cursors) + ", total=" + total + ")";
}
@Override
public Builder builder() {
return new Builder<>();
}
/**
* Builder class for building {@link PagingCursorbased} instances.
*
* @param The type of the objects contained in a paging object.
*/
public static final class Builder extends AbstractModelObject.Builder {
private String href;
private T[] items;
private Integer limit;
private String next;
private Cursor[] cursors;
private Integer total;
/**
* The href setter.
*
* @param href A link to the Web API endpoint returning the full result of the request.
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setHref(String href) {
this.href = href;
return this;
}
/**
* The items setter.
*
* @param items A page of items.
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setItems(T[] items) {
this.items = items;
return this;
}
/**
* The request limit setter.
*
* @param limit The maximum number of items in the response (as set in the query or by default).
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setLimit(Integer limit) {
this.limit = limit;
return this;
}
/**
* The next URL setter.
*
* @param next URL to the next page of items. ({@code null} if none)
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setNext(String next) {
this.next = next;
return this;
}
/**
* The cursor setter.
*
* @param cursors The cursors used to find the next set of items.
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setCursors(Cursor... cursors) {
this.cursors = cursors;
return this;
}
/**
* The total amount setter.
*
* @param total The total number of items available to return.
* @return A {@link PagingCursorbased.Builder}.
*/
public Builder setTotal(Integer total) {
this.total = total;
return this;
}
@Override
public PagingCursorbased build() {
return new PagingCursorbased<>(this);
}
}
/**
* JsonUtil class for building {@link PagingCursorbased} instances.
*
* @param The type of the objects contained in a paging object.
*/
@SuppressWarnings("unchecked")
public static final class JsonUtil extends AbstractModelObject.JsonUtil> {
public PagingCursorbased createModelObject(JsonObject jsonObject) {
if (jsonObject == null || jsonObject.isJsonNull()) {
return null;
}
return new Builder()
.setHref(
hasAndNotNull(jsonObject, "href")
? jsonObject.get("href").getAsString()
: null)
.setItems(
hasAndNotNull(jsonObject, "items")
? createModelObjectArray(
jsonObject.getAsJsonArray("items"), (Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0])
: null)
.setLimit(
hasAndNotNull(jsonObject, "limit")
? jsonObject.get("limit").getAsInt()
: null)
.setNext(
hasAndNotNull(jsonObject, "next")
? jsonObject.get("next").getAsString()
: null)
.setCursors(
hasAndNotNull(jsonObject, "cursors")
? new Cursor.JsonUtil().createModelObjectArray(
jsonObject.getAsJsonArray("cursors"))
: null)
.setTotal(
hasAndNotNull(jsonObject, "total")
? jsonObject.get("total").getAsInt()
: null)
.build();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy