1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package cn.com.qmth.export;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.collections4.CollectionUtils;
- /**
- * 多次批量获取数据
- * @author xiatian
- * @param <R> 结果类
- * @param <P> 参数类
- */
- public abstract class BatchGetDataUtil<R,P> {
- /**
- * @param resultList 全部结果集合
- * @param paramList 全部参数集合
- * @param batchSize 每批参数数量
- */
- public final List<R> getDataForBatch(List<P> paramList,int batchSize) {
- if(CollectionUtils.isEmpty(paramList)) {
- return null;
- }
- List<R> resultList=new ArrayList<>();
- if(paramList.size()<=batchSize) {
- List<R> temlist = getData(paramList);
- if(temlist!=null&&temlist.size()>0) {
- resultList.addAll(temlist);
- }
- }else {
- int size = paramList.size();
- int len=batchSize;
- int count = (size + len - 1) / len;
- for (int i = 0; i < count; i++) {
- List<P> subList = paramList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
- List<R> temlist = getData(subList);
- if(temlist!=null&&temlist.size()>0) {
- resultList.addAll(temlist);
- }
- }
- }
- return resultList;
- }
- /**
- * 每批获取数据方法
- * @param <R>
- * @param <P>
- * @param paramList 获取每批数据时参数
- * @return
- */
- protected abstract List<R> getData(List<P> paramList);
- }
|