io.github.nichetoolkit.rest.util.CollectUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
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
* The collect utils class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class CollectUtils {
/**
* collect
* The collect 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 srcCollection {@link java.util.Collection} The src collection parameter is Collection
type.
* @param function {@link io.github.nichetoolkit.rest.actuator.FunctionActuator} The function parameter is FunctionActuator
type.
* @return {@link java.util.Map} The collect return object is Map
type.
* @throws RestException {@link io.github.nichetoolkit.rest.RestException} The rest exception is RestException
type.
* @see java.util.Collection
* @see io.github.nichetoolkit.rest.actuator.FunctionActuator
* @see java.util.Map
* @see io.github.nichetoolkit.rest.RestException
*/
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;
}
/**
* collect
* The collect 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 srcCollection {@link java.util.Collection} The src collection parameter is Collection
type.
* @param function {@link java.util.function.Function} The function parameter is Function
type.
* @return {@link java.util.Map} The collect return object is Map
type.
* @see java.util.Collection
* @see java.util.function.Function
* @see java.util.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;
}
/**
* collect
* The collect 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 key K The key parameter is K
type.
* @param src V The src parameter is V
type.
* @param map {@link java.util.Map} The map parameter is Map
type.
* @see java.util.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);
}
}
/**
* collect
* The collect method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param key boolean The key parameter is boolean
type.
* @param data T The data parameter is T
type.
* @param falseCollection {@link java.util.Collection} The false collection parameter is Collection
type.
* @param trueCollection {@link java.util.Collection} The true collection parameter is Collection
type.
* @see java.util.Collection
*/
public static void collect(boolean key, T data, Collection falseCollection, Collection trueCollection) {
if (key) {
trueCollection.add(data);
} else {
falseCollection.add(data);
}
}
/**
* collect
* The collect 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 key K The key parameter is K
type.
* @param dataCollection {@link java.util.Collection} The data collection parameter is Collection
type.
* @param dataMap {@link java.util.Map} The data map parameter is Map
type.
* @see java.util.Collection
* @see java.util.Map
* @see java.lang.SuppressWarnings
*/
@SuppressWarnings("Duplicates")
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);
}
});
}
/**
* collect
* The collect 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 key K The key parameter is K
type.
* @param data T The data parameter is T
type.
* @param falseCollection {@link java.util.Collection} The false collection parameter is Collection
type.
* @param trueMap {@link java.util.Map} The true map parameter is Map
type.
* @see java.util.Collection
* @see java.util.Map
*/
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);
}
}
/**
* collect
* The collect 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 key K The key parameter is K
type.
* @param data T The data parameter is T
type.
* @param falseCollection {@link java.util.Collection} The false collection parameter is Collection
type.
* @param trueCollection {@link java.util.Collection} The true collection parameter is Collection
type.
* @see java.util.Collection
*/
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);
}
}
/**
* collect
* The collect method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param srcKey {@link java.lang.Long} The src key parameter is Long
type.
* @param targetKey {@link java.lang.Long} The target key parameter is Long
type.
* @param data T The data parameter is T
type.
* @param equalCollection {@link java.util.Collection} The equal collection parameter is Collection
type.
* @param unequalMap {@link java.util.Map} The unequal map parameter is Map
type.
* @see java.lang.Long
* @see java.util.Collection
* @see java.util.Map
*/
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);
}
}
/**
* collect
* The collect 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 srcKey K The src key parameter is K
type.
* @param targetKey K The target key parameter is K
type.
* @param data T The data parameter is T
type.
* @param equalCollection {@link java.util.Collection} The equal collection parameter is Collection
type.
* @param unequalCollection {@link java.util.Collection} The unequal collection parameter is Collection
type.
* @see java.util.Collection
*/
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);
}
}
/**
* collect
* The collect 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 srcKey K The src key parameter is K
type.
* @param targetKeyCollection {@link java.util.Collection} The target key collection parameter is Collection
type.
* @param data T The data parameter is T
type.
* @param dataMap {@link java.util.Map} The data map parameter is Map
type.
* @see java.util.Collection
* @see java.util.Map
*/
public static void collect(K srcKey, Collection targetKeyCollection, T data, Map> dataMap) {
if (targetKeyCollection.contains(srcKey)) {
collect(srcKey,data,dataMap);
}
}
}