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

com.github.sheigutn.pushbullet.http.ListRequestBuilder Maven / Gradle / Ivy

The newest version!
package com.github.sheigutn.pushbullet.http;

import com.github.sheigutn.pushbullet.Pushbullet;
import com.github.sheigutn.pushbullet.http.defaults.ListItemsRequest;
import com.github.sheigutn.pushbullet.items.ListResponse;
import com.github.sheigutn.pushbullet.util.ListUtil;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;

import java.util.List;

@Accessors(chain = true, fluent = true)
@Setter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class ListRequestBuilder {

    /**
     * The default API array length limit for the list requests
     */
    private final static int DEFAULT_LIMIT = 500;

    /**
     * The {@link Pushbullet} instance
     */
    private final Pushbullet pushbullet;

    /**
     * The request type
     */
    private final ListRequestType request;

    /**
     * The cursor of the request
     */
    private String cursor;

    /**
     * Whether only active items should be included in the list
     */
    private boolean onlyShowActiveItems = true;

    /**
     * Whether only items that were modified after the specified timestamp should be included in the list
     */
    private double modifiedAfter;

    /**
     * The maximum length of the requested list
     */
    private int limit = DEFAULT_LIMIT;

    /**
     * Whether the list should automatically make new requests when the cursor is not null and include the items in the list
     */
    private boolean completeList = false;

    /**
     * The returned cursor if there is any (has to be used after {@link #execute()})
     */
    private String lastReturnedCursor;

    /**
     * Returns a new {@link ListRequestBuilder} instance
     * @param pushbullet      The {@link Pushbullet} instance to use
     * @param listRequestType The type of the request to be used
     * @param              The generic type of the list
     * @return
     */
    public static  ListRequestBuilder of(Pushbullet pushbullet, ListRequestType listRequestType) {
        return new ListRequestBuilder<>(pushbullet, listRequestType);
    }

    /**
     * Sets the minimum timestamp for items
     * @param modifiedAfter The minimum timestamp
     * @return This {@link ListRequestBuilder}
     */
    public ListRequestBuilder modifiedAfter(double modifiedAfter) {
        this.modifiedAfter = Math.max(modifiedAfter, 0);
        return this;
    }

    /**
     * Sets the maximum length of the requested list,
     * if completeList is true and cursor is not null,
     * new items will automatically be appended to the list when {@link #execute()} was used
     * @param limit The max limit, maximum = 500
     * @return This {@link ListRequestBuilder}
     */
    public ListRequestBuilder limit(int limit) {
        this.limit = Math.min(Math.max(limit, 0), DEFAULT_LIMIT);
        return this;
    }

    /**
     * Executes the request and returns the list
     * @return The list of items
     */
    @SneakyThrows
    public List execute() {
        ListItemsRequest itemRequest = request.getRequestType().newInstance();
        itemRequest
                .setCursor(cursor)
                .setModifiedAfter(modifiedAfter)
                .setLimit(limit)
                .setOnlyShowActiveItems(onlyShowActiveItems);
        if(completeList) {
            return ListUtil.fullList(pushbullet, itemRequest, request.getFunction());
        }
        else {
            ListResponse response = pushbullet.executeRequest(itemRequest);
            lastReturnedCursor = response.getCursor();
            return request.getFunction().apply(response);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy