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

com.alipay.rdf.file.model.Summary Maven / Gradle / Ivy

There is a newer version: 2.2.11
Show newest version
package com.alipay.rdf.file.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alipay.rdf.file.exception.RdfErrorEnum;
import com.alipay.rdf.file.exception.RdfFileException;
import com.alipay.rdf.file.meta.StatisticPairMeta;
import com.alipay.rdf.file.summary.StatisticPair;
import com.alipay.rdf.file.util.RdfFileUtil;

/**
 * Copyright (C) 2013-2018 Ant Financial Services Group
 *
 * @author hongwei.quhw
 * @version $Id: Summary.java, v 0.1 2018年3月12日 下午4:22:36 hongwei.quhw Exp $
 */
@SuppressWarnings("rawtypes")
public class Summary {
    private String                    totalCountKey;
    /** 总记录数*/
    private Object                    totalCount;
    /** 汇总字段*/
    private final List   headSummaryPairs   = new ArrayList();
    private final List   tailSummaryPairs   = new ArrayList();
    private final List   summaryPairs       = new ArrayList();

    private final List headStatisticPairs = new ArrayList();
    private final List tailStatisticPairs = new ArrayList();
    private final List statisticPairs     = new ArrayList();

    public void addSummaryPair(SummaryPair pair) {
        if (RdfFileUtil.isNotBlank(pair.getHeadKey())) {
            headSummaryPairs.add(pair);
        }
        if (RdfFileUtil.isNotBlank(pair.getTailKey())) {
            tailSummaryPairs.add(pair);
        }

        summaryPairs.add(pair);
    }

    public void addStatisticPair(StatisticPairMeta pairMeta) {
        StatisticPair pair = new StatisticPair(pairMeta);
        if (FileDataTypeEnum.HEAD.equals(pairMeta.getStatisticdataType())) {
            headStatisticPairs.add(pair);
        }
        if (FileDataTypeEnum.TAIL.equals(pairMeta.getStatisticdataType())) {
            tailStatisticPairs.add(pair);
        }

        statisticPairs.add(pair);
    }

    public List getHeadSummaryPairs() {
        return headSummaryPairs;
    }

    public List getTailSummaryPairs() {
        return tailSummaryPairs;
    }

    public List getSummaryPairs() {
        return summaryPairs;
    }

    public List getHeadStatisticPairs() {
        return headStatisticPairs;
    }

    public List getTailStatisticPairs() {
        return tailStatisticPairs;
    }

    public List getStatisticPairs() {
        return statisticPairs;
    }

    /**
     * 总记录加加
     */
    public void addTotalCount(Object count) {
        RdfFileUtil.assertNotNull(count, "rdf-file#Summary.addTotalCount(Object count== null)");
        if (count instanceof BigDecimal) {
            if (totalCount == null) {
                totalCount = count;
            } else {
                totalCount = ((BigDecimal) totalCount).add((BigDecimal) count);
            }
        } else if (count instanceof Long) {
            if (null == totalCount) {
                totalCount = count;
            } else {
                totalCount = (Long) totalCount + (Long) count;
            }
        } else if (count instanceof Integer) {
            if (null == totalCount) {
                totalCount = count;
            } else {
                totalCount = (Integer) totalCount + (Integer) count;
            }
        } else {
            throw new RdfFileException(
                "rdf-file#Summary.addTotalCount  不支持计算类型为:" + count.getClass().getName(),
                RdfErrorEnum.COLUMN_TYPE_ERROR);
        }
    }

    public void setTotalCountKey(String totalCountKey) {
        this.totalCountKey = totalCountKey;
    }

    public Object getTotalCount() {
        return totalCount;
    }

    public Object getTotalCountWithoutNull() {
        return totalCount == null ? 0 : totalCount;
    }

    public Map summaryHeadToMap() {
        Map map = new HashMap();
        map.put(totalCountKey, getTotalCountWithoutNull());

        for (SummaryPair summaryPair : headSummaryPairs) {
            if (null == summaryPair.getSummaryValue()) {
                map.put(summaryPair.getHeadKey(), summaryPair.initDefaultColumnValue());
            } else {
                map.put(summaryPair.getHeadKey(), summaryPair.getSummaryValue());
            }
        }

        for (StatisticPair pair : headStatisticPairs) {
            map.put(pair.getHeadKey(), pair.getStaticsticValue());
        }

        return map;
    }

    public Map summaryTailToMap() {
        Map map = new HashMap();
        map.put(totalCountKey, getTotalCountWithoutNull());

        for (SummaryPair summaryPair : tailSummaryPairs) {
            if (null == summaryPair.getSummaryValue()) {
                map.put(summaryPair.getTailKey(), summaryPair.initDefaultColumnValue());
            } else {
                map.put(summaryPair.getTailKey(), summaryPair.getSummaryValue());
            }
        }

        for (StatisticPair pair : tailStatisticPairs) {
            map.put(pair.getTailKey(), pair.getStaticsticValue());
        }

        return map;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy