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

com.codingapi.springboot.framework.dto.response.MultiResponse Maven / Gradle / Ivy

There is a newer version: 3.3.12
Show newest version
package com.codingapi.springboot.framework.dto.response;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;

import java.util.Collection;

/**
 * @author lorne
 * 2020/12/17
 */
@Setter
@Getter
public class MultiResponse extends Response {

    private Content data;

    public MultiResponse() {
        this.data = new Content<>();
    }

    public static  MultiResponse of(Collection data, long total) {
        MultiResponse multiResponse = new MultiResponse<>();
        multiResponse.setSuccess(true);
        multiResponse.getData().setTotal(total);
        multiResponse.getData().setList(data);
        return multiResponse;
    }

    public static  MultiResponse of(Page page) {
        return of(page.getContent(), page.getTotalElements());
    }

    public static  MultiResponse empty() {
        MultiResponse multiResponse = new MultiResponse<>();
        multiResponse.setSuccess(true);
        return multiResponse;
    }

    public static  MultiResponse of(Collection data) {
        MultiResponse multiResponse = new MultiResponse<>();
        multiResponse.setSuccess(true);
        long total = 0;
        if (data != null) {
            total = data.size();
        }
        multiResponse.getData().setTotal(total);
        multiResponse.getData().setList(data);
        return multiResponse;
    }

    @Setter
    @Getter
    public static class Content {
        private long total;
        private Collection list;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy