All Downloads are FREE. Search and download functionalities are using the official Maven repository.

love.keeping.starter.web.utils.EnumUtil Maven / Gradle / Ivy

The newest version!
package love.keeping.starter.web.utils;

import love.keeping.starter.common.exceptions.impl.DefaultSysException;
import love.keeping.starter.common.utils.ArrayUtil;
import love.keeping.starter.common.utils.CollectionUtil;
import love.keeping.starter.common.utils.ObjectUtil;
import love.keeping.starter.common.utils.StringUtil;
import love.keeping.starter.web.enums.BaseEnum;
import love.keeping.starter.web.common.utils.ApplicationUtil;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
 * 枚举工具类
 *
 * @author Keeping
 */
@Slf4j
public class EnumUtil {

  private static final Map>, List>> ENUM_POOL;

  static {
    ENUM_POOL = new ConcurrentHashMap<>();
  }

  public static > String getDesc(Class clazz,
      Serializable code) {

    List enumList = getEnumList(clazz);

    for (C c : enumList) {
      if (c.getCode().equals(code)) {
        return c.getDesc();
      }
    }

    return null;
  }

  public static > C getByCode(Class clazz,
      Serializable code) {

    if (ObjectUtil.isNull(code)) {
      return null;
    }

    List enumList = getEnumList(clazz);
    for (C c : enumList) {
      if (c.getCode() == null) {
        continue;
      }
      if (String.valueOf(c.getCode()).equals(String.valueOf(code))) {
        return c;
      }
    }

    return null;
  }

  public static > C getByDesc(Class clazz,
      String desc) {

    if (StringUtil.isNullOrUndefined(desc)) {
      return null;
    }

    List enumList = getEnumList(clazz);
    for (C c : enumList) {
      if (c.getDesc().equals(desc)) {
        return c;
      }
    }

    return null;
  }

  public static > List getDescs(Class clazz) {
    List enumList = getEnumList(clazz);
    return enumList.stream().map(t -> t.getDesc()).collect(Collectors.toList());
  }

  private static > List getEnumList(Class clazz) {

    if (!ENUM_POOL.containsKey(clazz)) {
      try {
        List enumList = new ArrayList<>();
        if (Enum.class.isAssignableFrom(clazz)) {
          // 如果是Enum类型
          Method method = clazz.getMethod("values");

          C[] enums = (C[]) method.invoke(clazz);

          if (ArrayUtil.isNotEmpty(enums)) {
            enumList.addAll(Arrays.asList(enums));
          }
        } else {
          // 如果不是Enum类型,那么必须是Bean
          Map enums = ApplicationUtil.getBeansOfType(clazz);
          if (!CollectionUtil.isEmpty(enums)) {
            enumList.addAll(enums.values());
          }
        }
        ENUM_POOL.put(clazz, enumList);
      } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        log.error(e.getMessage(), e);
        throw new DefaultSysException(e.getMessage());
      }
    }

    List enumList = (List) ENUM_POOL.get(clazz);

    return enumList;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy