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 744fdca..4845d55 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -31,7 +31,7 @@ public abstract class Enumeration> implements Comparabl @Nonnull protected final String name; - protected Enumeration(int id, @Nonnull String name) { + protected Enumeration(final int id, @Nonnull final String name) { this.id = id; this.name = name; } @@ -46,7 +46,7 @@ public abstract class Enumeration> implements Comparabl } @Override - public final int compareTo(T o) { + public final int compareTo(final T o) { return Integer.compare(this.id, o.id); } @@ -56,7 +56,7 @@ public abstract class Enumeration> implements Comparabl } @Override - public final boolean equals(Object obj) { + public final boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) @@ -70,7 +70,7 @@ public abstract class Enumeration> implements Comparabl @Override @Nonnull public final String toString() { - return getClass().getSimpleName() + "[" + id + ": " + name + "]"; + return getClass().getSimpleName() + '(' + id + ":" + name + ')'; } protected static final class ValueSet> { @@ -83,12 +83,12 @@ public abstract class Enumeration> implements Comparabl } } - private void put(@Nonnull T value) { + private void put(@Nonnull final T value) { this.values.put(value.getId(), value); } @Nonnull - public T get(int id) { + public T get(final int id) { return Objects.requireNonNull(this.values.get(id)); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java index 6e6d552..eb73a9e 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/EnumerationTests.java @@ -1,7 +1,9 @@ package xyz.zhouxy.plusone.commons; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; @@ -15,8 +17,14 @@ class EnumerationTests { @Test void testEnumeration() { - assertEquals(EntityStatus.AVAILABLE, EntityStatus.of(0)); - log.info(EntityStatus.constants().toString()); + assertTrue(EntityStatus.AVAILABLE == EntityStatus.of(0)); + assertTrue(Result.SUCCESSFUL == Result.of(1)); + Collection>> enums = Lists.newArrayList(); + enums.addAll(EntityStatus.constants()); + enums.addAll(Result.constants()); + for (Comparable> anEnum : enums) { + log.info(anEnum.toString()); + } } } @@ -30,16 +38,37 @@ 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 ENUMERATION_VALUES = new ValueSet<>( + private static final ValueSet VALUE_SET = new ValueSet<>( AVAILABLE, DISABLED); @Nonnull public static EntityStatus of(int value) { - return ENUMERATION_VALUES.get(value); + return VALUE_SET.get(value); } @Nonnull public static Collection constants() { - return ENUMERATION_VALUES.getValues(); + return VALUE_SET.getValues(); + } +} + +final class Result extends Enumeration { + private Result(int id, @Nonnull String name) { + super(id, name); + } + + 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); + + @Nonnull + public static Result of(int id) { + return VALUE_SET.get(id); + } + + @Nonnull + public static Collection constants() { + return VALUE_SET.getValues(); } }