'Combine Pagination of Two Different Entities With different size

I'm working on a problem where i have to do pagination of two data lists.

ex. psudo code

int recordsPerPage = 100; // this is dynamic
int currentPage = 1; // this is dynamic

array users = [...]; //length = 55
array admins = [....]; //length = 455


i want pagination like this.

perpage = 100;

user_size = 55;
admin_zise = 455;

total_recors = 510 // 455+55

total_pages = Maths.ceil(total_recors/per_page) //5.1 // 6

50 per entity // 100/2

// records will be sent like this.

page 1> 50 user  - 50  admin
page 2> 5  user  - 95  admin
page 3> 0  user  - 100 admin
page 4> 0  user  - 100 admin
page 5> 0  user  - 100 admin
page 6> 0  user  - 10  admin

I'm not able to figure out logic for this case.

I want to implement this is java (spring boot).



Solution 1:[1]

I'm not quite sure what you asking but this might be nearly your question:

@Data
class PaginationResponse {
    private List<Object> data;
    private int sizePerPage;
    private int total;
}

PaginationResponse getResponse(int page, int sizePerPage) {
    List<Object> object = new ArrayList<>();
    ObjectA[] arr_a = {};
    ObjectB[] arr_b = {};

    object.addAll(Arrays.asList(arr_a));
    object.addAll(Arrays.asList(arr_b));

    PaginationResponse response = new PaginationResponse();
    int start = (page - 1) * sizePerPage;
    response.setData(object.subList(start, start + sizePerPage));
    response.setSizePerPage(sizePerPage);
    response.setTotal(object.size());

            return response;
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paranaaan