All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.helper.PartitionHelper Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package io.github.nichetoolkit.rest.helper;


import com.google.common.collect.Lists;
import io.github.nichetoolkit.rest.RestException;
import io.github.nichetoolkit.rest.actuator.ConsumerActuator;
import io.github.nichetoolkit.rest.actuator.FunctionActuator;
import io.github.nichetoolkit.rest.util.GeneralUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 

PartitionHelper

* @author Cyan ([email protected]) * @version v1.0.0 */ public class PartitionHelper { public static void partition(Collection dataList, Integer partitionSize, ConsumerActuator> consumer) throws RestException { if (GeneralUtils.isEmpty(dataList)) { return; } if (dataList.size() > partitionSize) { List> partitionList = Lists.partition(new ArrayList<>(dataList), partitionSize); for (List partition : partitionList) { consumer.actuate(partition); } } else { consumer.actuate(dataList); } } public static List query(Collection idList, Integer querySize, FunctionActuator, List> function) throws RestException { if (GeneralUtils.isEmpty(idList)) { return Collections.emptyList(); } List entityList; if (idList.size() > querySize) { entityList = new ArrayList<>(); List> partitionList = Lists.partition(new ArrayList<>(idList), querySize); for (List partition : partitionList) { entityList.addAll(function.actuate(partition)); } } else { entityList = function.actuate(idList); } return entityList; } public static Integer save(Collection dataList, Integer saveSize, FunctionActuator,Integer> function) throws RestException { if (GeneralUtils.isEmpty(dataList)) { return 0; } Integer result = 0; if (dataList.size() > saveSize) { List> partitionList = Lists.partition(new ArrayList<>(dataList), saveSize); for (List partition : partitionList) { result += function.actuate(partition); } } else { result = function.actuate(dataList); } return result; } public static void delete(Collection idList, Integer deleteSize, ConsumerActuator> consumer) throws RestException { partition(idList,deleteSize,consumer); } }