动态Table后台处理

和前端对接时,table设置成List<Map<String,String>>这种格式,和react,vue获取格式相同;
body: //map: title ,内容 private List<Map<String,String>> data;如果body格式是private Map<String,List<String>> data;这种格式前端获取不符合他们结构,他们要把map里的key进行排序。

@Setter
@Getter
public class Table {
    private long id;
    private int columnSize;
    private int rowSize;
    private Meta meta;
    private Header header;
    private Body body;
    private Integer version;

    public Table() {
        this.rowSize = 0;
        this.columnSize = 0;
        this.meta = new Meta();
        this.header = new Header();
        this.body = new Body();
        this.version = 0;
    }
}
@Setter
@Getter
public class Header {

    private List<String> titles;

    public Header() {
        titles = new ArrayList<>();
    }

    public Header(List<String> titles){
        this.titles = titles;
    }
}
@Setter
@Getter
public class Body {


    private List<Map<String, String>> data;

    public Body() {
        this.data = new ArrayList<>();
    }

    public void add(int rowIndex, Map<String, String> partCells) {
        if (data.size() <= rowIndex) {
            for (int i = data.size(); i <= rowIndex; i++) {
                Map<String, String> cells = Maps.newHashMapWithExpectedSize(4);
                data.add(cells);
            }
        }
        data.get(rowIndex).putAll(partCells);
    }
}
@Setter
@Getter
public class Meta {
    private List<Integer> valueTypes;
    private List<List<String>> availValues;

    public Meta() {
        this.valueTypes = new ArrayList<>();
        this.availValues = new ArrayList<>();
    }

tableUtils
 

package com.ximalaya.abtest.backend.table;

import com.google.common.collect.Maps;
import com.ximalaya.abtest.backend.table.handler.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

@Slf4j
public class TableUtils {

    public static void iteratorHeader(Table table, HeaderHandler headerHandler) {
        checkHeader(table);
        List<String> titles = table.getHeader().getTitles();
        for (int i = 0; i < titles.size(); i++) {
            headerHandler.handle(i, titles.get(i));
        }
    }

    private static void checkHeader(Table table) {
        Header header = table.getHeader();
        if (null == header) {
            throw new IllegalArgumentException("header is null");
        }
        List<String> titles = header.getTitles();
        if (CollectionUtils.isEmpty(titles)) {
            throw new IllegalArgumentException("titles is null");
        }
    }

    private static void checkMeta(Table table) {
        Meta meta = table.getMeta();
        if (null == meta) {
            throw new IllegalArgumentException("meta is null");
        }
    }

    public static void iteratorHeader(Table table, int columnStartIndex, int columnEndIndex, HeaderHandler headerHandler) {
        checkHeader(table);
        List<String> titles = table.getHeader().getTitles();
        checkIndex(columnStartIndex, columnEndIndex, titles.size());
        for (int i = columnStartIndex; i < columnEndIndex; i++) {
            headerHandler.handle(i, titles.get(i));
        }
    }

    public static void iteratorHeader(Table table, int columnStartIndex, int columnEndIndex, HeaderGenerator headerGenerator) {

        List<String> titles = table.getHeader().getTitles();
        if (columnStartIndex >= columnEndIndex) {
            throw new IllegalArgumentException("columnStartIndex must smaller than columnEndIndex");
        }
        for (int i = columnStartIndex; i < columnEndIndex; i++) {
            String title = headerGenerator.generate(i);
            //表头不能为空,为空异常
            if (StringUtils.isEmpty(title)) {
                throw new IllegalArgumentException("title is null");
            }
            titles.add(i, title);
        }
    }

    public static void iteratorAvailValues(Table table, int columnStartIndex, int columnEndIndex, AvailValuesGenerator availValuesGenerator) {
        if (table.getMeta() == null) {
            Meta meta = new Meta();
            table.setMeta(meta);
        }
        Meta meta = table.getMeta();
        List<String> titles = table.getHeader().getTitles();
        if (CollectionUtils.isNotEmpty(titles)) {
            checkIndex(columnStartIndex, columnEndIndex, titles.size());
        }

        for (int i = columnStartIndex; i < columnEndIndex; i++) {
            List<String> availValues = availValuesGenerator.generateAvailValues(i);
            meta.getAvailValues().add(i, availValues);
        }
    }

    public static void iteratorValueType(Table table, int columnStartIndex, int columnEndIndex, ValueTypeGenerator valueTypeGenerator) {
        if (table.getMeta() == null) {
            Meta meta = new Meta();
            table.setMeta(meta);
        }
        Meta meta = table.getMeta();
        List<String> titles = table.getHeader().getTitles();
        if (CollectionUtils.isNotEmpty(titles)) {
            checkIndex(columnStartIndex, columnEndIndex, titles.size());
        }

        for (int i = columnStartIndex; i < columnEndIndex; i++) {
            Integer valueType = valueTypeGenerator.generateValueType(i);
            meta.getValueTypes().add(i, valueType);
        }
    }

    private static void checkIndex(int columnStartIndex, int columnEndIndex, int size) {
        if (columnStartIndex >= columnEndIndex) {
            throw new IllegalArgumentException("columnStartIndex must smaller than columnEndIndex");
        }
        //后面【columnStartIndex,columnEndIndex)
        if (columnEndIndex > size) {
            throw new IllegalArgumentException("columnEndIndex must smaller than size");
        }
    }

    public static void iteratorCell(Table table, CellHandler cellHandler) {
        checkHeader(table);
        List<String> titles = table.getHeader().getTitles();
        checkBody(table);
        List<Map<String, String>> trs = table.getBody().getData();
        for (int hi = 0; hi < titles.size(); hi++) {
            String title = titles.get(hi);
            //处理单行
            for (int ri = 0; ri < trs.size(); ri++) {
                Map<String, String> td = trs.get(ri);
                cellHandler.handle(hi, title, ri, td.get(title));
            }
        }
    }

    private static void checkBody(Table table) {
        Body body = table.getBody();
        if (null == body) {
            throw new IllegalArgumentException("body is null");
        }
        List<Map<String, String>> trs = body.getData();
        if (CollectionUtils.isEmpty(trs)) {
            throw new IllegalArgumentException("body data is null");
        }
    }

    public static void iteratorCell(Table table, int columnStartIndex, int columnEndIndex, CellHandler cellHandler) {
        checkHeader(table);
        List<String> titles = table.getHeader().getTitles();
        checkBody(table);
        List<Map<String, String>> trs = table.getBody().getData();
        checkIndex(columnStartIndex, columnEndIndex, titles.size());
        for (int ri = 0; ri < trs.size(); ri++) {
            cellHandler.onNewRow(ri);
            for (int hi = columnStartIndex; hi < columnEndIndex; hi++) {
                cellHandler.onNewColumn(hi, ri);
                String title = titles.get(hi);
                Map<String, String> td = trs.get(ri);
                cellHandler.handle(hi, title, ri, td.get(title));
            }
        }
    }

    public static void iteratorCell(Table table, int columnStartIndex, int columnEndIndex, CellGenerator cellGenerator) {
        checkHeader(table);
        List<String> titles = table.getHeader().getTitles();
        Body body = table.getBody();

        checkIndex(columnStartIndex, columnEndIndex, CollectionUtils.size(titles));
        for (int ri = 0; ri < table.getRowSize(); ri++) {
            Map<String, String> map = Maps.newHashMapWithExpectedSize(4);
            for (int hi = columnStartIndex; hi < columnEndIndex; hi++) {
                String title = titles.get(hi);
                String cellValue = cellGenerator.generate(hi, title, ri);
                map.put(title, cellValue);
            }
            body.add(ri, map);

        }
    }
}
发布了136 篇原创文章 · 获赞 65 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/jakeswang/article/details/83864711