import java.util.List;
public class PageResponse<T> {
private Integer pageNum;
private Integer pageSize;
private Integer total;
private Integer totalPages;
private List<T> list;
private PageResponse() {
}
public PageResponse(Integer pageNum, Integer pageSize) {
this.pageNum = pageNum;
this.pageSize = pageSize;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
public class TransformUtil {
private TransformUtil() {
}
public static <R, T> R transform(T source, Supplier<R> supplier) {
if (null == source) {
return null;
}
R target = supplier.get();
BeanUtils.copyProperties(source, target);
return target;
}
public static <R, T> R transform(T source, Function<T, R> transformMethod) {
if (null == source) {
return null;
}
return transformMethod.apply(source);
}
public static <R, T> List<R> transform(List<T> sourceList, Supplier<R> supplier) {
return transform(sourceList, (Function<T, R>) source -> transform(source, supplier));
}
public static <R, T> List<R> transform(List<T> sourceList, Function<T, R> transformMethod) {
List<R> targetList = new ArrayList<>(sourceList.size());
sourceList.forEach(source -> targetList.add(transform(source, transformMethod)));
return targetList;
}
public static <R, T> PageResponse<R> transform(PageResponse<T> sourcePage, Supplier<R> supplier) {
return transform(sourcePage, (Function<T, R>) source -> transform(source, supplier));
}
public static <R, T> PageResponse<R> transform(PageResponse<T> sourcePage, Function<T, R> transformMethod) {
PageResponse<R> targetPage = new PageResponse<>(sourcePage.getPageNum(), sourcePage.getPageSize());
targetPage.setTotalPages(sourcePage.getTotalPages());
targetPage.setTotal(sourcePage.getTotal());
List<R> targetList = new ArrayList<>(sourcePage.getList().size());
sourcePage.getList().forEach(source -> targetList.add(transform(source, transformMethod)));
targetPage.setList(targetList);
return targetPage;
}
}