com.github.dennisit.vplus.data.utils.SliceUtils Maven / Gradle / Ivy
package com.github.dennisit.vplus.data.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SliceUtils {
private static final Integer SLICE_SIZE = 500;
/**
* 分片提交回调接口
*/
public interface Handle {
void handle(Collection collection);
}
/**
* 分片提交
*
* @param collection collection
* @param handle handle
*/
public static void handle(Collection collection, Handle handle) {
List list = new ArrayList<>(collection);
int offset = 0, limit;
while (offset < collection.size()) {
limit = Math.min(offset + SLICE_SIZE, collection.size());
handle.handle(list.subList(offset, limit));
offset = limit;
}
}
/**
* @param collection 集合
* @param handle 处理逻辑
* @param size 分片大小
*/
public static void handle(Collection collection, Handle handle, int size) {
List list = new ArrayList<>(collection);
int offset = 0, limit;
while (offset < collection.size()) {
limit = Math.min(offset + size, collection.size());
handle.handle(list.subList(offset, limit));
offset = limit;
}
}
}