io.github.nichetoolkit.rest.util.CollectUtils 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 io.github.nichetoolkit.rest.RestException;
import io.github.nichetoolkit.rest.actuator.FunctionActuator;
import java.util.*;
import java.util.function.Function;
/**
* CollectUtils
* @author Cyan (snow22314 @ outlook.com)
* @version v1.0.0
*/
public class CollectUtils {
public static Map> collect(Collection srcCollection, FunctionActuator function) throws RestException {
Map> map = new HashMap<>();
for (V src : srcCollection) {
K key = function.actuate(src);
collect(key,src,map);
}
return map;
}
public static Map> collect(Collection srcCollection, Function function) {
Map> map = new HashMap<>();
for (V src : srcCollection) {
K key = function.apply(src);
collect(key,src,map);
}
return map;
}
public static void collect(K key,V src, Map> map) {
if (map.containsKey(key)) {
map.get(key).add(src);
} else {
List wrapList = new ArrayList<>();
wrapList.add(src);
map.put(key, wrapList);
}
}
public static void collect(boolean key, T data, Collection falseCollection, Collection trueCollection) {
if (key) {
trueCollection.add(data);
} else {
falseCollection.add(data);
}
}
public static void collect(K key, Collection dataCollection, Map> dataMap) {
Optional.ofNullable(key).ifPresent(value -> {
if (dataMap.containsKey(value)) {
dataMap.get(value).addAll(dataCollection);
} else {
List tempList = new ArrayList<>(dataCollection);
dataMap.put(value, tempList);
}
});
}
public static void collect(K key, T data, Collection falseCollection, Map> trueMap) {
Optional optional = Optional.ofNullable(key);
if (optional.isPresent()) {
collect(key, data, trueMap);
} else {
falseCollection.add(data);
}
}
public static void collect(K key, T data, Collection falseCollection, Collection trueCollection) {
Optional optional = Optional.ofNullable(key);
if (optional.isPresent()) {
trueCollection.add(data);
} else {
falseCollection.add(data);
}
}
public static void collect(Long srcKey, Long targetKey, T data, Collection equalCollection, Map> unequalMap) {
if (srcKey.equals(targetKey)) {
equalCollection.add(data);
} else {
collect(targetKey, data, unequalMap);
}
}
public static void collect(K srcKey, K targetKey, T data, Collection equalCollection, Collection unequalCollection) {
if (srcKey.equals(targetKey)) {
equalCollection.add(data);
} else {
unequalCollection.add(data);
}
}
public static void collect(K srcKey, Collection targetKeyCollection, T data, Map> dataMap) {
if (targetKeyCollection.contains(srcKey)) {
collect(srcKey,data,dataMap);
}
}
}