From 1c5d5d4129b13d88a181a779f9493270de7e868a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 30 Apr 2023 11:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=8D=E5=8F=AF=E5=8F=98?= =?UTF-8?q?=20Map=20=E7=9A=84=E6=9E=84=E5=BB=BA=E6=96=B9=E5=BC=8F=EF=BC=9B?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E9=9D=99=E6=80=81=E5=B7=A5=E5=8E=82=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=88=9B=E5=BB=BA=20ValueSet=20=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/Enumeration.java | 27 ++++++++++++------- .../plusone/commons/EnumerationTests.java | 5 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java index 8c255a2..c24663c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -17,11 +17,13 @@ package xyz.zhouxy.plusone.commons.util; import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import java.util.Objects; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; + +import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; + /** * 枚举类 */ @@ -30,8 +32,9 @@ public abstract class Enumeration> implements Comparabl protected final String name; protected Enumeration(final int id, final String name) { + Assert.hasText(name, "Name of enumeration must has text."); this.id = id; - this.name = Objects.requireNonNull(name); + this.name = name; } public final int getId() { @@ -70,16 +73,20 @@ public abstract class Enumeration> implements Comparabl } protected static final class ValueSet> { - private final Map valueMap; + private final ImmutableMap valueMap; + + private ValueSet(ImmutableMap valueMap) { + this.valueMap = valueMap; + } @SafeVarargs - public ValueSet(T... values) { - Map temp = new HashMap<>(values.length); + @StaticFactoryMethod(ValueSet.class) + public static > ValueSet of(T... values) { + Builder builder = ImmutableMap.builder(); for (T value : values) { - Assert.notNull(value, "Value must not be null."); - temp.put(value.getId(), value); + builder.put(value.getId(), value); } - this.valueMap = Collections.unmodifiableMap(temp); + return new ValueSet<>(builder.build()); } public T get(int id) { diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index 983906c..b1d5154 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -38,8 +38,7 @@ final class EntityStatus extends Enumeration { public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常"); public static final EntityStatus DISABLED = new EntityStatus(1, "禁用"); - private static final ValueSet VALUE_SET = new ValueSet<>( - AVAILABLE, DISABLED); + private static final ValueSet VALUE_SET = ValueSet.of(AVAILABLE, DISABLED); public static EntityStatus of(int value) { return VALUE_SET.get(value); @@ -58,7 +57,7 @@ final class Result extends Enumeration { public static final Result SUCCESSFUL = new Result(1, "成功"); public static final Result FAILURE = new Result(0, "失败"); - private static final ValueSet VALUE_SET = new ValueSet<>(SUCCESSFUL, FAILURE); + private static final ValueSet VALUE_SET = ValueSet.of(SUCCESSFUL, FAILURE); public static Result of(int id) { return VALUE_SET.get(id);