Extension To The EnumUtils(Java) For Kotlin

之前学习了CSV文件的导出导入,运用的较为广泛,以后有时间咱们再继续学习.XLS/.XLSX格式的导出导入.在数据导出导入开发过程遇到的最为困难的就是数据导入了,你得限制用户数据的格式是否合法等等.接下来要说的是,在导入数据时关于这些问题中的一个枚举转换的问题,对org.apache.commons.lang3.EnumUtils进行符合自己要求的扩展,做个小的工具类.

自定义扩展接口:

//自定义扩展接口
interface ExtensionEnum {
    //自定义描述值
    fun getCustomerName() : String
    //自定义排序值
    fun getCustomerSort() : Int
    //自定义...
    .
    .
    .
    //根据实际需求多少个都行
}

自定义扩展接口2019-04-04 :

interface BaseEnum {
		val customerName: String
		val customerSort: Int
}

枚举定义:

enum class Flag(name: String , value: Int) : ExtensionEnum {
    YES("是" , 1) , NO("否" , 2);
    
	private val customerName: String = name
	private val customerSort: Int = value

	override fun getCustomerName(): String {
		return this.customerName
	}

	override fun getCustomerSort(): Int {
		return this.customerSort
	}
}

枚举定义(Kotlin)2019-04-04:

enum class Flag(override val customerName: String , override val customerSort: Int) : BaseEnum {
		YES("是" , 1) , NO("否" , 2) 
}

枚举定义(Java)2019-04-04: 

public enum IsFlag implements BaseEnum {
    YES("是", 0), NO("否", 1);
    private String label;
    private Integer level;

    IsFlag(String label, Integer level) {
        this.label = label;
        this.level = level;
    }

    @NotNull
    @Override
    public String getCustomerName() {
        return this.label;
    }

    @Override
    public int getCustomerSort() {
        return this.level;
    }

}

枚举小工具类:

object EnumUtils : EnumUtils() {

/**
 * if not found throw Exception value non-null
 * list to map 1 (K,V)
 * @param type 枚举类
 * @param customerSort 自定义排序
 * @return 对应枚举值
 */
 fun <T> getStringEnumValue(type: Class<T> , customerSort: Int): String where T : kotlin.Enum<T> , T : ExtensionEnum =
		 getEnumList(type).let { it.map { it.getCustomerSort() to it.name }.toMap()[customerSort] !! }

}

枚举小工具类2019-04-04:

class EnumUtil : EnumUtils() {
		companion object {
				/**
				 * 根据标签获取枚举类值
				 * @return if label is null or blank or not found enum value return null
				 */
				fun <T> getEnumValueByLabel(type: Class<T> , label: String?): T? where T : Enum<T> , T : BaseEnum =
						if (label.isNullOrBlank()) null else getEnumList(type).associateBy { it.customerName }[customerName]

				/**
				 * 根据级别获取枚举类值
				 * @return if level is null or blank or not found enum value return null
				 */
				fun <T> getEnumValueByLevel(type: Class<T> , level: Int?): T? where T : Enum<T> , T : BaseEnum =
						if (level.isNull()) null else getEnumList(type).associateBy { it.customerSort }[customerSort]

		}

}

 

/**
 * not found throw Exception value non-null
 * list to map 2 (K,V)
 * 根据枚举值(toString)获取自定义排序
 * @return 
 */
 fun <T> getCustomerSortByEnumValue(type: Class<T> , enumValue: String): Int where T : kotlin.Enum<T> , T : ExtensionEnum =
	 getEnumList(type).associateBy({ it.name } , { it.getCustomerSort() })[enumValue] !!
/**
 * 根据自定义排序获取枚举值
 * list to map 3 (K,T)
 * @param type 枚举类
 * @param customerSort 自定义排序
 * @return 枚举值 not found return null
 */
 fun <T> getEnumByCustomerSort(type: Class<T> , customerSort: Int): T? where T : kotlin.Enum<T> , T : ExtensionEnum =
	getEnumList(type).associateBy { it.getCustomerSort() }[customerSort]
/**
 * 根据自定义排序获取自定义描述
 * @param
 * @return 自定义描述 not found return ""
 */
 fun <T> getCustomerNameBySort(type: Class<T> , customerSort: Int): String where T : kotlin.Enum<T> , T : ExtensionEnum =
	 this.getEnumByCustomerSort(type , customerSort).let { if (it.isNull()) "" else it !!.getCustomerName() }


/**
 * 根据自定义描述获取枚举值
 * @param customerName 描述
 * @return 枚举值
 */
 fun <T> getEnumByCustomerName(type: Class<T> , customerName: String): T? where T : kotlin.Enum<T> , T : ExtensionEnum =
	getEnumList(type).associateBy { it.getCustomerName() }[customerName]


/**
 * 根据自定义描述获取自定义排序
 * @param customerName 自定义描述
 * @return 自定义排序 not found return -1
 */
 fun <T> getCustomerSortByName(type: Class<T> , customerName: String): Int where T : kotlin.Enum<T> , T : ExtensionEnum =
	 this.getEnumByCustomerName(type , customerName).let { if (it.isNull()) - 1 else it !!.getCustomerSort() }

 fun <T : kotlin.Enum<T>> getEnumClass(type: Class<T> , enumValue: String): T? = getEnum(type , enumValue)

测试使用:

fun main(args: Array<String>) {
	println(EnumUtils.getCustomerNameBySort(Flag::class.java , 1))
	println(EnumUtils.getEnumByCustomerName(Flag::class.java , "是"))
}

测试结果:

 这样以后让用户导数据就不用写"YES",而是用"是".咱们就能根据"是"找到"YES"了,妥妥的入库.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值