content: package com.alibaba.cola.dto;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.List;
  
  /**
   * Response with batch page record to return,
   * usually use in page query
   * <p/>
   * Created by xiaochu.lbj on 2020/06/30.
   */
  public class PageResponse<T> extends Response {
  
      private static final long serialVersionUID = 1L;
  
      private int totalCount = 0;
  
      private int pageSize = 1;
  
      private int pageIndex = 1;
  
      private Collection<T> data;
  
      public int getTotalCount() {
          return totalCount;
      }
  
      public void setTotalCount(int totalCount) {
          this.totalCount = totalCount;
      }
  
      public int getPageSize() {
          if (pageSize < 1) {
              return 1;
          }
          return pageSize;
      }
  
      public void setPageSize(int pageSize) {
          if (pageSize < 1) {
              this.pageSize = 1;
          } else {
              this.pageSize = pageSize;
          }
      }
  
      public int getPageIndex() {
          if (pageIndex < 1) {
              return 1;
          }
          return pageIndex;
      }
  
      public void setPageIndex(int pageIndex) {
          if (pageIndex < 1) {
              this.pageIndex = 1;
          } else {
              this.pageIndex = pageIndex;
          }
      }
  
      public List<T> getData() {
          if (null == data) {
              return Collections.emptyList();
          }
          if (data instanceof List) {
              return (List<T>) data;
          }
          return new ArrayList<>(data);
      }
  
      public void setData(Collection<T> data) {
          this.data = data;
      }
  
      public int getTotalPages() {
          return this.totalCount % this.pageSize == 0 ? this.totalCount
              / this.pageSize : (this.totalCount / this.pageSize) + 1;
      }
  
      public boolean isEmpty() {
          return data == null || data.isEmpty();
      }
  
      public boolean isNotEmpty() {
          return !isEmpty();
      }
  
      public static PageResponse buildSuccess() {
          PageResponse response = new PageResponse();
          response.setSuccess(true);
          return response;
      }
  
      public static PageResponse buildFailure(String errCode, String errMessage) {
          PageResponse response = new PageResponse();
          response.setSuccess(false);
          response.setErrCode(errCode);
          response.setErrMessage(errMessage);
          return response;
      }
  
      public static <T> PageResponse<T> of(int pageSize, int pageIndex) {
          PageResponse<T> response = new PageResponse<>();
          response.setSuccess(true);
          response.setData(Collections.emptyList());
          response.setTotalCount(0);
          response.setPageSize(pageSize);
          response.setPageIndex(pageIndex);
          return response;
      }
  
      public static <T> PageResponse<T> of(Collection<T> data, int totalCount, int pageSize, int pageIndex) {
          PageResponse<T> response = new PageResponse<>();
          response.setSuccess(true);
          response.setData(data);
          response.setTotalCount(totalCount);
          response.setPageSize(pageSize);
          response.setPageIndex(pageIndex);
          return response;
      }
  
  }
  