package cn.com.qmth.stmms.biz.report.utils.unit;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RangeCounter {

    public int totalCount;

    public double interval;

    public Map<Double, Integer> map;

    public RangeCounter() {
        this.interval = 1;
        this.map = new HashMap<Double, Integer>();
    }

    public void process(double score) {
        totalCount++;
        // 取最接近的分段起始分数
        double min = Math.floor(score);
        while ((min + interval) < score) {
            min += interval;
        }
        // 以起始分数为key，累加计数结果
        Integer count = map.get(min);
        if (count == null) {
            count = 0;
        }
        count++;
        map.put(min, count);
    }

    /**
     * 获取在[s1, s2)分数范围内的数量<br>
     * s1和s2可以有一个为null，不能全部为null
     * 
     * @param s1
     * @param s2
     * @return
     */
    public int countGeAndLt(Double s1, Double s2) {
        int count = 0;
        if (s1 != null || s2 != null) {
            for (Entry<Double, Integer> entry : map.entrySet()) {
                Double key = entry.getKey();
                if (s1 != null && key < s1) {
                    continue;
                }
                if (s2 != null && key >= s2) {
                    continue;
                }
                count += entry.getValue();
            }
        }
        return count;
    }

}
