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

com.obj.nc.utils.PagingUtils Maven / Gradle / Ivy

package com.obj.nc.utils;

import com.obj.nc.config.PagingConfigProperties;
import com.obj.nc.exceptions.PayloadValidationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.function.Predicate;

import static java.lang.String.format;

public class PagingUtils {
    public static Pageable createPageRequest(int pageNumber, int pageSize, PagingConfigProperties pagingProps) {
        int maxPageSize = pagingProps.getMaxPageSize();
        boolean oneIndexedParameters = pagingProps.isOneIndexedParameters();
        int lowerBound = oneIndexedParameters ? 1 : 0;

        assertCondition(pageNumber, n -> n >= lowerBound,
                format("Page number is out of bounds: %d, indexed from one: %b", pageNumber, oneIndexedParameters));
        assertCondition(pageSize, s -> s <= maxPageSize,
                format("Page size is out of bounds: %d, max page size: %d", pageSize, maxPageSize));

        return PageRequest.of(pageNumber - lowerBound, pageSize);
    }

    private static  void assertCondition(T obj, Predicate condition, String errorMessage) {
        if (!condition.test(obj)) {
            throw new PayloadValidationException(errorMessage);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy