mirror of https://github.com/halo-dev/halo
Add ServiceUtils
parent
df26777ddc
commit
0321f73a7e
|
@ -0,0 +1,86 @@
|
||||||
|
package cc.ryanc.halo.utils;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.logging.Logger;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utilities for service.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
*/
|
||||||
|
public class ServiceUtils {
|
||||||
|
|
||||||
|
private final static Logger LOGGER = Logger.getLogger(ServiceUtils.class);
|
||||||
|
|
||||||
|
private ServiceUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches id to set.
|
||||||
|
*
|
||||||
|
* @param datas data collection
|
||||||
|
* @param mappingFunction calculate the id in data list
|
||||||
|
* @param <ID> id type
|
||||||
|
* @param <T> data type
|
||||||
|
* @return a set of id
|
||||||
|
*/
|
||||||
|
public static <ID, T> Set<ID> fetchProperty(final Collection<T> datas, Function<T, ID> mappingFunction) {
|
||||||
|
return CollectionUtils.isEmpty(datas) ?
|
||||||
|
Collections.emptySet() :
|
||||||
|
datas.stream().map(mappingFunction).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a list to a list map where list contains id in ids.
|
||||||
|
*
|
||||||
|
* @param ids id collection
|
||||||
|
* @param list data list
|
||||||
|
* @param mappingFunction calculate the id in data list
|
||||||
|
* @param <ID> id type
|
||||||
|
* @param <D> data type
|
||||||
|
* @return a map which key is in ids and value containing in list
|
||||||
|
*/
|
||||||
|
public static <ID, D> Map<ID, List<D>> convertToListMap(Collection<ID> ids, Collection<D> list, Function<D, ID> mappingFunction) {
|
||||||
|
Assert.notNull(mappingFunction, "mapping function must not be null");
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(ids) || CollectionUtils.isEmpty(list)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<ID, List<D>> resultMap = new HashMap<>();
|
||||||
|
|
||||||
|
list.forEach(data -> resultMap.computeIfAbsent(mappingFunction.apply(data), id -> new LinkedList<>()).add(data));
|
||||||
|
|
||||||
|
ids.forEach(id -> resultMap.putIfAbsent(id, Collections.emptyList()));
|
||||||
|
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts to map (key from the list data)
|
||||||
|
*
|
||||||
|
* @param list data list
|
||||||
|
* @param mappingFunction calclulate the id from list data
|
||||||
|
* @param <ID> id type
|
||||||
|
* @param <D> data type
|
||||||
|
* @return a map which key from list data and value is data
|
||||||
|
*/
|
||||||
|
public static <ID, D> Map<ID, D> convertToMap(Collection<D> list, Function<D, ID> mappingFunction) {
|
||||||
|
Assert.notNull(mappingFunction, "mapping function must not be null");
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<ID, D> resultMap = new HashMap<>();
|
||||||
|
|
||||||
|
list.forEach(data -> resultMap.putIfAbsent(mappingFunction.apply(data), data));
|
||||||
|
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue