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 org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
/**
* Paged resource object.
*
* @param data type
*/
public final class PagedResource {
private final PageHelper pager;
private final 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(@NotNull 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(new PageHelper() {
@Override
public @NotNull String pagedSql(char namedParamPrefix, @NotNull String sql) {
return "";
}
@Override
public @NotNull Args pagedArgs() {
return Args.of();
}
}, 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);
}
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;
}
}