项目开发中常用的工具类



import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * Utilities for service.
 *
 * @author johnniang
 */
public class ServiceUtils {

    private ServiceUtils() {
    }

    /**
     * Fetches id to set.
     *
     * @param datas data collection
     * @param mappingFunction calculate the id in data list
     * @param <I> id type
     * @param <T> data type
     * @return a set of id
     */
    @NonNull
    public static <I, T> Set<I> fetchProperty(final Collection<T> datas,
        Function<T, I> 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 <I> id type
     * @param <D> data type
     * @return a map which key is in ids and value containing in list
     */
    @NonNull
    public static <I, D> Map<I, List<D>> convertToListMap(Collection<I> ids, Collection<D> list,
        Function<D, I> mappingFunction) {
        Assert.notNull(mappingFunction, "mapping function must not be null");

        if (CollectionUtils.isEmpty(ids) || CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }

        Map<I, 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 <I> id type
     * @param <D> data type
     * @return a map which key from list data and value is data
     */
    @NonNull
    public static <I, D> Map<I, D> convertToMap(Collection<D> list,
        Function<D, I> mappingFunction) {
        Assert.notNull(mappingFunction, "mapping function must not be null");

        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }

        Map<I, D> resultMap = new HashMap<>();

        list.forEach(data -> resultMap.putIfAbsent(mappingFunction.apply(data), data));

        return resultMap;
    }

    /**
     * Converts to map (key from the list data)
     *
     * @param list data list
     * @param keyFunction key mapping function
     * @param valueFunction value mapping function
     * @param <I> id type
     * @param <D> data type
     * @param <V> value type
     * @return a map which key from list data and value is data
     */
    @NonNull
    public static <I, D, V> Map<I, V> convertToMap(@Nullable Collection<D> list,
        @NonNull Function<D, I> keyFunction, @NonNull Function<D, V> valueFunction) {
        Assert.notNull(keyFunction, "Key function must not be null");
        Assert.notNull(valueFunction, "Value function must not be null");

        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }

        Map<I, V> resultMap = new HashMap<>();

        list.forEach(
            data -> resultMap.putIfAbsent(keyFunction.apply(data), valueFunction.apply(data)));

        return resultMap;
    }

    /**
     * Checks if the given number id is empty id.
     *
     * @param id the given number id
     * @return true if the given number id is empty id; false otherwise
     */
    public static boolean isEmptyId(@Nullable Number id) {
        return id == null || id.longValue() <= 0;
    }

    /**
     * Builds latest page request.
     *
     * @param top top must not be less than 1
     * @return latest page request
     */
    @NonNull
    public static Pageable buildLatestPageable(int top) {
        return buildLatestPageable(top, "createTime");
    }

    /**
     * Builds latest page request.
     *
     * @param top top must not be less than 1
     * @param sortProperty sort property must not be blank
     * @return latest page request
     */
    @NonNull
    public static Pageable buildLatestPageable(int top, @NonNull String sortProperty) {
        Assert.isTrue(top > 0, "Top number must not be less than 0");
        Assert.hasText(sortProperty, "Sort property must not be blank");

        return PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, sortProperty));
    }

    /**
     * Build empty page result.
     *
     * @param page page info must not be null
     * @param <T> target page result type
     * @param <S> source page result type
     * @return empty page result
     */
    @NonNull
    public static <T, S> Page<T> buildEmptyPageImpl(@NonNull Page<S> page) {
        Assert.notNull(page, "Page result must not be null");

        return new PageImpl<>(Collections.emptyList(), page.getPageable(), page.getTotalElements());
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值