From 6a1545e6f0f8177a6e27479f4faac275407a2871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Mon, 11 Apr 2022 00:11:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(IBaseEnum.java):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=80=BC=E8=8E=B7=E5=8F=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/youlai/common/base/IBaseEnum.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/base/IBaseEnum.java b/youlai-common/common-core/src/main/java/com/youlai/common/base/IBaseEnum.java index 2cbc5d821..79b90e256 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/base/IBaseEnum.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/base/IBaseEnum.java @@ -19,10 +19,11 @@ public interface IBaseEnum { String getLabel(); /** + * 根据值获取枚举 * * @param value * @param clazz - * @param 枚举 + * @param 枚举 * @return */ static & IBaseEnum> E getEnumByValue(Object value, Class clazz) { @@ -35,6 +36,14 @@ public interface IBaseEnum { return matchEnum; } + /** + * 根据值获取标签 + * + * @param value + * @param clazz + * @param + * @return + */ static & IBaseEnum> String getLabelByValue(Object value, Class clazz) { Objects.requireNonNull(value); EnumSet allEnums = EnumSet.allOf(clazz); // 获取类型下的所有枚举 @@ -51,4 +60,29 @@ public interface IBaseEnum { } + /** + * 根据值获取标签 + * + * @param label + * @param clazz + * @param + * @return + */ + static & IBaseEnum> Object getValueByLabel(String label, Class clazz) { + Objects.requireNonNull(label); + EnumSet allEnums = EnumSet.allOf(clazz); // 获取类型下的所有枚举 + String finalLabel = label; + E matchEnum = allEnums.stream() + .filter(e -> ObjectUtil.equal(e.getLabel(), finalLabel)) + .findFirst() + .orElse(null); + + Object value = null; + if (matchEnum != null) { + value = matchEnum.getValue(); + } + return value; + } + + }