package com.boman.common.core.utils.collection;

import com.alibaba.fastjson.JSONArray;
import com.boman.common.core.utils.obj.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author shiqian
 * @date 2021年03月26日 14:16
 **/
public class CollectionUtils {

    public static Long[] listToArray(List<Long> longList){
        ObjectUtils.requireNonNull(longList, "listToArray input data is empty");
        return longList.toArray(new Long[0]);
    }

    public static boolean containsIgnoreCase(Collection<String> list, String key) {
        return list.contains(key.toLowerCase()) || list.contains(key.toUpperCase());
    }

    public static <T> String listToString(List<T> list){
        StringBuilder buffer = new StringBuilder();
        for (T t : list) {
            buffer.append(t);
        }

        return buffer.toString();
    }

    public static <T> List<T> addAllDistinct(List<T> one, List<T> another) {
        one.addAll(another);
        return one.stream().distinct().collect(Collectors.toList());
    }

    @SuppressWarnings("unchecked")
    public static <T> void addJSONArray(Set<T> set, JSONArray jsonArray) {
        if (null == jsonArray || jsonArray.size() <= 0) {
            return;
        }

        for (Object object : jsonArray) {
            T input = (T) object;
            set.add(input);
        }
    }
}
