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

com.talkylabs.reach.base.Reader Maven / Gradle / Ivy

The newest version!
package com.talkylabs.reach.base;

import com.talkylabs.reach.Reach;
import com.talkylabs.reach.http.ReachRestClient;

import java.util.concurrent.CompletableFuture;

/**
 * Executor for listing of a resource.
 *
 * @param  type of the resource
 */
public abstract class Reader {

    private Integer pageSize;
    private Integer limit;

    /**
     * Execute a request using default client.
     *
     * @return ResourceSet of objects
     */
    public ResourceSet read() {
        return read(Reach.getRestClient());
    }

    /**
     * Execute a request using specified client.
     *
     * @param client client used to make request
     * @return ResourceSet of objects
     */
    public abstract ResourceSet read(final ReachRestClient client);

    /**
     * Execute an async request using default client.
     *
     * @return future that resolves to the ResourceSet of objects
     */
    public CompletableFuture> readAsync() {
        return readAsync(Reach.getRestClient());
    }

    /**
     * Execute an async request using specified client.
     *
     * @param client client used to make request
     * @return future that resolves to the ResourceSet of objects
     */
    public CompletableFuture> readAsync(final ReachRestClient client) {
        return CompletableFuture.supplyAsync(() -> read(client), Reach.getExecutorService());
    }

    /**
     * Fetch the first page of resources.
     *
     * @return Page containing the first pageSize of resources
     */
    public Page firstPage() {
        return firstPage(Reach.getRestClient());
    }

    /**
     * Fetch the first page of resources using specified client.
     *
     * @param client client used to fetch
     * @return Page containing the first pageSize of resources
     */
    public abstract Page firstPage(final ReachRestClient client);

    /**
     * Retrieve the target page of resources.
     *
     * @param targetUrl API-generated URL for the requested results page
     * @return Page containing the target pageSize of resources
     */
    public Page getPage(final String targetUrl) {
        return getPage(targetUrl, Reach.getRestClient());
    }

    /**
     * Retrieve the target page of resources.
     *
     * @param targetUrl API-generated URL for the requested results page
     * @param client    client used to fetch
     * @return Page containing the target pageSize of resources
     */
    public abstract Page getPage(final String targetUrl, final ReachRestClient client);

    /**
     * Fetch the following page of resources.
     *
     * @param page current page of resources
     * @return Page containing the next pageSize of resources
     */
    public Page nextPage(final Page page) {
        return nextPage(page, Reach.getRestClient());
    }
    
    /**
     * Check if there is a next page of resources.
     *
     * @param page current page of resources
     * @return boolean
     */
    public boolean hasNextPage(final Page page) {
        return page.hasNextPage();
    }
    
    /**
     * Check if there is a previous page of resources.
     *
     * @param page current page of resources
     * @return boolean
     */
    public boolean hasPreviousPage(final Page page) {
        return page.hasPreviousPage();
    }

    /**
     * Fetch the following page of resources using specified client.
     *
     * @param page   current page of resources
     * @param client client used to fetch
     * @return Page containing the next pageSize of resources
     */
    public abstract Page nextPage(final Page page, final ReachRestClient client);

    /**
     * Fetch the prior page of resources.
     *
     * @param page current page of resources
     * @return Page containing the previous pageSize of resources
     */
    public Page previousPage(final Page page) {
        return previousPage(page, Reach.getRestClient());
    }

    /**
     * Fetch the prior page of resources using specified client.
     *
     * @param page   current page of resources
     * @param client client used to fetch
     * @return Page containing the previous pageSize of resources
     */
    public abstract Page previousPage(final Page page, final ReachRestClient client);

    public Integer getPageSize() {
        return pageSize;
    }

    public Reader pageSize(final int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public Integer getLimit() {
        return limit;
    }

    /**
     * Sets the max number of records to read.
     *
     * @param limit max number of records to read
     * @return this reader
     */
    public Reader limit(final int limit) {
        this.limit = limit;

        
        if (this.pageSize == null) {
            this.pageSize = this.limit.intValue();
        }
        

        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy