package api.common.util;

import java.util.*;

public class CollectionUtil {

    public static <E> E[] createArray(E[] elements) {
        return elements;
    }

    public static int[] createIntArray(int... elements) {
        return elements;
    }

    public static <T> List<T> arrayToList(T[] array) {
        return new ArrayList<>(Arrays.asList(array));
    }


    @SafeVarargs
    public static <T> ArrayList<T> createArrayList(T... elements) {
        return new ArrayList<>(Arrays.asList(elements));
    }

    @SafeVarargs
    public static <T> HashSet<T> createHashSet(T... elements) {
        return new HashSet<>(Arrays.asList(elements));
    }

    public static <T> Set<T> listToSet(List<T> list) {
        return new HashSet<>(list);
    }

    public static <E> String listToSequence(List<E> list) {
        StringBuilder stringBuilder = new StringBuilder();
        list.forEach(element -> stringBuilder.append(element).append(","));
        if (list.size() != 0) {
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
        return stringBuilder.toString();
    }

    public static List<String> sequenceStringToList(String sequenceString, String regex) {
        if (sequenceString == null || "".equals(sequenceString)) {
            throw new RuntimeException("字符串" + sequenceString + "为空。");
        } else {
            if (sequenceString.contains(regex)) {
                return arrayToList(sequenceString.split(regex));
            } else {
                return createArrayList(sequenceString);
            }
        }
    }


    public static <T> List<T> setToList(Set<T> set) {
        return new ArrayList<>(set);
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isEmpty(ArrayList<?> arrayList) {
        return arrayList == null || arrayList.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static void addValueToMap(Map<String, Integer> map, int value, String key) {
        map.merge(key, value, Integer::sum);
    }


}
