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

io.github.nichetoolkit.rest.util.PartitionUtils Maven / Gradle / Ivy

The newest version!
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
 * 

The partition utils class.

* @author Cyan ([email protected]) * @since Jdk1.8 */ public class PartitionUtils { /** * partition *

The partition method.

* @param {@link java.lang.Object}

The parameter can be of any type.

* @param dataList {@link java.util.Collection}

The data list parameter is Collection type.

* @param partitionSize {@link java.lang.Integer}

The partition size parameter is Integer type.

* @param consumer {@link java.util.function.Consumer}

The consumer parameter is Consumer type.

* @see java.util.Collection * @see java.lang.Integer * @see java.util.function.Consumer */ 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); } } /** * query *

The query method.

* @param {@link java.lang.Object}

The parameter can be of any type.

* @param {@link java.lang.Object}

The parameter can be of any type.

* @param idList {@link java.util.Collection}

The id list parameter is Collection type.

* @param querySize {@link java.lang.Integer}

The query size parameter is Integer type.

* @param function {@link java.util.function.Function}

The function parameter is Function type.

* @return {@link java.util.List}

The query return object is List type.

* @see java.util.Collection * @see java.lang.Integer * @see java.util.function.Function * @see java.util.List */ 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; } /** * save *

The save method.

* @param {@link java.lang.Object}

The parameter can be of any type.

* @param dataList {@link java.util.Collection}

The data list parameter is Collection type.

* @param saveSize {@link java.lang.Integer}

The save size parameter is Integer type.

* @param function {@link java.util.function.Function}

The function parameter is Function type.

* @return {@link java.lang.Integer}

The save return object is Integer type.

* @see java.util.Collection * @see java.lang.Integer * @see java.util.function.Function */ 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; } /** * delete *

The delete method.

* @param {@link java.lang.Object}

The parameter can be of any type.

* @param idList {@link java.util.Collection}

The id list parameter is Collection type.

* @param deleteSize {@link java.lang.Integer}

The delete size parameter is Integer type.

* @param consumer {@link java.util.function.Consumer}

The consumer parameter is Consumer type.

* @see java.util.Collection * @see java.lang.Integer * @see java.util.function.Consumer */ public static void delete(Collection idList, Integer deleteSize, Consumer> consumer) { partition(idList,deleteSize,consumer); } }