io.github.nichetoolkit.rest.util.PartitionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-toolkit-utils Show documentation
Show all versions of rest-toolkit-utils Show documentation
Rest toolkit utils project for Spring Boot
package io.github.nichetoolkit.rest.util;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* PartitionUtils
* @author Cyan ([email protected])
* @version v1.0.0
*/
public class PartitionUtils {
public static void partition(Collection dataList, Integer partitionSize, Consumer> consumer) {
if (GeneralUtils.isEmpty(dataList)) {
return;
}
if (dataList.size() > partitionSize) {
List> partitionList = Lists.partition(new ArrayList<>(dataList), partitionSize);
partitionList.forEach(consumer);
} else {
consumer.accept(dataList);
}
}
public static List query(Collection idList, Integer querySize, Function, List> function) {
if (GeneralUtils.isEmpty(idList)) {
return Collections.emptyList();
}
List entityList;
if (idList.size() > querySize) {
entityList = new ArrayList<>();
List> partitionList = Lists.partition(new ArrayList<>(idList), querySize);
partitionList.stream().map(function).forEach(entityList::addAll);
} else {
entityList = function.apply(idList);
}
return entityList;
}
public static Integer save(Collection dataList, Integer saveSize, Function,Integer> function) {
if (GeneralUtils.isEmpty(dataList)) {
return 0;
}
Integer result = 0;
if (dataList.size() > saveSize) {
List> partitionCollection = Lists.partition(new ArrayList<>(dataList), saveSize);
for (Collection partition : partitionCollection) {
result += function.apply(partition);
}
} else {
result = function.apply(dataList);
}
return result;
}
public static void delete(Collection idList, Integer deleteSize, Consumer> consumer) {
partition(idList,deleteSize,consumer);
}
}