com.stellariver.milky.common.base.PageResult Maven / Gradle / Ivy
package com.stellariver.milky.common.base;
import java.util.Collections;
import java.util.List;
public class PageResult extends Result> {
private final Paginator paginator = new Paginator();
private PageResult() {
super();
}
private PageResult(List data, long total, long pageNo, long pageSize) {
super();
this.success = true;
this.data = data;
paginator.setPageNo(pageNo);
paginator.setPageSize(pageSize);
paginator.setPageCount((total/pageSize + (total%pageSize == 0 ? 0 : 1)));
paginator.setTotal(total);
}
public Paginator getPaginator() {
return this.paginator;
}
public static PageResult success(List data, long total, long pageNo, long pageSize) {
return new PageResult<>(data, total, pageNo, pageSize);
}
public static PageResult empty(long pageNo, long pageSize) {
return success(Collections.emptyList(), 0L, pageNo, pageSize);
}
public static PageResult pageError(Error error) {
PageResult result = new PageResult<>();
result.success = false;
result.errorCode = error.getCode();
result.message = error.getMessage();
result.errors = Collections.singletonList(error);
return result;
}
public static PageResult pageError(List errors) {
PageResult result = new PageResult<>();
result.success = false;
result.errorCode = errors.get(0).getCode();
result.message = errors.get(0).getMessage();
result.errors = errors;
return result;
}
}