package cn.com.qmth.examcloud.core.questions.service.util;

import java.util.List;

public abstract class BatchSetDataUtil<P> {
	/**
	 * @param dataList 待填充的对象集合
	 * @param batchSize 每批数量
	 */
	public final void setDataForBatch(List<P> dataList, int batchSize) {
		if (dataList == null || dataList.size() == 0) {
			return;
		}
		if (dataList.size() <= batchSize) {
			setData(dataList);
		} else {
			int size = dataList.size();
			int len = batchSize;
			int count = (size + len - 1) / len;

			for (int i = 0; i < count; i++) {
				List<P> subList = dataList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
				setData(subList);
			}
		}
	}

	protected abstract void setData(List<P> dataList);
}
