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 90edf0a..bca9392 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,10 @@ 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 java.util.concurrent.ConcurrentHashMap; - -import javax.annotation.Nonnull; /** * 枚举类 @@ -71,26 +70,25 @@ public abstract class Enumeration> implements Comparabl } protected static final class ValueSet> { - private final Map values = new ConcurrentHashMap<>(); + private final Map valueMap; @SafeVarargs public ValueSet(T... values) { + Map valueMap = new HashMap<>(values.length); for (T value : values) { - put(value); + Assert.notNull(value, "Value must not be null."); + valueMap.put(value.getId(), value); } - } - - private void put(final T value) { - this.values.put(value.getId(), Objects.requireNonNull(value, "Value must not be null.")); + this.valueMap = Collections.unmodifiableMap(valueMap); } public T get(int id) { - Assert.isTrue(this.values.containsKey(id), "%s 对应的值不存在", id); - return this.values.get(id); + Assert.isTrue(this.valueMap.containsKey(id), "%s 对应的值不存在", id); + return this.valueMap.get(id); } - public Collection<@Nonnull T> getValues() { - return this.values.values(); + public Collection getValues() { + return this.valueMap.values(); } } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index 619c4d8..983906c 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -1,6 +1,6 @@ package xyz.zhouxy.plusone.commons; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertSame; import com.google.common.collect.Lists; import org.junit.jupiter.api.Test; @@ -17,8 +17,8 @@ class EnumerationTests { @Test void testEnumeration() { - assertTrue(EntityStatus.AVAILABLE == EntityStatus.of(0)); - assertTrue(Result.SUCCESSFUL == Result.of(1)); + assertSame(EntityStatus.AVAILABLE, EntityStatus.of(0)); + assertSame(Result.SUCCESSFUL, Result.of(1)); Collection>> enums = Lists.newArrayList(); enums.addAll(EntityStatus.constants()); enums.addAll(Result.constants());