package com.engine.organization.util;

import com.engine.organization.enums.BaseEnum;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/6/14
 * @Version V1.0
 **/
public class OrganizationEnumUtil {
    /**
     * 根据枚举的value获取枚举对象
     *
     * @param value 枚举中的value
     * @param list
     * @param T
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T enumMatchByValue(Integer value, BaseEnum<Integer>[] list, Class<? extends BaseEnum<Integer>> T) {
        return (T) Arrays.stream(list).filter(item -> Objects.equals(item.getValue(), value)).findFirst().get();
    }

    /**
     * 枚举数组转字符串
     *
     * @param list
     * @return
     */
    public static String enumArrToString(BaseEnum<Integer>[] list) {
        List<String> collect = Arrays.stream(list).map(item -> String.valueOf(item.getValue())).collect(Collectors.toList());
        return StringUtils.join(collect, ",");
    }


}
