package com.yeejoin.equipmanage.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
 * @Author: songLei
 * @Description:
 * @Date: 2020/12/23 20:21
 * @Version: 1.0
 */
@Slf4j
public class EnumsUtils {
    private EnumsUtils() {
    }

    private static Map<Class,Object> map = new ConcurrentHashMap<>();



    /**
     * 根据条件获取枚举对象
     * @param className 枚举类
     * @param predicate 筛选条件
     * @param <T>
     * @return
     */
    public static <T> Optional<T> getEnumObject(Class<T> className, Predicate<T> predicate) {

        if(!className.isEnum()){
            log.info("Class 不是枚举类");
            return null;
        }
        Object obj = map.get(className);
        T[] ts = null;
        if(obj == null){
            ts = className.getEnumConstants();
            map.put(className,ts);
        }else{
            ts = (T[])obj;
        }
        return Arrays.stream(ts).filter(predicate).findAny();
    }
}
