All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commons.box.app.DataResult Maven / Gradle / Ivy

The newest version!
package commons.box.app;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * 数据结果对象 用于表示查询结果(包含 result list 和 page 信息)
 */
public class DataResult implements DataObject {
    private List result;
    private DataPage page;


    public DataResult() {
        this.result = new ArrayList<>();
        this.page = null;
    }

    public DataResult(List result) {
        this.result = result;
        this.page = null;
    }

    public DataResult(List result, long offset, long limit) {
        this.result = result;
        this.page = new DataPage(offset, limit);
    }

    public DataResult(T[] result, long offset, long limit) {
        this.result = Arrays.asList(result);
        this.page = new DataPage(offset, limit);
    }


    public DataResult(List result, long offset, long limit, long count) {
        this.result = result;
        this.page = new DataPage(offset, limit, count);
    }

    public DataResult(T[] result, long offset, long limit, long count) {
        this.result = Arrays.asList(result);
        this.page = new DataPage(offset, limit, count);
    }


    public DataResult(List result, DataPage page) {
        this.result = result;
        this.page = page;
    }

    public DataResult(T[] result, DataPage page) {
        this.result = Arrays.asList(result);
        this.page = page;
    }

    @Nonnull
    public static  DataResult inst() {
        return new DataResult<>();
    }

    @Nonnull
    public static  DataResult inst(List result) {
        return new DataResult<>(result);
    }

    @Nonnull
    public DataResult result(List result) {
        this.setResult(result);
        return this;
    }


    @Nonnull
    @SafeVarargs
    public final DataResult result(T... result) {
        this.setResult(Arrays.asList(result));
        return this;
    }

    @Nonnull
    public DataResult page(DataPage page) {
        this.setPage((page != null) ? page : DataPage.inst());
        return this;
    }

    @Nonnull
    public DataResult page(long offset, long limit) {
        DataPage dp = this.getPage();
        if (dp == null) dp = DataPage.inst();
        this.setPage(dp.page(offset, limit));
        return this;
    }

    @Nonnull
    public DataResult page(long offset, long limit, long count) {
        DataPage dp = this.getPage();
        if (dp == null) dp = DataPage.inst();
        this.setPage(dp.page(offset, limit).count(count));
        return this;
    }

    @Nonnull
    public DataResult count(long count) {
        DataPage dp = this.getPage();
        if (dp == null) dp = DataPage.inst();
        this.setPage(dp.count(count));
        return this;
    }

    /**
     * 遍历并执行
     *
     * @param cmd
     * @return
     */
    @Nonnull
    public DataResult iter(Consumer cmd) {
        List rs = this.getResult();
        if (cmd == null || rs == null) return this;
        rs.forEach(cmd);
        return this;
    }

    /**
     * 遍历并执行
     *
     * @param cmd
     * @return
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public 

DataResult iter(Class type, Consumer

cmd) { List rs = this.getResult(); if (cmd == null || rs == null) return this; rs.forEach(p -> cmd.accept((P) p)); return this; } /** * 遍历并执行 * * @param cmd * @return */ @Nonnull public DataResult iter(BiConsumer cmd) { List rs = this.getResult(); if (cmd == null || rs == null) return this; for (int i = 0; i < rs.size(); i++) cmd.accept(rs.get(i), i); return this; } /** * 遍历并执行 * * @param cmd * @return */ @SuppressWarnings("unchecked") @Nonnull public

DataResult iter(Class type, BiConsumer cmd) { List rs = this.getResult(); if (cmd == null || rs == null) return this; for (int i = 0; i < rs.size(); i++) { T r = rs.get(i); cmd.accept((r != null) ? (P) r : null, i); } return this; } /** * 特殊标记 表示此对象时 data result * * @return */ public final boolean isDataResult() { return true; } public List getResult() { return result; } public void setResult(List result) { this.result = result; } public DataPage getPage() { return page; } public void setPage(DataPage page) { this.page = page; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DataResult that = (DataResult) o; return Objects.equals(getResult(), that.getResult()) && Objects.equals(getPage(), that.getPage()); } @Override public int hashCode() { return Objects.hash(getResult(), getPage()); } @Override public String toString() { return "DataResult{" + "result=" + result + ", page=" + page + '}'; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy