根据枚举code获取枚举值

Posted 诺浅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据枚举code获取枚举值相关的知识,希望对你有一定的参考价值。

枚举接口类BaseIntEnum

package com.bt.common.core.enums;

import java.util.*;

/**
 * Enum implement this interface to customize:
 * <ol>
 * <li>@link #value() real numeric into DB. Default impl Enum's
 * @link #ordinal()</li>
 * <li>@link #text() description text(display for end user). Default impl
 * Enum's @link #toString()</li>
 * </ol>
 * 
 * <p>
 */
public interface BaseIntEnum 
	// enum methods///
	String name();
	int ordinal();
	/

	// customize value/text /
	/**
	 * actual value(db stored).
	 * 
	 * @return
	 */
	default int value() 
		return this.ordinal();
	

	/**
	 * description text(for end user)
	 * 
	 * @return
	 */
	default String text() 
		return this.toString();
	
	

	/**
	 * Deserialize from numeric value
	 * 
	 * @param enumType
	 * @param value
	 * @return
	 */
	public static <T extends Enum<?>> T valueOf(Class<T> enumType, int value) 
		T[] values = enumType.getEnumConstants();

		T result = null;
		if (BaseIntEnum.class.isAssignableFrom(enumType)) 
			for (T e : values) 
				if (((BaseIntEnum) e).value() == value) 
					result = e;
					break;
				
			
		 else if (value >= 0 && value < values.length) 
			result = values[value];
		

		if (result == null) 
			throw new IllegalArgumentException("Unknown enum constant " + enumType.getCanonicalName() + ": " + value);
		
		return result;
	

	/**
	 * get int-value by value(if implements this interface ) otherwise ordinal()
	 * 
	 * @param e
	 * @return
	 */
	public static <T extends Enum<?>> int value(T e) 
		if (e instanceof BaseIntEnum) 
			return ((BaseIntEnum) e).value();
		 else 
			return e.ordinal();
		
	

	/**
	 * key: enum element's name, value: enum element's text/toString
	 * 
	 * @param enumType
	 * @return
	 */
	public static <T extends Enum<?>> Map<String, String> mapElements(Class<T> enumType) 
		T[] values = enumType.getEnumConstants();
		if (values == null) 
			return Collections.emptyMap();
		
		Map<String, String> map = new HashMap<>(values.length);
		if (BaseIntEnum.class.isAssignableFrom(enumType)) 
			for (T e : values) 
				map.put(e.name(), ((BaseIntEnum) e).text());
			
		 else 
			for (T e : values) 
				map.put(e.name(), e.toString());
			
		
		return map;
	

	/**
	 * 
	 * @param enumType
	 * @return
	 */
	public static <T extends Enum<?>> List<EnumItemDesc> listElements(Class<T> enumType) 
		T[] values = enumType.getEnumConstants();
		if (values == null) 
			return Collections.emptyList();
		
		List<EnumItemDesc> list = new ArrayList<>(values.length);
		if (BaseIntEnum.class.isAssignableFrom(enumType)) 
			for (T e : values) 
				list.add(EnumItemDesc.of(e.name(), ((BaseIntEnum) e).text()));
			
		 else 
			for (T e : values) 
				list.add(EnumItemDesc.of(e.name(), e.toString()));
			
		
		return list;
	

	static public class EnumItemDesc 

		public static EnumItemDesc of(String name, String text) 
			return new EnumItemDesc(name, text);
		

		private String name;
		private String text;

		public EnumItemDesc(String name, String text) 
			this.name = name;
			this.text = text;
		

		public String getName() 
			return name;
		

		public void setName(String name) 
			this.name = name;
		

		public String getText() 
			return text;
		

		public void setText(String text) 
			this.text = text;
		

	


枚举实现类SystemType

public enum SystemType implements BaseIntEnum 

	PLATFORM(0, "平台"),
	HOSPITAL(1, "医院"),
	DEALER(2, "经销商"),
	VENDOR(3, "厂商"),
	
	;

	private final int value;
	private final String text;

	private SystemType(int value, String text) 
		this.value = value;
		this.text = text;
	

	@Override
	public int value() 
		return value;
	

	@Override
	public String text() 
		return text;
	

根据枚举code获取枚举值的工具类

public class EnumUtil 

    public static <T extends BaseIntEnum> T getByCode(Integer code, Class<T> enumClass) 
        // 通过反射取出Enum所有常量的属性值
        for (T each : enumClass.getEnumConstants()) 
            // 利用code进行循环比较,获取对应的枚举
            if (code.equals(each.value())) 
                return each;
            
        
        return null;
    

以上是关于根据枚举code获取枚举值的主要内容,如果未能解决你的问题,请参考以下文章

根据枚举code获取枚举值

如何在类中根据枚举值,获取枚举的message的工具类

怎么根据一个值获取它在枚举里的值?

C# 根据数字值获取相应枚举

第三章 EnumUtil根据值获取枚举对象

java 根据值获取枚举对象