package com.webchat.common.util;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Author: 程序员七七
 * @Dae: 3.6.22 10:53 下午
 */
public class ListUtil {

    public static void shuffle(List list) {
        int size = list.size();
        Random random = new Random();
        for(int i = 0; i < size; i++) {
            // 获取随机位置
            int randomPos = random.nextInt(size);
            // 当前元素与随机元素交换
            Object temp = list.get(i);
            list.set(i, list.get(randomPos));
            list.set(randomPos, temp);
        }
    }

    public static String list2String(List<Long> list, String separator) {
        return Joiner.on(separator).join(list);
    }

    public static String stringList2String(List<String> list, String separator) {
        return Joiner.on(separator).join(list);
    }

    public static List<Long> string2LongList(String str) {
        if (StringUtils.isBlank(str)) {
            return Collections.emptyList();
        }
        return Arrays.stream(str.split(",")).map(Long::valueOf).collect(Collectors.toList());
    }

    public static List<String> string2List(String str) {
        if (StringUtils.isBlank(str)) {
            return Collections.emptyList();
        }
        return Arrays.stream(str.split(",")).collect(Collectors.toList());
    }
}
