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

com.alon.spring.crud.resource.dto.ListOutput Maven / Gradle / Ivy

Go to download

Projeto base para criação de serviços e recusos de CRUD com Spring Data JPA.

There is a newer version: 3.0.0
Show newest version
package com.alon.spring.crud.resource.dto;

import com.alon.spring.crud.model.BaseEntity;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.data.domain.Page;

public class ListOutput {
    
    protected List content;
    protected int page;
    protected int pageSize;
    protected int totalPages;
    protected int totalSize;

    public List getContent() {
        return content;
    }

    public int getPage() {
        return page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public int getTotalSize() {
        return totalSize;
    }
    
    public static  ListOutputBuilder of(Class type) {
        return new ListOutputBuilder<>(type);
    }
    
    public static  ListOutput 
        of(Page page, OutputDtoConverter outputConverter) {
        
        return new ListOutputBuilder(ListOutput.class)
                .page(page.getNumber() + 1)
                .pageSize(page.getNumberOfElements())
                .totalPages(page.getTotalPages())
                .totalSize(Long.valueOf(page.getTotalElements()).intValue())
                .content(page.getContent()
                             .stream()
                             .map(value -> outputConverter.convert(value))
                             .collect(Collectors.toList()))
                .build();
        
    }
    
    public static class ListOutputBuilder {
        
        private L output;
        
        private ListOutputBuilder(Class type) {
            try {
                this.output = type.getConstructor().newInstance();
            } catch (Exception ex) {
                throw new InternalError(ex);
            }
        }
        
        public  ListOutputBuilder content(List content) {
            this.output.content = content;
            return this;
        }
        
        public ListOutputBuilder page(int page) {
            this.output.page = page;
            return this;
        }
        
        public ListOutputBuilder pageSize(int pageSize) {
            this.output.pageSize = pageSize;
            return this;
        }
        
        public ListOutputBuilder totalPages(int totalPages) {
            this.output.totalPages = totalPages;
            return this;
        }
        
        public ListOutputBuilder totalSize(int totalSize) {
            this.output.totalSize = totalSize;
            return this;
        }
        
        public L build() {
            return this.output;
        }
        
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy