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

fun.fengwk.convention4j.api.page.DefaultPage Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package fun.fengwk.convention4j.api.page;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 
 * @author fengwk
 */
public class DefaultPage implements Page {

    private static final long serialVersionUID = 1L;

    private final int pageNumber;
    private final int pageSize;
    private final List results;
    private final long totalCount;

    public DefaultPage(int pageNumber, int pageSize, List results, long totalCount) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.results = results;
        this.totalCount = totalCount;
    }

    @Override
    public  Page map(Function mapper) {
        List mappedResult = results.stream().map(mapper).collect(Collectors.toList());
        return new DefaultPage<>(pageNumber, pageSize, mappedResult, totalCount);
    }

    @Override
    public  Page mapAll(Function, ? extends List> mapper) {
        List mappedResult = mapper.apply(results);
        return new DefaultPage<>(pageNumber, pageSize, mappedResult, totalCount);
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public List getResults() {
        return results;
    }

    @Override
    public long getTotalCount() {
        return totalCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DefaultPage that = (DefaultPage) o;
        return pageNumber == that.pageNumber && pageSize == that.pageSize && totalCount == that.totalCount && Objects.equals(results, that.results);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pageNumber, pageSize, results, totalCount);
    }

    @Override
    public String toString() {
        return "DefaultPage{" +
            "pageNumber=" + pageNumber +
            ", pageSize=" + pageSize +
            ", results=" + results +
            ", totalCount=" + totalCount +
            '}';
    }

}