package cc.smtweb.framework.core.mvc.service;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class SwListData {
    public static final SwListData EMPTY = new SwListData(new ArrayList<>(), 0);

    private final List rows;
    // 总数, -1 表示需要异步获取数量， >=0 表示总数
    private int total = 0;

    private SwListData(List rows, int total) {
        this.rows = rows;
        this.total = total;
    }

    public static SwListData create(List list, int rows) {
        if (list == null) {
            return SwListData.EMPTY;
        }

        if (rows > 0 && list.size() >= rows) {
            return new SwListData(list, -1);
        } else {
            return new SwListData(list, list.size());
        }
    }

    public static SwListData create(List list) {
        if (list == null) {
            return SwListData.EMPTY;
        }

        return new SwListData(list, 0);
    }

    public boolean isEmpty() {
        return this.rows == null || this.rows.isEmpty();
    }
}
