com.github.chengyuxing.sql.PagedResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rabbit-sql Show documentation
Show all versions of rabbit-sql Show documentation
Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql
file.
package com.github.chengyuxing.sql;
import com.github.chengyuxing.sql.page.PageHelper;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
/**
* Paged resource object.
*
* @param data type
*/
public final class PagedResource {
private PageHelper pager;
private List data;
public PagedResource(PageHelper pager, List data) {
this.pager = pager;
this.data = data;
}
/**
* Returns a PagedResource.
*
* @param pager page helper instance
* @param data paged data
* @param data type
* @return PagedResource instance
*/
public static PagedResource of(PageHelper pager, List data) {
return new PagedResource<>(pager, data);
}
/**
* Returns an empty PagedResource.
*
* @param data type
* @return empty PagedResource
*/
public static PagedResource empty() {
return of(null, Collections.emptyList());
}
/**
* Convert paged resource to custom structured result, e.g.
*
*
* ({@link PageHelper pager}, data) -> {@link com.github.chengyuxing.common.DataRow DataRow}.of(
* "length", pager.getRecordCount(),
* "data", data)
* );
*
*
*
* @param converter (PageHelper, List) -> (new result)
* @param result type
* @return new structured result
*/
public R to(BiFunction, R> converter) {
return converter.apply(pager, data);
}
void setData(List data) {
this.data = data;
}
void setPager(PageHelper pager) {
this.pager = pager;
}
public List getData() {
return data;
}
public PageHelper getPager() {
return pager;
}
@Override
public String toString() {
return "Pageable{" +
"pager=" + pager +
", data=" + data +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PagedResource)) return false;
PagedResource> that = (PagedResource>) o;
if (getPager() != null ? !getPager().equals(that.getPager()) : that.getPager() != null) return false;
return getData() != null ? getData().equals(that.getData()) : that.getData() == null;
}
@Override
public int hashCode() {
int result = getPager() != null ? getPager().hashCode() : 0;
result = 31 * result + (getData() != null ? getData().hashCode() : 0);
return result;
}
}